· 5 years ago · Aug 06, 2020, 11:12 AM
1<?php
2// Abort if this file is called directly.
3if ( ! defined( 'WPINC' ) ) {
4 die;
5}
6
7/**
8 * Class API
9 *
10 * WP REST API Custom Methods
11 *
12 */
13class API extends WP_REST_Server {
14
15 private $version;
16 private $namespace;
17
18 /**
19 * Constructor.
20 *
21 * @since 1.0.0
22 */
23 public function __construct()
24 {
25 parent::__construct();
26
27 $this->version = '1.0.0';
28 $this->namespace = 'dentons-digital/secure-blocks/v' . $this->version;
29 $this->run();
30 }
31
32 /**
33 * Run all of the plugin functions.
34 *
35 * @since 1.0.0
36 */
37 public function run() {
38 add_action( 'rest_api_init', array( $this, 'multi_sites' ) );
39 }
40
41 /**
42 * Register REST API endpoints.
43 */
44
45 public function multi_sites() {
46 // get possible multisites
47 register_rest_route(
48 $this->namespace,
49 '/multi_sites',
50 array(
51 'methods' => 'POST',
52 'callback' => array( $this, 'get_multisite_data' ),
53 'permission_callback' => function () {
54 return (int)current_user_can( 'edit_posts' );
55 },
56 )
57 );
58 // getting current checkbox data
59 register_rest_route(
60 $this->namespace,
61 '/set_checkboxes',
62 array(
63 'methods' => 'POST',
64 'callback' => array( $this, 'save_checkbox_data' ),
65 'permission_callback' => function () {
66 return (int)current_user_can( 'edit_posts' );
67 },
68 )
69 );
70 }
71
72 /**
73 * Save checkbox status to database
74 *
75 * @return $update_result The new meta field ID if a field with the given key didn't exist and was therefore added, true on successful update, false on failure.
76 * @param {object} $data Object of checkbox statuses.
77 */
78 public function save_checkbox_data($data) {
79 $data = $data->get_json_params();
80 $sites = json_encode($data["sites"]);
81 $update_result = update_post_meta($data["id"], '_post_sites', $sites );
82 return $update_result;
83 }
84
85 /**
86 * Figure out which checkboxes need to be ticked on load and which ones need to be shown on the post settings.
87 *
88 * @return $sites An array of details for each checkbox
89 * @param {data} $data Object of checkbox statuses.
90 */
91 public function get_multisite_data($data) {
92 $data = $data->get_json_params();
93 switch_to_blog( $data['blog_id'] );
94 $parent_id = get_post_meta($data["id"], '_parent_post_id', true);
95 restore_current_blog();
96
97 if($parent_id == "" || $parent_id == null){
98
99 $checkboxes = get_post_meta($data["id"], "_post_sites", true);
100 }else{
101 $checkboxes = get_post_meta((int)$parent_id, "_post_sites", true);
102 }
103
104 $checkboxes = json_decode($checkboxes);
105 $blog_ids = array();
106 foreach($checkboxes as $checkbox){
107 if ($checkbox->active == true){
108 $blog_ids[] = $checkbox->id;
109 }
110 }
111 $index = 0;
112 foreach(get_sites() as $site){
113 $current_blog_details = get_blog_details( array( 'blog_id' => $site->blog_id ) );
114 $sites[] = array(
115 'id' => $site->blog_id,
116 'name' => $current_blog_details->blogname,
117 );
118 if (in_array($site->blog_id, $blog_ids)){
119 $sites[$index]['active'] = true;
120 }else{
121 $sites[$index]['active'] = false;
122 }
123 $index++;
124 }
125 return $sites;
126 }
127}