PluginProbe
SchedulePress – Auto Post & Publish, Auto Social Share, Schedule Posts with Editorial Calendar & Missed Schedule Post Publisher / trunk
SchedulePress – Auto Post & Publish, Auto Social Share, Schedule Posts with Editorial Calendar & Missed Schedule Post Publisher vtrunk
5.3.3 5.3.2 5.3.1 5.3.0 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.1.8 5.1.9 5.2.0 5.2.1 5.2.10 5.2.11 5.2.12 5.2.13 5.2.14 5.2.15 5.2.16 5.2.17 5.2.18 5.2.2 5.2.3 5.2.4 All 117 releases
wp-scheduled-posts / includes / API / Settings.php

Settings.php in SchedulePress – Auto Post & Publish, Auto Social Share, Schedule Posts with Editorial Calendar & Missed Schedule Post Publisher trunk, at includes/API/Settings.php

493 lines 17.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPSP\API;
4 use WPSP;
5 use WPSP\Social\ReconnectHandler;
6 use WPSP\Social\SocialProfile;
7 use WPSP\Helper;
8
9 class Settings
10 {
11 /**
12 * Main Setting Option Name
13 *
14 * @since 1.0.0
15 *
16 * @var string
17 */
18 private $settings_name = null;
19 /**
20 * Instance of this class.
21 *
22 * @since 1.0.0
23 *
24 * @var object
25 */
26 protected static $instance = null;
27
28 /**
29 * Initialize hooks and option name
30 */
31 private function __construct()
32 {
33 $this->settings_name = WPSP_SETTINGS_NAME;
34 $this->do_hooks();
35 }
36
37 /**
38 * Set up WordPress hooks and filters
39 *
40 * @return void
41 */
42 public function do_hooks()
43 {
44 add_action('rest_api_init', array($this, 'register_routes'));
45 add_action('rest_api_init', array($this, 'register_social_profile_routes'));
46 add_action('rest_api_init', array($this, 'meta_rest_api'));
47 }
48 public function meta_rest_api() {
49 $allow_post_types = \WPSP\Helper::get_all_allowed_post_type();
50 $allow_post_types = (!empty($allow_post_types) ? $allow_post_types : array('post'));
51 foreach ($allow_post_types as $type) {
52 register_post_meta(
53 $type,
54 '_wpscppro_dont_share_socialmedia',
55 [
56 'show_in_rest' => true,
57 'single' => true,
58 'type' => 'boolean',
59 'default' => false,
60 'auth_callback' => function() {
61 return current_user_can( 'edit_posts' );
62 }
63 ]
64 );
65 register_post_meta(
66 $type,
67 '_wpscppro_custom_social_share_image',
68 [
69 'show_in_rest' => true,
70 'single' => true,
71 'type' => 'integer',
72 'auth_callback' => function() {
73 return current_user_can( 'edit_posts' );
74 }
75 ]
76 );
77
78 $social_media_meta_key = ['_facebook_share_type', '_twitter_share_type', '_linkedin_share_type', '_pinterest_share_type', '_linkedin_share_type_page', '_instagram_share_type', '_medium_share_type', '_threads_share_type','_google_business_share_type', '_bluesky_share_type', '_mastodon_share_type'];
79 // Social media meta
80 foreach ($social_media_meta_key as $value) {
81 register_post_meta(
82 $type,
83 $value,
84 [
85 'show_in_rest' => true,
86 'single' => true,
87 'type' => 'string',
88 'auth_callback' => function() {
89 return current_user_can( 'edit_posts' );
90 }
91 ]
92 );
93 }
94
95 // Save selected profile for specific page
96 register_post_meta(
97 $type,
98 '_selected_social_profile',
99 [
100 'show_in_rest' => [
101 'schema' => [
102 'type' => 'array',
103 'items' => [
104 'type' => 'object',
105 'properties' => [
106 'id' => ['type' => ['string','integer']],
107 'postid' => ['type' => 'integer'],
108 'platform' => ['type' => 'string'],
109 'platformKey' => ['type' => 'integer'],
110 'pinterest_custom_board_name' => ['type' => 'string'],
111 'pinterest_custom_section_name' => ['type' => 'string'],
112 'name' => ['type' => 'string'],
113 'thumbnail_url' => ['type' => 'string'],
114 'type' => ['type' => 'string'],
115 'share_type' => ['type' => 'string'],
116 'pinterest_board_type' => ['type' => 'string'],
117 'nonce' => ['type' => 'string'],
118 ],
119 ],
120 ],
121 ],
122 'single' => true,
123 'type' => 'array',
124 'auth_callback' => function() {
125 return current_user_can( 'edit_posts' );
126 }
127 ]
128 );
129
130
131 }
132
133 }
134
135
136
137
138 public function register_social_profile_routes()
139 {
140 $namespace = WPSP_PLUGIN_SLUG . '/v1';
141 // Get option data
142 register_rest_route($namespace, 'get-option-data', array(
143 'methods' => 'GET',
144 'callback' => array($this, 'wpsp_get_options_data'),
145 'permission_callback' => function() {
146 return current_user_can( 'edit_posts' );
147 }
148 ));
149
150 // Instant share on social media
151 register_rest_route($namespace,'instant-social-share',array(
152 'methods' => 'GET',
153 'callback' => array($this, 'wpsp_instant_social_share'),
154 'permission_callback' => function() {
155 return current_user_can( 'edit_posts' );
156 }
157 ));
158
159
160
161 register_rest_route($namespace,'get-categories',array(
162 'methods' => 'GET',
163 'callback' => array($this, 'wpsp_get_categories'),
164 'permission_callback' => function() {
165 return current_user_can( 'edit_posts' );
166 }
167 ));
168 register_rest_route($namespace,'update-refresh-token',array(
169 'methods' => 'POST',
170 'callback' => array($this, 'wpsp_update_refresh_token'),
171 'permission_callback' => function() {
172 return current_user_can( 'edit_posts' );
173 }
174 ));
175 }
176
177
178 public function wpsp_update_refresh_token(\WP_REST_Request $request) {
179 $platform = $request->get_param('platform');
180 $item = $request->get_param('item');
181 $response = ReconnectHandler::handleProfileReconnect($platform, $item);
182 die();
183 }
184
185 public function wpsp_get_categories(\WP_REST_Request $request)
186 {
187 $limit = $request->get_param('limit') ?: 10;
188 $page = $request->get_param('page') ?: 1;
189 $categories = $this->get_options_with_pagination($limit, $page);
190 return rest_ensure_response($categories);
191 }
192
193 /**
194 * Get categories with pagination.
195 *
196 * @param int $limit Number of items per page.
197 * @param int $page Current page number.
198 *
199 * @return array List of categories with the desired format.
200 */
201 function get_options_with_pagination($limit, $page)
202 {
203 $allowed_post_types = \WPSP\Helper::get_all_allowed_post_type(); // Fetch allowed post types
204 $result = ['result' => []];
205 $offset = ($page - 1) * $limit;
206
207 foreach ($allowed_post_types as $post_type) {
208 $taxonomies = get_object_taxonomies($post_type); // Get taxonomies for each post type
209
210 foreach ($taxonomies as $taxonomy) {
211 $args = [
212 'taxonomy' => $taxonomy,
213 'hide_empty' => false, // Include terms without posts
214 'number' => $limit,
215 'offset' => $offset,
216 ];
217
218 $terms = get_terms($args);
219
220 if (!is_wp_error($terms)) {
221 foreach ($terms as $term) {
222 $result['result'][] = [
223 'term_id' => $term->term_id,
224 'label' => $term->name,
225 'slug' => $term->slug,
226 'taxonomy' => $term->taxonomy,
227 'postType' => $post_type,
228 'value' => $post_type . '.' . $term->taxonomy . '.' . $term->slug
229 ];
230 }
231 }
232 }
233 }
234 return $result['result']; // Returning the 'result' key
235 }
236
237 // Instant social share
238 public function wpsp_instant_social_share( $data )
239 {
240 do_action('wpsp_instant_social_single_profile_share', $data->get_params());
241 }
242
243
244
245
246
247 public function wpsp_get_options_data( $request ) {
248 if ( !Helper::is_user_allow() ) {
249 return new \WP_Error('unauthorized', 'Unauthorized', array('status' => 401));
250 }
251
252 $option_value = get_option('wpsp_settings_v5');
253 if ($option_value !== false) {
254 $option_value = json_decode($option_value, true);
255 // Check and process `linkedin_profile_list` if it exists
256 if (isset($option_value['linkedin_profile_list']) && is_array($option_value['linkedin_profile_list'])) {
257 $option_value['linkedin_profile_list'] = array_map(function($profile) {
258 if (isset($profile['__id']) && isset($profile['id'])) {
259 $profile['id'] = $profile['__id'];
260 unset($profile['__id']);
261 }
262 if (!isset($profile['thumbnail_url']) || $profile['thumbnail_url'] === null) {
263 $profile['thumbnail_url'] = '';
264 }
265 return $profile;
266 }, $option_value['linkedin_profile_list']);
267 }
268
269 // set default thumanil url
270 $social_media_lists = [
271 'facebook_profile_list',
272 'twitter_profile_list',
273 'instagram_profile_list',
274 'pinterest_profile_list',
275 'threads_profile_list',
276 'medium_profile_list',
277 ];
278
279 foreach ($social_media_lists as $list_key) {
280 if (isset($option_value[$list_key]) && is_array($option_value[$list_key])) {
281 $option_value[$list_key] = array_map(function($profile) {
282 // Set default value for thumbnail_url if null
283 if (!isset($profile['thumbnail_url']) || $profile['thumbnail_url'] === null) {
284 $profile['thumbnail_url'] = '';
285 }
286 if (!isset($profile['name']) || $profile['name'] === null) {
287 $profile['name'] = '';
288 }
289 return $profile;
290 }, $option_value[$list_key]);
291 }
292 }
293 $option_value = json_encode($option_value);
294 return rest_ensure_response($option_value);
295 } else {
296 return new \WP_Error('option_not_found', 'Option not found', array('status' => 40));
297 }
298 }
299
300
301 /**
302 * Return an instance of this class.
303 *
304 * @since 0.8.1
305 *
306 * @return object A single instance of this class.
307 */
308 public static function get_instance()
309 {
310
311 // If the single instance hasn't been set, set it now.
312 if (null == self::$instance) {
313 self::$instance = new self;
314 }
315
316 return self::$instance;
317 }
318
319
320 /**
321 * Register the routes for the objects of the controller.
322 */
323 public function register_routes()
324 {
325 $namespace = WPSP_PLUGIN_SLUG . '/v1';
326 $endpoint = apply_filters('wpsp_rest_endpoint', '/settings/');
327
328 register_rest_route($namespace, $endpoint, array(
329 array(
330 'methods' => \WP_REST_Server::READABLE,
331 'callback' => array($this, 'get_value'),
332 'permission_callback' => array($this, 'wpsp_permissions_check'),
333 'args' => array(),
334 ),
335 ));
336
337 register_rest_route($namespace, $endpoint, array(
338 array(
339 'methods' => \WP_REST_Server::CREATABLE,
340 'callback' => array($this, 'update_value'),
341 'permission_callback' => array($this, 'wpsp_permissions_check'),
342 'args' => array(),
343 ),
344 ));
345
346 register_rest_route($namespace, $endpoint, array(
347 array(
348 'methods' => \WP_REST_Server::EDITABLE,
349 'callback' => array($this, 'update_value'),
350 'permission_callback' => array($this, 'wpsp_permissions_check'),
351 'args' => array(),
352 ),
353 ));
354
355 register_rest_route($namespace, $endpoint, array(
356 array(
357 'methods' => \WP_REST_Server::DELETABLE,
358 'callback' => array($this, 'delete_value'),
359 'permission_callback' => array($this, 'wpsp_permissions_check'),
360 'args' => array(),
361 ),
362 ));
363
364 register_rest_route($namespace, 'fetch_pinterest_section', array(
365 array(
366 'methods' => \WP_REST_Server::EDITABLE,
367 'callback' => array($this, 'fetch_pinterest_section'),
368 'permission_callback' => array($this, 'wpsp_permissions_check'),
369 'args' => array(),
370 ),
371 ));
372
373 register_rest_route( $namespace, '/save-profile', array(
374 'methods' => 'POST',
375 'callback' => [$this, 'save_profile'],
376 'permission_callback' => [$this, 'wpsp_permissions_check'],
377 ));
378
379 register_rest_route($namespace, 'fetch_pinterest_section', array(
380 array(
381 'methods' => \WP_REST_Server::EDITABLE,
382 'callback' => array($this, 'fetch_pinterest_section'),
383 'permission_callback' => array($this, 'wpsp_permissions_check'),
384 'args' => array(),
385 ),
386 ));
387
388 }
389
390
391 public function save_profile($request)
392 {
393 $platform = $request->get_param('platform');
394 $profiles = $request->get_param('profiles');
395 foreach ($profiles as $profile) {
396 do_action("wpsp_profile_reconnect_{$platform}", [ 'id' => $profile ] );
397 }
398
399 }
400
401
402 /**
403 * Fetch pinterest section
404 *
405 * @param $data
406 */
407 public function fetch_pinterest_section($data)
408 {
409 do_action('social_profile_fetch_pinterest_section', $data->get_params());
410 }
411
412 /**
413 * Get wpsp
414 *
415 * @param WP_REST_Request $request Full data about the request.
416 * @return WP_Error|WP_REST_Request
417 */
418 public function get_value($request)
419 {
420 $wpsp_option = get_option($this->settings_name);
421 // Don't return false if there is no option
422 if (!$wpsp_option) {
423 return new \WP_REST_Response(array(
424 'success' => true,
425 'value' => ''
426 ), 200);
427 }
428
429 return new \WP_REST_Response(array(
430 'success' => true,
431 'value' => $wpsp_option,
432 ), 200);
433 }
434
435 /**
436 * Create OR Update wpsp
437 *
438 * @param \WP_REST_Request $request Full data about the request.
439 * @return \WP_Error|\WP_REST_Request
440 */
441 public function update_value($request)
442 {
443 $settings = $request->get_params();
444 $arr = ['allow_post_types', 'allow_categories', 'allow_user_by_role'];
445 $settingObject = WPSP_Start()->getAdmin()->load_settings();
446
447 $settings_arr = $settingObject->get_settings_array();
448 $defaults = $settingObject->get_field_names($settings_arr['tabs']);
449
450 foreach($arr as $key){
451 if(!empty($defaults[$key]) && empty($settings[$key])){
452 $settings[$key] = $defaults[$key];
453 }
454 }
455
456 $settings = apply_filters('wpsp_settings_before_save', $settings);
457 $updated = update_option($this->settings_name, json_encode($settings));
458
459 return new \WP_REST_Response(array(
460 'success' => $updated,
461 'value' => $request->get_params()
462 ), 200);
463 }
464
465 /**
466 * Delete wpsp
467 *
468 * @param WP_REST_Request $request Full data about the request.
469 * @return WP_Error|WP_REST_Request
470 */
471 public function delete_value($request)
472 {
473 $deleted = delete_option($this->settings_name);
474
475 return new \WP_REST_Response(array(
476 'success' => $deleted,
477 'value' => ''
478 ), 200);
479 }
480
481 /**
482 * Check if a given request has access to update a setting
483 *
484 * @param WP_REST_Request $request Full data about the request.
485 * @return WP_Error|bool
486 */
487 public function wpsp_permissions_check($request)
488 {
489 return current_user_can('manage_options');
490 }
491
492 }
493