class-admin.php
2 weeks ago
class-columns-modal.php
2 weeks ago
class-columns.php
2 weeks ago
class-counter.php
2 weeks ago
class-crawler-detect.php
2 weeks ago
class-cron.php
2 weeks ago
class-dashboard.php
2 weeks ago
class-emails-mailer.php
2 weeks ago
class-emails-period.php
2 weeks ago
class-emails-query.php
2 weeks ago
class-emails-scheduler.php
2 weeks ago
class-emails-template.php
2 weeks ago
class-emails.php
2 weeks ago
class-frontend.php
2 weeks ago
class-functions.php
2 weeks ago
class-import.php
2 weeks ago
class-integration-gutenberg.php
2 weeks ago
class-integrations.php
2 weeks ago
class-query.php
2 weeks ago
class-settings-api.php
2 weeks ago
class-settings-display.php
2 weeks ago
class-settings-emails.php
2 weeks ago
class-settings-general.php
2 weeks ago
class-settings-integrations.php
2 weeks ago
class-settings-other.php
2 weeks ago
class-settings-reports.php
2 weeks ago
class-settings.php
2 weeks ago
class-toolbar.php
2 weeks ago
class-traffic-signals.php
2 weeks ago
class-update.php
2 weeks ago
class-widgets.php
2 weeks ago
functions.php
2 weeks ago
class-integrations.php
331 lines
| 1 | <?php |
| 2 | // exit if accessed directly |
| 3 | if ( ! defined( 'ABSPATH' ) ) |
| 4 | exit; |
| 5 | |
| 6 | /** |
| 7 | * Post_Views_Counter_Integrations class. |
| 8 | * |
| 9 | * Handles loading and management of integrations. |
| 10 | * |
| 11 | * @class Post_Views_Counter_Integrations |
| 12 | */ |
| 13 | class Post_Views_Counter_Integrations { |
| 14 | |
| 15 | /** |
| 16 | * Get all registered integration definitions (base + filtered). |
| 17 | * |
| 18 | * This is the canonical definition list used for save validation and UI rendering. |
| 19 | * Other plugins add integrations here via the pvc_integrations filter. |
| 20 | * |
| 21 | * @return array |
| 22 | */ |
| 23 | public static function get_registered_integrations() { |
| 24 | return apply_filters( 'pvc_integrations', self::get_base_integrations() ); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Get all integrations with their definitions and effective status. |
| 29 | * |
| 30 | * @return array |
| 31 | */ |
| 32 | public static function get_integrations() { |
| 33 | // get main instance |
| 34 | $pvc = Post_Views_Counter(); |
| 35 | |
| 36 | // get saved statuses |
| 37 | $saved_statuses = isset( $pvc->options['integrations']['integrations'] ) ? $pvc->options['integrations']['integrations'] : []; |
| 38 | |
| 39 | // get all registered integrations (base + any added via pvc_integrations filter) |
| 40 | $integrations = self::get_registered_integrations(); |
| 41 | |
| 42 | // compute effective status for each |
| 43 | foreach ( $integrations as $slug => &$integration ) { |
| 44 | $pro_active = ! ( isset( $integration['pro'] ) && $integration['pro'] ) || class_exists( 'Post_Views_Counter_Pro' ); |
| 45 | $available = self::is_integration_available( $integration ); |
| 46 | $saved_status = isset( $saved_statuses[$slug] ) ? (bool) $saved_statuses[$slug] : null; |
| 47 | |
| 48 | // default status: saved if set, otherwise true |
| 49 | $default_status = $saved_status === null ? true : $saved_status; |
| 50 | |
| 51 | // effective status: available and enabled_check result |
| 52 | $enabled_check = isset( $integration['enabled_check'] ) && is_callable( $integration['enabled_check'] ) |
| 53 | ? $integration['enabled_check'] |
| 54 | : function( $default_status, $integration, $slug, $saved_status ) { return $default_status; }; |
| 55 | |
| 56 | $integration['pro_active'] = $pro_active; |
| 57 | $integration['status'] = $available && $pro_active && call_user_func( $enabled_check, $default_status, $integration, $slug, $saved_status ); |
| 58 | $integration['availability'] = $available; |
| 59 | } |
| 60 | |
| 61 | return $integrations; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Check if a specific integration is effectively enabled. |
| 66 | * |
| 67 | * @param string $slug |
| 68 | * @return bool |
| 69 | */ |
| 70 | public static function is_integration_enabled( $slug ) { |
| 71 | $integrations = self::get_integrations(); |
| 72 | return isset( $integrations[$slug]['status'] ) ? $integrations[$slug]['status'] : false; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Get the status of a specific integration. |
| 77 | * |
| 78 | * @param string $slug |
| 79 | * @return bool |
| 80 | */ |
| 81 | public static function get_integration_status( $slug ) { |
| 82 | return self::is_integration_enabled( $slug ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Check if an integration is available based on its availability_check. |
| 87 | * |
| 88 | * @param array $integration |
| 89 | * @return bool |
| 90 | */ |
| 91 | private static function is_integration_available( $integration ) { |
| 92 | if ( ! isset( $integration['availability_check'] ) ) |
| 93 | return false; |
| 94 | |
| 95 | if ( is_callable( $integration['availability_check'] ) ) |
| 96 | return call_user_func( $integration['availability_check'] ); |
| 97 | |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Get base integrations definitions. |
| 103 | * |
| 104 | * @return array |
| 105 | */ |
| 106 | public static function get_base_integrations() { |
| 107 | return [ |
| 108 | 'gutenberg' => [ |
| 109 | 'name' => 'Gutenberg', |
| 110 | 'description' => __( 'Integrate with WordPress block editor to order posts by views in Query Loop and Latest Posts blocks.', 'post-views-counter' ), |
| 111 | 'menu_order' => 5, |
| 112 | 'pro' => false, |
| 113 | 'availability_check' => function() { return function_exists( 'register_block_type' ); }, |
| 114 | 'enabled_check' => function( $default_status, $integration, $slug, $saved_status ) { return $default_status; }, |
| 115 | 'items' => [ |
| 116 | [ |
| 117 | 'name' => __( 'Query Loop Block', 'post-views-counter' ), |
| 118 | 'description' => __( 'Enables ordering posts by view count in the core Query Loop block.', 'post-views-counter' ), |
| 119 | 'status' => true |
| 120 | ], |
| 121 | [ |
| 122 | 'name' => __( 'Latest Posts Block', 'post-views-counter' ), |
| 123 | 'description' => __( 'Enables ordering posts by view count in the core Latest Posts block.', 'post-views-counter' ), |
| 124 | 'status' => true |
| 125 | ] |
| 126 | ] |
| 127 | ], |
| 128 | 'amp' => [ |
| 129 | 'name' => 'AMP', |
| 130 | 'description' => __( 'Integrate with AMP plugin to handle post views on AMP pages.', 'post-views-counter' ), |
| 131 | 'menu_order' => 20, |
| 132 | 'pro' => true, |
| 133 | 'availability_check' => function() { return function_exists( 'amp_is_request' ); }, |
| 134 | 'enabled_check' => function( $default_status, $integration, $slug, $saved_status ) { return $default_status; }, |
| 135 | 'items' => [ |
| 136 | [ |
| 137 | 'name' => __( 'AMP Support', 'post-views-counter' ), |
| 138 | 'description' => __( 'Tracks and displays views on AMP-enabled pages.', 'post-views-counter' ), |
| 139 | 'status' => true |
| 140 | ] |
| 141 | ] |
| 142 | ], |
| 143 | 'beaver-builder' => [ |
| 144 | 'name' => 'Beaver Builder', |
| 145 | 'description' => __( 'Integrate with Beaver Builder to order module posts by views.', 'post-views-counter' ), |
| 146 | 'menu_order' => 20, |
| 147 | 'pro' => true, |
| 148 | 'availability_check' => function() { return class_exists( 'FLBuilder' ); }, |
| 149 | 'enabled_check' => function( $default_status, $integration, $slug, $saved_status ) { return $default_status; }, |
| 150 | 'items' => [ |
| 151 | [ |
| 152 | 'name' => __( 'Post Grid Module', 'post-views-counter' ), |
| 153 | 'description' => __( 'Orders posts by views in the Beaver Builder Post Grid module.', 'post-views-counter' ), |
| 154 | 'status' => true |
| 155 | ], |
| 156 | [ |
| 157 | 'name' => __( 'Post Carousel Module', 'post-views-counter' ), |
| 158 | 'description' => __( 'Orders posts by views in the Beaver Builder Post Carousel module.', 'post-views-counter' ), |
| 159 | 'status' => true |
| 160 | ], |
| 161 | [ |
| 162 | 'name' => __( 'Post Slider Module', 'post-views-counter' ), |
| 163 | 'description' => __( 'Orders posts by views in the Beaver Builder Post Slider module.', 'post-views-counter' ), |
| 164 | 'status' => true |
| 165 | ] |
| 166 | ] |
| 167 | ], |
| 168 | 'coblocks' => [ |
| 169 | 'name' => 'CoBlocks', |
| 170 | 'description' => __( 'Integrate with CoBlocks to order post feed results by views.', 'post-views-counter' ), |
| 171 | 'menu_order' => 20, |
| 172 | 'pro' => true, |
| 173 | 'availability_check' => function() { return defined( 'COBLOCKS_VERSION' ); }, |
| 174 | 'enabled_check' => function( $default_status, $integration, $slug, $saved_status ) { return $default_status; }, |
| 175 | 'items' => [ |
| 176 | [ |
| 177 | 'name' => __( 'Posts Block', 'post-views-counter' ), |
| 178 | 'description' => __( 'Adds post views ordering to the CoBlocks Posts block.', 'post-views-counter' ), |
| 179 | 'status' => true |
| 180 | ], |
| 181 | [ |
| 182 | 'name' => __( 'Post Carousel Block', 'post-views-counter' ), |
| 183 | 'description' => __( 'Adds post views ordering to the CoBlocks Post Carousel block.', 'post-views-counter' ), |
| 184 | 'status' => true |
| 185 | ] |
| 186 | ] |
| 187 | ], |
| 188 | 'divi' => [ |
| 189 | 'name' => 'Divi', |
| 190 | 'description' => __( 'Integrate with Divi Theme to order module posts by views when the module uses the "orderby-post-views" CSS class.', 'post-views-counter' ), |
| 191 | 'menu_order' => 20, |
| 192 | 'pro' => true, |
| 193 | 'availability_check' => function() { return defined( 'ET_CORE_VERSION' ); }, |
| 194 | 'enabled_check' => function( $default_status, $integration, $slug, $saved_status ) { return $default_status; }, |
| 195 | 'items' => [ |
| 196 | [ |
| 197 | 'name' => __( 'Blog Module', 'post-views-counter' ), |
| 198 | 'description' => __( 'Orders posts by views in the Divi Blog module.', 'post-views-counter' ), |
| 199 | 'status' => true |
| 200 | ], |
| 201 | [ |
| 202 | 'name' => __( 'Post Slider Module', 'post-views-counter' ), |
| 203 | 'description' => __( 'Orders posts by views in the Divi Post Slider module.', 'post-views-counter' ), |
| 204 | 'status' => true |
| 205 | ], |
| 206 | [ |
| 207 | 'name' => __( 'Portfolio Module', 'post-views-counter' ), |
| 208 | 'description' => __( 'Orders posts by views in the Divi Portfolio module.', 'post-views-counter' ), |
| 209 | 'status' => true |
| 210 | ] |
| 211 | ] |
| 212 | ], |
| 213 | 'elementor-pro' => [ |
| 214 | 'name' => 'Elementor Pro', |
| 215 | 'description' => __( 'Integrate with Elementor Pro to order posts by views when a widget query uses the "post_views" Query ID.', 'post-views-counter' ), |
| 216 | 'menu_order' => 20, |
| 217 | 'pro' => true, |
| 218 | 'availability_check' => function() { return defined( 'ELEMENTOR_PRO_VERSION' ); }, |
| 219 | 'enabled_check' => function( $default_status, $integration, $slug, $saved_status ) { return $default_status; }, |
| 220 | 'items' => [ |
| 221 | [ |
| 222 | 'name' => __( 'Posts Widget', 'post-views-counter' ), |
| 223 | 'description' => __( 'Orders posts by views in the Elementor Pro Posts widget when its Query ID is set to "post_views".', 'post-views-counter' ), |
| 224 | 'status' => true |
| 225 | ], |
| 226 | [ |
| 227 | 'name' => __( 'Portfolio Widget', 'post-views-counter' ), |
| 228 | 'description' => __( 'Orders items by views in the Elementor Pro Portfolio widget when its Query ID is set to "post_views".', 'post-views-counter' ), |
| 229 | 'status' => true |
| 230 | ], |
| 231 | [ |
| 232 | 'name' => __( 'Loop Grid / Loop Carousel', 'post-views-counter' ), |
| 233 | 'description' => __( 'Orders items by views in the Elementor Pro Loop Grid and Loop Carousel widgets when the Query ID is set to "post_views".', 'post-views-counter' ), |
| 234 | 'status' => true |
| 235 | ] |
| 236 | ] |
| 237 | ], |
| 238 | 'generateblocks' => [ |
| 239 | 'name' => 'GenerateBlocks', |
| 240 | 'description' => __( 'Integrate with GenerateBlocks to order Query block results by views.', 'post-views-counter' ), |
| 241 | 'menu_order' => 20, |
| 242 | 'pro' => true, |
| 243 | 'availability_check' => function() { return defined( 'GENERATEBLOCKS_VERSION' ); }, |
| 244 | 'enabled_check' => function( $default_status, $integration, $slug, $saved_status ) { return $default_status; }, |
| 245 | 'items' => [ |
| 246 | [ |
| 247 | 'name' => __( 'Dynamic Content', 'post-views-counter' ), |
| 248 | 'description' => __( 'Adds post_views ordering to the GenerateBlocks Query block.', 'post-views-counter' ), |
| 249 | 'status' => true |
| 250 | ] |
| 251 | ] |
| 252 | ], |
| 253 | 'jet-engine' => [ |
| 254 | 'name' => 'JetEngine', |
| 255 | 'description' => __( 'Integrate with JetEngine plugin to display post views in custom listings and queries.', 'post-views-counter' ), |
| 256 | 'menu_order' => 20, |
| 257 | 'pro' => true, |
| 258 | 'availability_check' => function() { return class_exists( 'Jet_Engine' ); }, |
| 259 | 'enabled_check' => function( $default_status, $integration, $slug, $saved_status ) { return $default_status; }, |
| 260 | 'items' => [ |
| 261 | [ |
| 262 | 'name' => __( 'Query Builder', 'post-views-counter' ), |
| 263 | 'description' => __( 'Enables ordering posts by view count in Query Builder posts queries.', 'post-views-counter' ), |
| 264 | 'status' => true |
| 265 | ], |
| 266 | [ |
| 267 | 'name' => __( 'Listing Grid', 'post-views-counter' ), |
| 268 | 'description' => __( 'Enables ordering posts by view count in Listing Grid, Maps Listing and Calendar widgets.', 'post-views-counter' ), |
| 269 | 'status' => true |
| 270 | ] |
| 271 | ] |
| 272 | ], |
| 273 | 'kadence-blocks' => [ |
| 274 | 'name' => 'Kadence Blocks', |
| 275 | 'description' => __( 'Integrate with Kadence Blocks to order post results by views in the Posts block.', 'post-views-counter' ), |
| 276 | 'menu_order' => 20, |
| 277 | 'pro' => true, |
| 278 | 'availability_check' => function() { return defined( 'KADENCE_BLOCKS_VERSION' ); }, |
| 279 | 'enabled_check' => function( $default_status, $integration, $slug, $saved_status ) { return $default_status; }, |
| 280 | 'items' => [ |
| 281 | [ |
| 282 | 'name' => __( 'Posts Block', 'post-views-counter' ), |
| 283 | 'description' => __( 'Adds post views ordering to the Posts block.', 'post-views-counter' ), |
| 284 | 'status' => true |
| 285 | ] |
| 286 | ] |
| 287 | ], |
| 288 | 'polylang' => [ |
| 289 | 'name' => 'Polylang', |
| 290 | 'description' => __( 'Integrate with Polylang to filter reports, exports, and dashboard widgets by language.', 'post-views-counter' ), |
| 291 | 'menu_order' => 20, |
| 292 | 'pro' => true, |
| 293 | 'availability_check' => function() { return function_exists( 'pll_languages_list' ) || class_exists( 'Polylang' ); }, |
| 294 | 'enabled_check' => function( $default_status, $integration, $slug, $saved_status ) { return $default_status; }, |
| 295 | 'items' => [ |
| 296 | [ |
| 297 | 'name' => __( 'Reports Language Filters', 'post-views-counter' ), |
| 298 | 'description' => __( 'Adds language filtering and a language column to Reports tables, charts, and exports.', 'post-views-counter' ), |
| 299 | 'status' => true |
| 300 | ], |
| 301 | [ |
| 302 | 'name' => __( 'Dashboard Widgets', 'post-views-counter' ), |
| 303 | 'description' => __( 'Filters dashboard widgets (site, post, and term views) by the selected language.', 'post-views-counter' ), |
| 304 | 'status' => true |
| 305 | ] |
| 306 | ] |
| 307 | ], |
| 308 | 'wpml' => [ |
| 309 | 'name' => 'WPML', |
| 310 | 'description' => __( 'Integrate with WPML to filter reports, exports, and dashboard widgets by language.', 'post-views-counter' ), |
| 311 | 'menu_order' => 20, |
| 312 | 'pro' => true, |
| 313 | 'availability_check' => function() { return defined( 'WPML_PLUGIN_FILE' ) || class_exists( 'SitePress' ); }, |
| 314 | 'enabled_check' => function( $default_status, $integration, $slug, $saved_status ) { return $default_status; }, |
| 315 | 'items' => [ |
| 316 | [ |
| 317 | 'name' => __( 'Reports Language Filters', 'post-views-counter' ), |
| 318 | 'description' => __( 'Adds language filtering and a language column to Reports tables, charts, and exports.', 'post-views-counter' ), |
| 319 | 'status' => true |
| 320 | ], |
| 321 | [ |
| 322 | 'name' => __( 'Dashboard Widgets', 'post-views-counter' ), |
| 323 | 'description' => __( 'Filters dashboard widgets (site, post, and term views) by the selected language.', 'post-views-counter' ), |
| 324 | 'status' => true |
| 325 | ] |
| 326 | ] |
| 327 | ] |
| 328 | ]; |
| 329 | } |
| 330 | } |
| 331 |