| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO\Admin |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Class for managing integration toggles. |
| 10 |
*/ |
| 11 |
class Yoast_Integration_Toggles { |
| 12 |
|
| 13 |
/** |
| 14 |
* Available integration toggles. |
| 15 |
* |
| 16 |
* @var array |
| 17 |
*/ |
| 18 |
protected $toggles; |
| 19 |
|
| 20 |
/** |
| 21 |
* Instance holder. |
| 22 |
* |
| 23 |
* @var self|null |
| 24 |
*/ |
| 25 |
protected static $instance = null; |
| 26 |
|
| 27 |
/** |
| 28 |
* Gets the main integration toggles manager instance used. |
| 29 |
* |
| 30 |
* This essentially works like a Singleton, but for its drawbacks does not restrict |
| 31 |
* instantiation otherwise. |
| 32 |
* |
| 33 |
* @return self Main instance. |
| 34 |
*/ |
| 35 |
public static function instance() { |
| 36 |
self::$instance ??= new self(); |
| 37 |
|
| 38 |
return self::$instance; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Gets all available integration toggles. |
| 43 |
* |
| 44 |
* @return array List of sorted Yoast_Feature_Toggle instances. |
| 45 |
*/ |
| 46 |
public function get_all() { |
| 47 |
$this->toggles ??= $this->load_toggles(); |
| 48 |
|
| 49 |
return $this->toggles; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Loads the available integration toggles. |
| 54 |
* |
| 55 |
* Also ensures that the toggles are all Yoast_Feature_Toggle instances and sorted by their order value. |
| 56 |
* |
| 57 |
* @return array List of sorted Yoast_Feature_Toggle instances. |
| 58 |
*/ |
| 59 |
protected function load_toggles() { |
| 60 |
$integration_toggles = [ |
| 61 |
(object) [ |
| 62 |
/* translators: %s: 'Semrush' */ |
| 63 |
'name' => sprintf( __( '%s integration', 'wordpress-seo' ), 'Semrush' ), |
| 64 |
'setting' => 'semrush_integration_active', |
| 65 |
'label' => sprintf( |
| 66 |
/* translators: %s: 'Semrush' */ |
| 67 |
__( 'The %s integration offers suggestions and insights for keywords related to the entered focus keyphrase.', 'wordpress-seo' ), |
| 68 |
'Semrush', |
| 69 |
), |
| 70 |
'order' => 10, |
| 71 |
], |
| 72 |
(object) [ |
| 73 |
/* translators: %s: Algolia. */ |
| 74 |
'name' => sprintf( esc_html__( '%s integration', 'wordpress-seo' ), 'Algolia' ), |
| 75 |
'premium' => true, |
| 76 |
'setting' => 'algolia_integration_active', |
| 77 |
'label' => __( 'Improve the quality of your site search! Automatically helps your users find your cornerstone and most important content in your internal search results. It also removes noindexed posts & pages from your site’s search results.', 'wordpress-seo' ), |
| 78 |
/* translators: %s: Algolia. */ |
| 79 |
'read_more_label' => sprintf( __( 'Find out more about our %s integration.', 'wordpress-seo' ), 'Algolia' ), |
| 80 |
'read_more_url' => 'https://yoa.st/4eu', |
| 81 |
'premium_url' => 'https://yoa.st/4ex', |
| 82 |
'premium_upsell_url' => 'https://yoa.st/get-algolia-integration', |
| 83 |
'order' => 25, |
| 84 |
], |
| 85 |
]; |
| 86 |
|
| 87 |
/** |
| 88 |
* Filter to add integration toggles from add-ons. |
| 89 |
* |
| 90 |
* @param array $integration_toggles Array with integration toggle objects where each object |
| 91 |
* should have a `name`, `setting` and `label` property. |
| 92 |
*/ |
| 93 |
$integration_toggles = apply_filters( 'wpseo_integration_toggles', $integration_toggles ); |
| 94 |
|
| 95 |
$integration_toggles = array_map( [ $this, 'ensure_toggle' ], $integration_toggles ); |
| 96 |
usort( $integration_toggles, [ $this, 'sort_toggles_callback' ] ); |
| 97 |
|
| 98 |
return $integration_toggles; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Ensures that the passed value is a Yoast_Feature_Toggle. |
| 103 |
* |
| 104 |
* @param Yoast_Feature_Toggle|object|array $toggle_data Feature toggle instance, or raw object or array |
| 105 |
* containing integration toggle data. |
| 106 |
* @return Yoast_Feature_Toggle Feature toggle instance based on $toggle_data. |
| 107 |
*/ |
| 108 |
protected function ensure_toggle( $toggle_data ) { |
| 109 |
if ( $toggle_data instanceof Yoast_Feature_Toggle ) { |
| 110 |
return $toggle_data; |
| 111 |
} |
| 112 |
|
| 113 |
if ( is_object( $toggle_data ) ) { |
| 114 |
$toggle_data = get_object_vars( $toggle_data ); |
| 115 |
} |
| 116 |
|
| 117 |
return new Yoast_Feature_Toggle( $toggle_data ); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Callback for sorting integration toggles by their order. |
| 122 |
* |
| 123 |
* {@internal Once the minimum PHP version goes up to PHP 7.0, the logic in the function |
| 124 |
* can be replaced with the spaceship operator `<=>`.} |
| 125 |
* |
| 126 |
* @param Yoast_Feature_Toggle $feature_a Feature A. |
| 127 |
* @param Yoast_Feature_Toggle $feature_b Feature B. |
| 128 |
* |
| 129 |
* @return int An integer less than, equal to, or greater than zero indicating respectively |
| 130 |
* that feature A is considered to be less than, equal to, or greater than feature B. |
| 131 |
*/ |
| 132 |
protected function sort_toggles_callback( Yoast_Feature_Toggle $feature_a, Yoast_Feature_Toggle $feature_b ) { |
| 133 |
return ( $feature_a->order - $feature_b->order ); |
| 134 |
} |
| 135 |
} |
| 136 |
|