PluginProbe
Social Media Auto Poster – Schedule & Publish to Buffer / 6.2.0
Social Media Auto Poster – Schedule & Publish to Buffer v6.2.0
6.2.4 6.2.3 6.2.2 6.2.1 6.2.0 6.1.2 6.1.1 6.1.0 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 6.0.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 All 125 releases
wp-to-buffer / lib / social / includes / class-settings.php

class-settings.php in Social Media Auto Poster – Schedule & Publish to Buffer 6.2.0, at lib/social/includes/class-settings.php

965 lines 27.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Settings class
4 *
5 * @package WPZinc\Social
6 * @author WP Zinc
7 */
8
9 namespace WPZinc\Social;
10
11 /**
12 * Handles reading and writing settings.
13 *
14 * @package WPZinc\Social
15 * @author WP Zinc
16 * @version 3.0.0
17 */
18 class Settings {
19
20 /**
21 * Holds the class object.
22 *
23 * @since 3.1.4
24 *
25 * @var object
26 */
27 public static $instance;
28
29 /**
30 * Holds the base object.
31 *
32 * @since 3.4.7
33 *
34 * @var object
35 */
36 public $base;
37
38 /**
39 * Constructor
40 *
41 * @since 3.4.7
42 *
43 * @param object $base Base Plugin Class.
44 */
45 public function __construct( $base ) {
46
47 // Store base class.
48 $this->base = $base;
49
50 }
51
52 /**
53 * Retrieves a setting from the options table.
54 *
55 * Safely checks if the key(s) exist before returning the default
56 * or the value.
57 *
58 * @since 3.0.0
59 *
60 * @param string $type Setting Type.
61 * @param string $key Setting key value to retrieve.
62 * @param string $default_value Default Value.
63 * @return string Value/Default Value
64 */
65 public function get_setting( $type, $key, $default_value = '' ) {
66
67 // Get settings.
68 $settings = $this->get_settings( $type );
69
70 // Convert string to keys.
71 $keys = explode( '][', $key );
72
73 foreach ( $keys as $count => $key ) {
74 // Cleanup key.
75 $key = trim( $key, '[]' );
76
77 // Check if key exists.
78 if ( ! isset( $settings[ $key ] ) ) {
79 return $default_value;
80 }
81
82 // Key exists - make settings the value (which could be an array or the final value)
83 // of this key.
84 $settings = $settings[ $key ];
85 }
86
87 // If here, setting exists.
88 // This will be a non-array value.
89 return $settings;
90
91 }
92
93 /**
94 * Returns the settings for the given Post Type
95 *
96 * @since 3.0.0
97 *
98 * @param string $type Type.
99 * @return array Settings
100 */
101 public function get_settings( $type ) {
102
103 // Get current settings.
104 $settings = get_option( $this->base->plugin->settingsName . '-' . $type );
105
106 /**
107 * Filters Post Type Settings before they are returned.
108 *
109 * @since 3.0.0
110 *
111 * @param array $settings Settings.
112 * @param string $type Post Type.
113 */
114 $settings = apply_filters( $this->base->plugin->filter_name . '_get_settings', $settings, $type );
115
116 // Return result.
117 return $settings;
118
119 }
120
121 /**
122 * Stores the given settings for the given Post Type into the options table
123 *
124 * @since 3.0.0
125 *
126 * @param string $type Type.
127 * @param array $settings Settings.
128 * @return mixed array (error) | bool (success)
129 */
130 public function update_settings( $type, $settings ) {
131
132 // Get old settings.
133 $existing_settings = $this->get_settings( $type );
134
135 // Iterate through array of Post Type Settings to strip HTML tags.
136 $settings = $this->strip_tags_deep( $settings );
137
138 /**
139 * Filters Post Type Settings before they are saved.
140 *
141 * @since 3.0.0
142 *
143 * @param array $settings Settings.
144 * @param string $type Post Type.
145 * @param array $existing_settings Existing Settings.
146 */
147 $settings = apply_filters( $this->base->plugin->filter_name . '_update_settings', $settings, $type, $existing_settings );
148
149 // Save.
150 $result = $this->update_option( $type, $settings );
151
152 // If update_option failed, either no settings were changed, or they were changeed but the DB collation is wrong.
153 if ( ! $result ) {
154 // Check if the existing and new settings differ i.e. the user actually made a change.
155 if ( md5( maybe_serialize( $existing_settings ) ) !== md5( maybe_serialize( $settings ) ) ) {
156 // Settings were changed, but could not be saved using update_option.
157 // Check the DB collation.
158 if ( ! $this->base->get_class( 'common' )->is_table_charset_and_collation_correct( 'options', 'utf8mb4' ) ) {
159 return new \WP_Error(
160 $this->base->plugin->filter_name . '_settings_update_settings_db_collation_error',
161 sprintf(
162 /* translators: %1$s: Documentation URL */
163 __( 'Unable to save settings due to an invalid database collation and charset on the options table. Please refer to the <a href="%1$s" target="_blank">Documentation</a>.', 'wp-to-buffer' ),
164 'https://www.wpzinc.com/documentation/wordpress-buffer-pro/debugging-issues/#unable-to-save-settings-due-to-an-invalid-database-collation-and-charset-on-the-options-table'
165 ),
166 );
167 }
168
169 // No changes were made to the settings.
170 return new \WP_Error(
171 $this->base->plugin->filter_name . '_settings_update_settings_no_changes',
172 __( 'Unable to save settings due to an error. Please try again.', 'wp-to-buffer' )
173 );
174 }
175 }
176
177 // Check for duplicate statuses.
178 $duplicates = $this->base->get_class( 'validation' )->check_for_duplicates( $settings );
179 if ( is_array( $duplicates ) ) {
180 // Fetch Post Type Name, Profile Name and Action Name.
181 $post_type_object = get_post_type_object( $type );
182 if ( $duplicates['profile_id'] === 'default' ) {
183 $profile = __( 'Defaults', 'wp-to-buffer' );
184 } elseif ( isset( $profiles[ $profile_id ] ) ) {
185 $profile = $profiles[ $profile_id ]['formatted_service'] . ': ' . $profiles[ $profile_id ]['formatted_username'];
186 }
187 $post_actions = $this->base->get_class( 'common' )->get_post_actions();
188 $action = $post_actions[ $duplicates['action'] ];
189
190 // Return error object.
191 return new \WP_Error(
192 $this->base->plugin->filter_name . '_settings_update_settings_duplicates',
193 sprintf(
194 /* translators: %1$s: Post Type Name, Plural, %2$s: Social Media Profile Name, %3$s: Action (Publish, Update, Repost, Bulk Publish), %4$s: Social Media Service Name (Buffer, Hootsuite) */
195 __( 'Two or more statuses defined in %1$s > %2$s > %3$s are the same. Please correct this to ensure each status update is unique, otherwise your status updates will NOT publish to %4$s as they will be seen as duplicates, which violate Facebook and Twitter\'s Terms of Service.', 'wp-to-buffer' ),
196 $post_type_object->label,
197 $profile,
198 $action,
199 $this->base->plugin->account
200 )
201 );
202 }
203
204 // No duplicate statuses found.
205 return true;
206
207 }
208
209 /**
210 * Strip HTML tags from the given array or string.
211 *
212 * @since 4.8.9
213 *
214 * @param string|array $value Setting value.
215 * @return string Setting value
216 */
217 private function strip_tags_deep( $value ) {
218
219 return is_array( $value ) ? array_map( array( $this, 'strip_tags_deep' ), $value ) : wp_strip_all_tags( $value );
220
221 }
222
223 /**
224 * Returns an array of default settings for a new installation.
225 *
226 * @since 3.4.0
227 *
228 * @param string $post_type Post Type.
229 * @return array Settings
230 */
231 public function default_installation_settings( $post_type ) {
232
233 // Define default settings.
234 $settings = array(
235 'default' => array(
236 'publish' => array(
237 'enabled' => 1,
238 'status' => array(
239 $this->get_default_status( $post_type, 'New ' . ucfirst( $post_type ) . ': {title}', $this->base->plugin->default_schedule ),
240 ),
241 ),
242 'update' => array(
243 'enabled' => 1,
244 'status' => array(
245 $this->get_default_status( $post_type, 'Updated ' . ucfirst( $post_type ) . ': {title}', $this->base->plugin->default_schedule ),
246 ),
247 ),
248 ),
249 );
250
251 /**
252 * Filters Default Post Type Settings used on Plugin Activation before they are returned.
253 *
254 * @since 3.4.0
255 *
256 * @param array $settings Settings.
257 * @param string $type Post Type.
258 */
259 $settings = apply_filters( $this->base->plugin->filter_name . '_default_installation_settings', $settings );
260
261 // Return.
262 return $settings;
263
264 }
265
266 /**
267 * Merges the given status array with the default status array,
268 * to ensure that the returned status has all expected keys
269 *
270 * @since 4.4.0
271 *
272 * @param array $status Status.
273 * @param string $post_type Post Type.
274 * @return array Status
275 */
276 public function get_status( $status, $post_type ) {
277
278 return array_merge( $this->get_default_status( $post_type, false, $this->base->plugin->default_schedule ), $status );
279
280 }
281
282 /**
283 * Returns value => label key/value arrays for Authors and Taxonomies,
284 * so that selectize instances can be populated with both labels and their values
285 *
286 * @since 4.4.0
287 *
288 * @param array $status Status.
289 * @param string $post_type Post Type.
290 * @return array Labels
291 */
292 public function get_status_value_labels( $status, $post_type ) {
293
294 $labels = array(
295 'authors' => array(),
296 );
297
298 // Authors.
299 if ( $status['authors'] !== false && $status['authors'] !== '' ) {
300 foreach ( $status['authors'] as $index => $user_id ) {
301 // Get user.
302 $user = get_user_by( 'id', absint( $user_id ) );
303
304 // Remove setting if the user no longer exists.
305 if ( ! $user ) {
306 unset( $status['authors'][ $index ] );
307 continue;
308 }
309
310 // Add label.
311 $labels['authors'][ $index ] = array(
312 'id' => $user_id,
313 'text' => $user->user_login,
314 );
315 }
316 }
317
318 // Taxonomies.
319 $taxonomies = $this->base->get_class( 'common' )->get_taxonomies( $post_type );
320 if ( is_array( $taxonomies ) && count( $taxonomies ) > 0 ) {
321 foreach ( $taxonomies as $taxonomy => $details ) {
322 $labels[ $taxonomy ] = array();
323
324 // Skip if conditions don't exist for this Taxonomy.
325 if ( ! isset( $status['terms'][ $taxonomy ] ) ) {
326 continue;
327 }
328 if ( ! is_array( $status['terms'][ $taxonomy ] ) ) {
329 continue;
330 }
331 if ( ! count( $status['terms'][ $taxonomy ] ) ) {
332 continue;
333 }
334
335 // Term(s) exist.
336 foreach ( $status['terms'][ $taxonomy ] as $index => $term_id ) {
337 // Get Term.
338 $term = get_term_by( 'id', absint( $term_id ), $taxonomy );
339
340 // Remove setting if the Term no longer exists.
341 if ( ! $term ) {
342 unset( $status['terms'][ $taxonomy ][ $index ] );
343 continue;
344 }
345
346 // Add label.
347 $labels[ $taxonomy ][ $index ] = array(
348 'id' => $term_id,
349 'text' => $term->name,
350 );
351 }
352 }
353 }
354
355 // Return.
356 return $labels;
357
358 }
359
360 /**
361 * Returns an array of a status' information that can be output in
362 * the table row cells
363 *
364 * @since 4.4.0
365 *
366 * @param array $status Status.
367 * @param string $post_type Post Type.
368 * @param string $action Action (publish,update,repost,bulk_publish).
369 * @return array Table Row Cell Status (message, image, schedule)
370 */
371 public function get_status_row( $status, $post_type, $action ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
372
373 // Get Options.
374 $featured_image_options = $this->base->get_class( 'image' )->get_status_image_options( false, $post_type );
375 $schedule = $this->base->get_class( 'common' )->get_schedule_options( $post_type, true );
376
377 // Define row.
378 return array(
379 'message' => ( ( strlen( $status['message'] ) > 100 ) ? substr( $status['message'], 0, 100 ) . '...' : $status['message'] ),
380 'post_type' => $status['post_type'],
381 'schedule' => $schedule[ $status['schedule'] ],
382 );
383
384 }
385
386 /**
387 * Returns a default status array
388 *
389 * @since 4.4.0
390 *
391 * @param string $post_type Post Type.
392 * @param mixed $default_message Default Message (if false, uses {title} {url}).
393 * @param string $default_schedule Default Schedule.
394 * @return array Status
395 */
396 public function get_default_status( $post_type, $default_message = false, $default_schedule = 'immediate' ) {
397
398 // Get Taxonomies supported by this Post Type.
399 $conditions = array();
400 $terms = array();
401 foreach ( $this->base->get_class( 'common' )->get_taxonomies( $post_type ) as $taxonomy => $object ) {
402 $conditions[ $taxonomy ] = '';
403 $terms[ $taxonomy ] = array();
404 }
405
406 // Define skeleton status to be used for new statuses.
407 $status = array(
408 // All Profiles.
409 'post_type' => 'link',
410 'message' => ( ! $default_message ? '{title}' : $default_message ),
411 'first_comment' => '',
412 'url' => '{url}',
413 'schedule' => $default_schedule,
414 'image' => 'featured_image',
415 'image_additional' => '',
416 'image_additional_limit' => '',
417 'days' => 0,
418 'hours' => 0,
419 'minutes' => 0,
420 'schedule_relative_day' => '',
421 'schedule_relative_time' => '00:00:00',
422 'schedule_custom_field_name' => '',
423 'schedule_custom_field_relation' => 'after',
424 'schedule_tec_relation' => 'after',
425 'schedule_specific' => '',
426
427 // Text to Image.
428 'text_to_image' => '',
429
430 // Profiles: Pinterest.
431 'pinterest' => array(
432 'board' => '',
433 'title' => '{title}',
434 ),
435
436 // Profiles: Google Business.
437 'googlebusiness' => array(
438 'post_type' => 'whats_new', // whats_new, offer, event.
439
440 // What's New, Event.
441 'cta' => '', // book,order,shop,learn_more,signup.
442
443 // Offer, Event.
444 'start_date_option' => 'custom',
445 'start_date' => '',
446 'end_date_option' => 'custom',
447 'end_date' => '',
448 'title' => '',
449
450 // Offer.
451 'code' => '',
452 'terms' => '',
453 ),
454
455 // Post Conditions.
456 'post_title' => array(
457 'compare' => 0,
458 'value' => '',
459 ),
460 'post_excerpt' => array(
461 'compare' => 0,
462 'value' => '',
463 ),
464 'post_content' => array(
465 'compare' => 0,
466 'value' => '',
467 ),
468 'start_date' => array(
469 'month' => '',
470 'day' => '',
471 ),
472 'end_date' => array(
473 'month' => '',
474 'day' => '',
475 ),
476
477 // Author Conditions.
478 'authors' => false,
479 'authors_compare' => '=',
480 'authors_roles' => false,
481 'authors_roles_compare' => '=',
482
483 // Taxonomy Conditions.
484 'conditions' => $conditions,
485 'terms' => $terms,
486
487 // Custom Field Conditions.
488 'custom_fields' => array(),
489 );
490
491 /**
492 * Returns a skeleton status object for the given action, used when defining new status(es)
493 *
494 * @since 4.4.0
495 *
496 * @param array $status Status.
497 */
498 $status = apply_filters( $this->base->plugin->filter_name . '_settings_get_default_status', $status );
499
500 // Return.
501 return $status;
502
503 }
504
505 /**
506 * Helper method to determine whether the given Post Type has at least
507 * one social media account enabled, and there is a publish or update
508 * action enabled in the Defaults for the Post Type or the Social Media account.
509 *
510 * @since 3.4.0
511 *
512 * @param string $post_type Post Type.
513 * @return bool Enabled
514 */
515 public function is_post_type_enabled( $post_type ) {
516
517 // Get Settings for Post Type.
518 $settings = $this->get_settings( $post_type );
519
520 // If no settings, bail.
521 if ( ! $settings ) {
522 return false;
523 }
524
525 /**
526 * Default Publish or Update enabled
527 * 1+ Profiles enabled without override
528 */
529 $default_publish_action_enabled = $this->get_setting( $post_type, '[default][publish][enabled]', 0 );
530 $default_update_action_enabled = $this->get_setting( $post_type, '[default][update][enabled]', 0 );
531 if ( $default_publish_action_enabled || $default_update_action_enabled ) {
532 foreach ( $settings as $profile_id => $profile_settings ) {
533 // Skip defaults.
534 if ( $profile_id === 'default' ) {
535 continue;
536 }
537
538 // Profile enabled, no override.
539 if ( isset( $profile_settings['enabled'] ) && $profile_settings['enabled'] ) {
540 if ( ! isset( $profile_settings['override'] ) || ! $profile_settings['override'] ) {
541 // Post Type is enabled with Defaults + 1+ Profile not using override settings.
542 return true;
543 }
544 }
545 }
546 }
547
548 /**
549 * 1+ Profiles enabled with override and publish / update enabled
550 */
551 foreach ( $settings as $profile_id => $profile_settings ) {
552 // Skip defaults.
553 if ( $profile_id === 'default' ) {
554 continue;
555 }
556
557 // Skip if profile not enabled.
558 if ( ! isset( $profile_settings['enabled'] ) || ! $profile_settings['enabled'] ) {
559 continue;
560 }
561
562 // Skip if override not enabled.
563 if ( ! isset( $profile_settings['override'] ) || ! $profile_settings['override'] ) {
564 continue;
565 }
566
567 // Profile action enabled.
568 if ( isset( $profile_settings['publish']['enabled'] ) && $profile_settings['publish']['enabled'] == '1' ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
569 // Post Type is enabled with 1+ Profile with override and publish enabled.
570 return true;
571 }
572 if ( isset( $profile_settings['update']['enabled'] ) && $profile_settings['update']['enabled'] == '1' ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
573 // Post Type is enabled with 1+ Profile with override and update enabled.
574 return true;
575 }
576 if ( isset( $profile_settings['repost']['enabled'] ) && $profile_settings['repost']['enabled'] == '1' ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
577 // Post Type is enabled with 1+ Profile with override and repost enabled.
578 return true;
579 }
580 if ( isset( $profile_settings['bulk_publish']['enabled'] ) && $profile_settings['bulk_publish']['enabled'] == '1' ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
581 // Post Type is enabled with 1+ Profile with override and bulk publish enabled.
582 return true;
583 }
584 }
585
586 // If here, Post Type can't be sent to the API.
587 return false;
588
589 }
590
591 /**
592 * Runs the given individual status settings through validation.
593 *
594 * @since 3.7.3
595 *
596 * @param array $status Status Message Settings.
597 * @return array Status Message Settings
598 */
599 private function validate_status( $status ) {
600
601 /**
602 * Filters status settings during validation, allowing them to be changed.
603 *
604 * @since 3.7.3
605 *
606 * @param array $status Status.
607 */
608 $status = apply_filters( $this->base->plugin->filter_name . '_settings_validate_status', $status );
609
610 // Return.
611 return $status;
612
613 }
614
615 /**
616 * Returns all accounts and their access token, refresh token and token expiry values.
617 *
618 * @since 5.4.0
619 *
620 * @return array
621 */
622 public function get_accounts() {
623
624 return get_option( $this->base->plugin->settingsName . '-accounts', array() );
625
626 }
627
628 /**
629 * Checks if at least one account is connected.
630 *
631 * @since 5.4.0
632 *
633 * @return bool
634 */
635 public function account_connected() {
636
637 $accounts = $this->get_accounts();
638 return ! empty( $accounts ) && count( $accounts ) > 0;
639
640 }
641
642 /**
643 * Stores the given account, including its credentials.
644 *
645 * @since 3.5.0
646 *
647 * @param string $access_token Access Token.
648 * @param string $refresh_token Refresh Token.
649 * @param bool|int $token_expires Token Expires (false | timestamp).
650 * @param string $account_id Account ID.
651 * @param string $account_name Account Name.
652 * @param string $account_email Account Email.
653 * @param int $account_channel_limit Account Channel Limit.
654 * @param string $plan Plan Name.
655 * @param array $profile_ids Profile IDs.
656 */
657 public function update_account( $access_token = '', $refresh_token = '', $token_expires = false, $account_id = 'default', $account_name = 'Default', $account_email = '', $account_channel_limit = 0, $plan = 'free', $profile_ids = array() ) {
658
659 // Get existing accounts.
660 $accounts = $this->get_accounts();
661
662 // Update account.
663 $accounts[ $account_id ] = array(
664 'id' => $account_id,
665 'name' => $account_name,
666 'email' => $account_email,
667 'channel_limit' => $account_channel_limit,
668 'plan' => $plan,
669 'access_token' => $access_token,
670 'refresh_token' => $refresh_token,
671 'token_expires' => $token_expires,
672 'profile_ids' => $profile_ids,
673 );
674
675 // Update the accounts.
676 update_option( $this->base->plugin->settingsName . '-accounts', $accounts );
677
678 }
679
680 /**
681 * Updates the credentials for the given account ID.
682 *
683 * @since 6.0.0
684 *
685 * @param string $access_token Access Token.
686 * @param string $refresh_token Refresh Token.
687 * @param bool|int $token_expires Token Expires (false | timestamp).
688 * @param string $account_id Account ID.
689 */
690 public function update_account_credentials( $access_token, $refresh_token, $token_expires = false, $account_id = 'default' ) {
691
692 // Get existing accounts.
693 $accounts = $this->get_accounts();
694
695 // If the account doesn't exist, bail.
696 if ( ! isset( $accounts[ $account_id ] ) ) {
697 return;
698 }
699
700 // Update the account credentials.
701 $accounts[ $account_id ]['access_token'] = $access_token;
702 $accounts[ $account_id ]['refresh_token'] = $refresh_token;
703 $accounts[ $account_id ]['token_expires'] = $token_expires;
704
705 // Update the accounts.
706 update_option( $this->base->plugin->settingsName . '-accounts', $accounts );
707
708 }
709
710 /**
711 * Updates the information for the given account ID.
712 *
713 * @since 6.0.0
714 *
715 * @param string $account_id Account ID.
716 * @param string $account_name Account Name.
717 * @param string $account_email Account Email.
718 * @param int $account_channel_limit Account Channel Limit.
719 * @param string $plan Plan Name.
720 * @param array $profile_ids Profile IDs.
721 */
722 public function update_account_information( $account_id = 'default', $account_name = 'Default', $account_email = '', $account_channel_limit = 0, $plan = 'free', $profile_ids = array() ) {
723
724 // Get existing accounts.
725 $accounts = $this->get_accounts();
726
727 // If the account doesn't exist, bail.
728 if ( ! isset( $accounts[ $account_id ] ) ) {
729 return;
730 }
731
732 // Update the account information.
733 $accounts[ $account_id ]['name'] = $account_name;
734 $accounts[ $account_id ]['plan'] = $plan;
735 $accounts[ $account_id ]['email'] = $account_email;
736 $accounts[ $account_id ]['channel_limit'] = $account_channel_limit;
737 $accounts[ $account_id ]['profile_ids'] = $profile_ids;
738
739 // Update the accounts.
740 update_option( $this->base->plugin->settingsName . '-accounts', $accounts );
741
742 }
743
744 /**
745 * Stores the given profile IDs against the given account ID.
746 *
747 * @since 5.4.7
748 *
749 * @param string $account_id Account ID.
750 * @param array $profile_ids Profile IDs.
751 */
752 public function update_account_profile_ids( $account_id, $profile_ids ) {
753
754 // Get existing accounts.
755 $accounts = $this->get_accounts();
756
757 // If the account doesn't exist, bail.
758 if ( ! isset( $accounts[ $account_id ] ) ) {
759 return;
760 }
761
762 // Update the account profile IDs.
763 $accounts[ $account_id ]['profile_ids'] = $profile_ids;
764
765 // Update the accounts.
766 update_option( $this->base->plugin->settingsName . '-accounts', $accounts );
767
768 }
769
770 /**
771 * Deletes the access, refresh and token expiry values for the specified account ID.
772 *
773 * @since 3.5.0
774 *
775 * @param string $account_id Account ID.
776 * @return bool
777 */
778 public function delete_account( $account_id = 'default' ) {
779
780 // Get existing accounts.
781 $accounts = $this->get_accounts();
782
783 // Delete the account.
784 unset( $accounts[ $account_id ] );
785
786 // Delete the stored profiles for this account.
787 delete_option( $this->base->plugin->name . '-profiles-' . $account_id );
788
789 // Update the accounts.
790 return update_option( $this->base->plugin->settingsName . '-accounts', $accounts );
791
792 }
793
794 /**
795 * Retrieves the account IDs for the given access token.
796 *
797 * @since 6.0.1
798 *
799 * @param string $access_token Access Token.
800 * @return array
801 */
802 public function get_account_ids_by_access_token( $access_token ) {
803
804 $account_ids = array();
805
806 // Get existing accounts.
807 $accounts = $this->get_accounts();
808
809 // Iterate through accounts to find the account that contains the given access token.
810 foreach ( $accounts as $account_id => $account ) {
811 if ( $account['access_token'] === $access_token ) {
812 $account_ids[] = $account_id;
813 }
814 }
815
816 return $account_ids;
817
818 }
819
820 /**
821 * Retrieves the access token for the given profile ID.
822 *
823 * @since 5.4.0
824 *
825 * @param string $profile_id Profile ID.
826 * @return string
827 */
828 public function get_access_token_by_profile_id( $profile_id = '' ) {
829
830 // Get existing accounts.
831 $accounts = $this->get_accounts();
832
833 // Iterate through accounts to find the account that contains the given profile ID.
834 foreach ( $accounts as $account ) {
835 if ( in_array( $profile_id, $account['profile_ids'], true ) ) {
836 return $account['access_token'];
837 }
838 }
839
840 return '';
841
842 }
843
844 /**
845 * Retrieves the refresh token for the given profile ID.
846 *
847 * @since 5.4.0
848 *
849 * @param string $profile_id Profile ID.
850 * @return string
851 */
852 public function get_refresh_token_by_profile_id( $profile_id = '' ) {
853
854 // Get existing accounts.
855 $accounts = $this->get_accounts();
856
857 // Iterate through accounts to find the account that contains the given profile ID.
858 foreach ( $accounts as $account ) {
859 if ( in_array( $profile_id, $account['profile_ids'], true ) ) {
860 return $account['refresh_token'];
861 }
862 }
863
864 return '';
865
866 }
867
868 /**
869 * Retrieves the access token for the given profile ID.
870 *
871 * @since 5.4.0
872 *
873 * @param string $profile_id Profile ID.
874 * @return string
875 */
876 public function get_token_expires_by_profile_id( $profile_id = '' ) {
877
878 // Get existing accounts.
879 $accounts = $this->get_accounts();
880
881 // Iterate through accounts to find the account that contains the given profile ID.
882 foreach ( $accounts as $account ) {
883 if ( in_array( $profile_id, $account['profile_ids'], true ) ) {
884 return $account['token_expires'];
885 }
886 }
887
888 return '';
889
890 }
891
892 /**
893 * Helper method to get a value from the options table
894 *
895 * @since 3.0.0
896 *
897 * @param string $key Option Key.
898 * @param string $default_value Default Value if key does not exist.
899 * @return string Option Value
900 */
901 public function get_option( $key, $default_value = '' ) {
902
903 $result = get_option( $this->base->plugin->settingsName . '-' . $key );
904 if ( ! $result ) {
905 return $default_value;
906 }
907
908 return $result;
909
910 }
911
912 /**
913 * Helper method to store a value to the options table
914 *
915 * @since 3.0.0
916 *
917 * @param string $key Key.
918 * @param string $value Value.
919 * @return bool Success
920 */
921 public function update_option( $key, $value ) {
922
923 // Depending on the key, perform some validation before saving.
924 switch ( $key ) {
925 /**
926 * Custom Tags
927 * - Remove duplicate keys.
928 */
929 case 'custom_tags':
930 // Skip validation if there are no custom field key/values to validate.
931 if ( count( $value ) === 0 ) {
932 break;
933 }
934
935 foreach ( $value as $post_type => $custom_tags ) {
936 // Remove duplicate keys.
937 $value[ $post_type ]['key'] = array_unique( array_filter( $custom_tags['key'] ) );
938
939 // Iterate through labels, removing them if there is now no key.
940 foreach ( $custom_tags['label'] as $label_key => $label ) {
941 if ( ! isset( $value[ $post_type ]['key'][ $label_key ] ) ) {
942 unset( $value[ $post_type ]['label'][ $label_key ] );
943 }
944 }
945 }
946 break;
947 }
948
949 /**
950 * Filters the key and value pair before saving to the options table.
951 *
952 * @since 3.0.0
953 *
954 * @param string $value Option Value.
955 * @param string $key Option Key.
956 */
957 $value = apply_filters( $this->base->plugin->filter_name . '_update_option', $value, $key );
958
959 // Update.
960 return update_option( $this->base->plugin->settingsName . '-' . $key, $value );
961
962 }
963
964 }
965