class-advanced-ads-admin.php
652 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Advanced Ads main admin class |
| 4 | * |
| 5 | * @package Advanced_Ads_Admin |
| 6 | * @author Thomas Maier <thomas.maier@webgilde.com> |
| 7 | * @license GPL-2.0+ |
| 8 | * @link https://wpadvancedads.com |
| 9 | * @copyright since 2013 Thomas Maier, webgilde GmbH |
| 10 | * |
| 11 | * Plugin class. This class should ideally be used to work with the |
| 12 | * administrative side of the WordPress site. |
| 13 | */ |
| 14 | |
| 15 | class Advanced_Ads_Admin { |
| 16 | |
| 17 | /** |
| 18 | * Instance of this class. |
| 19 | * |
| 20 | * @since 1.0.0 |
| 21 | * @var object |
| 22 | */ |
| 23 | protected static $instance = null; |
| 24 | |
| 25 | /** |
| 26 | * Instance of admin notice class. |
| 27 | * |
| 28 | * @since 1.5.2 |
| 29 | * @var object |
| 30 | */ |
| 31 | protected $notices = null; |
| 32 | |
| 33 | /** |
| 34 | * Slug of the settings page |
| 35 | * |
| 36 | * @since 1.0.0 |
| 37 | * @var string |
| 38 | */ |
| 39 | public $plugin_screen_hook_suffix = null; |
| 40 | |
| 41 | /** |
| 42 | * General plugin slug |
| 43 | * |
| 44 | * @since 1.0.0 |
| 45 | * @var string |
| 46 | */ |
| 47 | protected $plugin_slug = ''; |
| 48 | |
| 49 | /** |
| 50 | * Initialize the plugin by loading admin scripts & styles and adding a |
| 51 | * settings page and menu. |
| 52 | * |
| 53 | * @since 1.0.0 |
| 54 | */ |
| 55 | private function __construct() { |
| 56 | if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
| 57 | new Advanced_Ads_Ad_Ajax_Callbacks(); |
| 58 | add_action( 'plugins_loaded', array( $this, 'wp_plugins_loaded_ajax' ) ); |
| 59 | } else { |
| 60 | add_action( 'plugins_loaded', array( $this, 'wp_plugins_loaded' ) ); |
| 61 | add_filter( 'admin_footer_text', array( $this, 'admin_footer_text' ), 100 ); |
| 62 | Advanced_Ads_Ad_List_Filters::get_instance(); |
| 63 | } |
| 64 | // add shortcode creator to TinyMCE. |
| 65 | Advanced_Ads_Shortcode_Creator::get_instance(); |
| 66 | Advanced_Ads_Admin_Licenses::get_instance(); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * License handling legacy code after moving license handling code to Advanced_Ads_Admin_Licenses |
| 71 | * |
| 72 | * @param string $addon slug of the add-on. |
| 73 | * @param string $plugin_name name of the add-on. |
| 74 | * @param string $options_slug slug of the options the plugin is saving in the options table. |
| 75 | * @since version 1.7.16 (early January 2017) |
| 76 | */ |
| 77 | public function deactivate_license( $addon = '', $plugin_name = '', $options_slug = '' ) { |
| 78 | return Advanced_Ads_Admin_Licenses::get_instance()->deactivate_license( $addon, $plugin_name, $options_slug ); } |
| 79 | |
| 80 | /** |
| 81 | * Get license status. |
| 82 | * |
| 83 | * @param string $slug slug of the add-on. |
| 84 | * @return string license status |
| 85 | */ |
| 86 | public function get_license_status( $slug = '' ) { |
| 87 | return Advanced_Ads_Admin_Licenses::get_instance()->get_license_status( $slug ); } |
| 88 | |
| 89 | /** |
| 90 | * Actions and filter available after all plugins are initialized. |
| 91 | */ |
| 92 | public function wp_plugins_loaded() { |
| 93 | /* |
| 94 | * Call $plugin_slug from public plugin class. |
| 95 | * |
| 96 | */ |
| 97 | $plugin = Advanced_Ads::get_instance(); |
| 98 | $this->plugin_slug = $plugin->get_plugin_slug(); |
| 99 | |
| 100 | add_action( 'current_screen', array( $this, 'current_screen' ) ); |
| 101 | |
| 102 | // Load admin style sheet and JavaScript. |
| 103 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ) ); |
| 104 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ), 9 ); |
| 105 | |
| 106 | // update placements. |
| 107 | add_action( 'admin_init', array( 'Advanced_Ads_Placements', 'update_placements' ) ); |
| 108 | |
| 109 | // check for update logic. |
| 110 | add_action( 'admin_notices', array( $this, 'admin_notices' ) ); |
| 111 | |
| 112 | // add links to plugin page. |
| 113 | add_filter( 'plugin_action_links_' . ADVADS_BASE, array( $this, 'add_plugin_links' ) ); |
| 114 | |
| 115 | // display information when user is going to disable the plugin. |
| 116 | add_filter( 'admin_footer', array( $this, 'add_deactivation_logic' ) ); |
| 117 | // add_filter( 'after_plugin_row_' . ADVADS_BASE, array( $this, 'display_deactivation_message' ) ); |
| 118 | // disable adding rel="noopener noreferrer" to link added through TinyMCE for rich content ads. |
| 119 | add_filter( 'tiny_mce_before_init', array( $this, 'tinymce_allow_unsafe_link_target' ) ); |
| 120 | |
| 121 | add_action( 'plugins_api_result', array( $this, 'recommend_suitable_add_ons' ), 11, 3 ); |
| 122 | |
| 123 | Advanced_Ads_Admin_Meta_Boxes::get_instance(); |
| 124 | Advanced_Ads_Admin_Menu::get_instance(); |
| 125 | Advanced_Ads_Admin_Ad_Type::get_instance(); |
| 126 | Advanced_Ads_Admin_Settings::get_instance(); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Actions and filters that should also be available for ajax |
| 131 | */ |
| 132 | public function wp_plugins_loaded_ajax() { |
| 133 | // needed here in order to work with Quick Edit option on ad list page. |
| 134 | Advanced_Ads_Admin_Ad_Type::get_instance(); |
| 135 | |
| 136 | add_action( 'wp_ajax_advads_send_feedback', array( $this, 'send_feedback' ) ); |
| 137 | add_action( 'wp_ajax_advads_load_rss_widget_content', array( 'Advanced_Ads_Admin_Meta_Boxes', 'dashboard_widget_function_output' ) ); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Return an instance of this class. |
| 142 | * |
| 143 | * @since 1.0.0 |
| 144 | * |
| 145 | * @return object A single instance of this class. |
| 146 | */ |
| 147 | public static function get_instance() { |
| 148 | // If the single instance hasn't been set, set it now. |
| 149 | if ( null === self::$instance ) { |
| 150 | self::$instance = new self(); |
| 151 | } |
| 152 | |
| 153 | return self::$instance; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * General stuff after page is loaded and screen variable is available |
| 158 | */ |
| 159 | public function current_screen() { |
| 160 | $screen = get_current_screen(); |
| 161 | |
| 162 | if ( ! isset( $screen->id ) ) { |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | switch ( $screen->id ) { |
| 167 | case 'edit-advanced_ads': // ad overview page. |
| 168 | case 'advanced_ads': // ad edit page. |
| 169 | // remove notice about missing first ad. |
| 170 | break; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Register and enqueue admin-specific style sheet. |
| 176 | * |
| 177 | * @since 1.0.0 |
| 178 | */ |
| 179 | public function enqueue_admin_styles() { |
| 180 | wp_enqueue_style( $this->plugin_slug . '-admin-styles', plugins_url( 'assets/css/admin.css', __FILE__ ), array(), ADVADS_VERSION ); |
| 181 | if ( self::screen_belongs_to_advanced_ads() ) { |
| 182 | // jQuery ui smoothness style 1.11.4. |
| 183 | wp_enqueue_style( $this->plugin_slug . '-jquery-ui-styles', plugins_url( 'assets/jquery-ui/jquery-ui.min.css', __FILE__ ), array(), '1.11.4' ); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Register and enqueue admin-specific JavaScript. |
| 189 | * |
| 190 | * @since 1.0.0 |
| 191 | */ |
| 192 | public function enqueue_admin_scripts() { |
| 193 | |
| 194 | // global js script. |
| 195 | wp_enqueue_script( $this->plugin_slug . '-admin-global-script', plugins_url( 'assets/js/admin-global.js', __FILE__ ), array( 'jquery' ), ADVADS_VERSION, false ); |
| 196 | wp_enqueue_script( $this->plugin_slug . '-admin-find-adblocker', plugins_url( 'assets/js/advertisement.js', __FILE__ ), array(), ADVADS_VERSION, false ); |
| 197 | |
| 198 | // register ajax nonce. |
| 199 | $params = array( |
| 200 | 'ajax_nonce' => wp_create_nonce( 'advanced-ads-admin-ajax-nonce' ), |
| 201 | ); |
| 202 | wp_localize_script( $this->plugin_slug . '-admin-global-script', 'advadsglobal', $params ); |
| 203 | |
| 204 | if ( self::screen_belongs_to_advanced_ads() ) { |
| 205 | wp_register_script( $this->plugin_slug . '-admin-script', plugins_url( 'assets/js/admin.js', __FILE__ ), array( 'jquery', 'jquery-ui-autocomplete', 'jquery-ui-button' ), ADVADS_VERSION, false ); |
| 206 | wp_register_script( $this->plugin_slug . '-wizard-script', plugins_url( 'assets/js/wizard.js', __FILE__ ), array( 'jquery' ), ADVADS_VERSION, false ); |
| 207 | |
| 208 | // jquery ui. |
| 209 | wp_enqueue_script( 'jquery-ui-accordion' ); |
| 210 | wp_enqueue_script( 'jquery-ui-button' ); |
| 211 | wp_enqueue_script( 'jquery-ui-tooltip' ); |
| 212 | |
| 213 | // just register this script for later inclusion on ad group list page. |
| 214 | wp_register_script( 'inline-edit-group-ads', plugins_url( 'assets/js/inline-edit-group-ads.js', __FILE__ ), array( 'jquery' ), ADVADS_VERSION, false ); |
| 215 | |
| 216 | $auto_ads_strings = Advanced_Ads_AdSense_Admin::get_auto_ads_messages(); |
| 217 | |
| 218 | // register admin.js translations. |
| 219 | $translation_array = array( |
| 220 | 'condition_or' => __( 'or', 'advanced-ads' ), |
| 221 | 'condition_and' => __( 'and', 'advanced-ads' ), |
| 222 | 'after_paragraph_promt' => __( 'After which paragraph?', 'advanced-ads' ), |
| 223 | 'page_level_ads_enabled' => $auto_ads_strings['enabled'], |
| 224 | ); |
| 225 | |
| 226 | wp_localize_script( $this->plugin_slug . '-admin-script', 'advadstxt', $translation_array ); |
| 227 | |
| 228 | wp_enqueue_script( $this->plugin_slug . '-admin-script' ); |
| 229 | wp_enqueue_script( $this->plugin_slug . '-wizard-script' ); |
| 230 | } |
| 231 | |
| 232 | // call media manager for image upload only on ad edit pages. |
| 233 | $screen = get_current_screen(); |
| 234 | if ( isset( $screen->id ) && Advanced_Ads::POST_TYPE_SLUG === $screen->id ) { |
| 235 | // the 'wp_enqueue_media' function can be executed only once and should be called with the 'post' parameter |
| 236 | // in this case, the '_wpMediaViewsL10n' js object inside html will contain id of the post, that is necessary to view oEmbed priview inside tinyMCE editor. |
| 237 | // since other plugins can call the 'wp_enqueue_media' function without the 'post' parameter, Advanced Ads should call it earlier. |
| 238 | global $post; |
| 239 | wp_enqueue_media( array( 'post' => $post ) ); |
| 240 | } |
| 241 | |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Check if the current screen belongs to Advanced Ads |
| 246 | * |
| 247 | * @since 1.6.6 |
| 248 | * @return bool true if screen belongs to Advanced Ads |
| 249 | */ |
| 250 | public static function screen_belongs_to_advanced_ads() { |
| 251 | |
| 252 | if ( ! function_exists( 'get_current_screen' ) ) { |
| 253 | return false; |
| 254 | } |
| 255 | |
| 256 | $screen = get_current_screen(); |
| 257 | if ( ! isset( $screen->id ) ) { |
| 258 | return false; |
| 259 | } |
| 260 | |
| 261 | $advads_pages = apply_filters( |
| 262 | 'advanced-ads-dashboard-screens', array( |
| 263 | 'advanced-ads_page_advanced-ads-groups', // ad groups. |
| 264 | 'edit-advanced_ads', // ads overview. |
| 265 | 'advanced_ads', // ad edit page. |
| 266 | 'advanced-ads_page_advanced-ads-placements', // placements. |
| 267 | 'advanced-ads_page_advanced-ads-settings', // settings. |
| 268 | 'toplevel_page_advanced-ads', // overview. |
| 269 | 'admin_page_advanced-ads-debug', // debug. |
| 270 | // 'advanced-ads_page_advanced-ads-support', // support. |
| 271 | 'admin_page_advanced-ads-import-export', // import & export. |
| 272 | ) |
| 273 | ); |
| 274 | |
| 275 | if ( in_array( $screen->id, $advads_pages, true ) ) { |
| 276 | return true; |
| 277 | } |
| 278 | |
| 279 | return false; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Get action from the params |
| 284 | * |
| 285 | * @since 1.0.0 |
| 286 | */ |
| 287 | public function current_action() { |
| 288 | $request = wp_unslash( $_REQUEST ); |
| 289 | if ( isset( $request['action'] ) && -1 !== $request['action'] ) { |
| 290 | return $request['action']; |
| 291 | } |
| 292 | |
| 293 | return false; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Get DateTimeZone object for the WP installation |
| 298 | */ |
| 299 | public static function get_wp_timezone() { |
| 300 | $_time_zone = get_option( 'timezone_string' ); |
| 301 | $time_zone = new DateTimeZone( 'UTC' ); |
| 302 | if ( $_time_zone ) { |
| 303 | $time_zone = new DateTimeZone( $_time_zone ); |
| 304 | } else { |
| 305 | $gmt_offset = floatval( get_option( 'gmt_offset' ) ); |
| 306 | $sign = ( 0 > $gmt_offset ) ? '-' : '+'; |
| 307 | $int = floor( abs( $gmt_offset ) ); |
| 308 | $frac = abs( $gmt_offset ) - $int; |
| 309 | |
| 310 | $gmt = ''; |
| 311 | if ( $gmt_offset ) { |
| 312 | $gmt .= $sign . zeroise( $int, 2 ) . ':' . zeroise( 60 * $frac, 2 ); |
| 313 | $time_zone = date_create( '2017-10-01T12:00:00' . $gmt )->getTimezone(); |
| 314 | } |
| 315 | } |
| 316 | return $time_zone; |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Get literal expression of timezone |
| 321 | * |
| 322 | * @param DateTimeZone $date_time_zone the DateTimeZone object to get literal value from. |
| 323 | */ |
| 324 | public static function timezone_get_name( $date_time_zone ) { |
| 325 | if ( $date_time_zone instanceof DateTimeZone ) { |
| 326 | $time_zone = timezone_name_get( $date_time_zone ); |
| 327 | if ( 'UTC' === $time_zone ) { |
| 328 | return 'UTC+0'; |
| 329 | } |
| 330 | if ( false === strpos( $time_zone, '/' ) ) { |
| 331 | $time_zone = 'UTC' . $time_zone; |
| 332 | } else { |
| 333 | // translators: time zone name. |
| 334 | $time_zone = sprintf( __( 'time of %s', 'advanced-ads' ), $time_zone ); |
| 335 | } |
| 336 | return $time_zone; |
| 337 | } |
| 338 | return 'UTC+0'; |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Initiate the admin notices class |
| 343 | * |
| 344 | * @since 1.5.3 |
| 345 | */ |
| 346 | public function admin_notices() { |
| 347 | // display ad block warning to everyone who can edit ads. |
| 348 | if ( current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads' ) ) ) { |
| 349 | if ( $this->screen_belongs_to_advanced_ads() ) { |
| 350 | include ADVADS_BASE_PATH . 'admin/views/notices/adblock.php'; |
| 351 | include ADVADS_BASE_PATH . 'admin/views/notices/jqueryui_error.php'; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | if ( current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads' ) ) ) { |
| 356 | $this->notices = Advanced_Ads_Admin_Notices::get_instance()->notices; |
| 357 | Advanced_Ads_Admin_Notices::get_instance()->display_notices(); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Add links to the plugins list |
| 363 | * |
| 364 | * @since 1.6.14 |
| 365 | * @param arr $links array of links for the plugins, adapted when the current plugin is found. |
| 366 | * @return array $links |
| 367 | */ |
| 368 | public function add_plugin_links( $links ) { |
| 369 | |
| 370 | if ( ! is_array( $links ) ) { |
| 371 | return $links; |
| 372 | } |
| 373 | |
| 374 | // add link to support page. |
| 375 | $support_link = '<a href="' . esc_url( admin_url( 'admin.php?page=advanced-ads-settings#top#support' ) ) . '">' . __( 'Support', 'advanced-ads' ) . '</a>'; |
| 376 | array_unshift( $links, $support_link ); |
| 377 | |
| 378 | // add link to add-ons. |
| 379 | $extend_link = '<a href="' . ADVADS_URL . 'add-ons/#utm_source=advanced-ads&utm_medium=link&utm_campaign=plugin-page" target="_blank">' . __( 'Add-Ons', 'advanced-ads' ) . '</a>'; |
| 380 | array_unshift( $links, $extend_link ); |
| 381 | |
| 382 | return $links; |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * Display deactivation logic on plugins page |
| 387 | * |
| 388 | * @since 1.7.14 |
| 389 | */ |
| 390 | public function add_deactivation_logic() { |
| 391 | |
| 392 | $screen = get_current_screen(); |
| 393 | if ( ! isset( $screen->id ) || ! in_array( $screen->id, array( 'plugins', 'plugins-network' ), true ) ) { |
| 394 | return; |
| 395 | } |
| 396 | |
| 397 | $current_user = wp_get_current_user(); |
| 398 | if ( ! ( $current_user instanceof WP_User ) ) { |
| 399 | $from = ''; |
| 400 | $email = ''; |
| 401 | } else { |
| 402 | $from = $current_user->user_nicename . ' <' . trim( $current_user->user_email ) . '>'; |
| 403 | $email = $current_user->user_email; |
| 404 | } |
| 405 | |
| 406 | include ADVADS_BASE_PATH . 'admin/views/feedback-disable.php'; |
| 407 | } |
| 408 | |
| 409 | /** |
| 410 | * Send feedback via email |
| 411 | * |
| 412 | * @since 1.7.14 |
| 413 | */ |
| 414 | public function send_feedback() { |
| 415 | /** |
| 416 | * We first need to get the form data from the string and can verify the nonce afterwards |
| 417 | * This throws an issue with the WP Coding Standards though |
| 418 | */ |
| 419 | if ( isset( $_POST['formdata'] ) ) { |
| 420 | parse_str( wp_unslash( $_POST['formdata'] ), $form ); |
| 421 | } |
| 422 | |
| 423 | if ( ! wp_verify_nonce( $form['advanced_ads_disable_form_nonce'], 'advanced_ads_disable_form' ) ) { |
| 424 | die(); |
| 425 | } |
| 426 | |
| 427 | $text = ''; |
| 428 | if ( isset( $form['advanced_ads_disable_text'] ) ) { |
| 429 | $text = implode( "\n\r", $form['advanced_ads_disable_text'] ); |
| 430 | } |
| 431 | |
| 432 | // get first version to see if this is a new problem or might be an older on. |
| 433 | $options = Advanced_Ads_Plugin::get_instance()->internal_options(); |
| 434 | $installed = isset( $options['installed'] ) ? date( 'd.m.Y', $options['installed'] ) : '–'; |
| 435 | |
| 436 | $text .= "\n\n" . home_url() . " ($installed)"; |
| 437 | |
| 438 | $headers = array(); |
| 439 | |
| 440 | $from = isset( $form['advanced_ads_disable_from'] ) ? $form['advanced_ads_disable_from'] : ''; |
| 441 | // the user clicked on the "don’t disable" button or if an address is given in the form then use that one. |
| 442 | if ( isset( $form['advanced_ads_disable_reason'] ) |
| 443 | && 'get help' === $form['advanced_ads_disable_reason'] |
| 444 | && ! empty( $form['advanced_ads_disable_reply_email'] ) ) { |
| 445 | $email = isset( $form['advanced_ads_disable_reply_email'] ) ? trim( $form['advanced_ads_disable_reply_email'] ) : $current_user->email; |
| 446 | $current_user = wp_get_current_user(); |
| 447 | $name = ( $current_user instanceof WP_User ) ? $current_user->user_nicename : ''; |
| 448 | $from = $name . ' <' . $email . '>'; |
| 449 | $is_german = ( preg_match( '/\.de$/', $from ) || "de_" === substr( get_locale(), 0, 3 ) || "de_" === substr( get_user_locale(), 0, 3 ) ); |
| 450 | if ( isset( $form['advanced_ads_disable_text'][0] ) |
| 451 | && trim( $form['advanced_ads_disable_text'][0] ) !== '' ) { // is a text given then ask for help. |
| 452 | // send German text |
| 453 | if( $is_german ){ |
| 454 | $text .= "\n\n Hilfe ist auf dem Weg."; |
| 455 | } else { |
| 456 | $text .= "\n\n Help is on its way."; |
| 457 | } |
| 458 | } else { // if no text is given, just reply. |
| 459 | if( $is_german ){ |
| 460 | $text .= "\n\n Vielen Dank für das Feedback."; |
| 461 | } else { |
| 462 | $text .= "\n\n Thank you for your feedback."; |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | if ( $from ) { |
| 467 | $headers[] = "From: $from"; |
| 468 | $headers[] = "Reply-To: $from"; |
| 469 | } |
| 470 | |
| 471 | $subject = isset( $form['advanced_ads_disable_reason'] ) ? $form['advanced_ads_disable_reason'] : '(no reason given)'; |
| 472 | // append plugin name to get a better subject. |
| 473 | $subject .= ' (Advanced Ads)'; |
| 474 | |
| 475 | $success = wp_mail( 'improve@wpadvancedads.com', $subject, $text, $headers ); |
| 476 | |
| 477 | die(); |
| 478 | |
| 479 | } |
| 480 | |
| 481 | /** |
| 482 | * Configure TinyMCE to allow unsafe link target. |
| 483 | * |
| 484 | * @param boolean $mce_init the tinyMce initialization array. |
| 485 | * @return boolean |
| 486 | */ |
| 487 | public function tinymce_allow_unsafe_link_target( $mce_init ) { |
| 488 | |
| 489 | // check if we are on the ad edit screen. |
| 490 | if ( ! function_exists( 'get_current_screen' ) ) { |
| 491 | return $mce_init; |
| 492 | } |
| 493 | |
| 494 | $screen = get_current_screen(); |
| 495 | if ( isset( $screen->id ) && 'advanced_ads' === $screen->id ) { |
| 496 | $mce_init['allow_unsafe_link_target'] = true; |
| 497 | } |
| 498 | |
| 499 | return $mce_init; |
| 500 | } |
| 501 | |
| 502 | /** |
| 503 | * Sort visitor and display condition arrays alphabetically by their label. |
| 504 | * |
| 505 | * @param array $a array to be compared. |
| 506 | * @param array $b array to be compared. |
| 507 | * @since 1.8.12 |
| 508 | */ |
| 509 | public static function sort_condition_array_by_label( $a, $b ) { |
| 510 | if ( ! isset( $a['label'] ) || ! isset( $b['label'] ) ) { |
| 511 | return; |
| 512 | } |
| 513 | return strcmp( strtolower( $a['label'] ), strtolower( $b['label'] ) ); |
| 514 | } |
| 515 | |
| 516 | /** |
| 517 | * Recommend additional add-ons |
| 518 | * |
| 519 | * @param object $result original result object. |
| 520 | * @param unknown $action action. |
| 521 | * @param object $args additional arguments. |
| 522 | */ |
| 523 | public function recommend_suitable_add_ons( $result, $action, $args ) { |
| 524 | if ( empty( $args->browse ) ) { |
| 525 | return $result; |
| 526 | } |
| 527 | |
| 528 | if ( 'featured' !== $args->browse && 'recommended' !== $args->browse && 'popular' !== $args->browse ) { |
| 529 | return $result; |
| 530 | } |
| 531 | |
| 532 | if ( ! isset( $result->info['page'] ) || 1 < $result->info['page'] ) { |
| 533 | return $result; |
| 534 | } |
| 535 | |
| 536 | // Recommend AdSense In-Feed add-on. |
| 537 | if ( ! is_plugin_active( 'advanced-ads-adsense-in-feed/advanced-ads-in-feed.php' ) |
| 538 | && ! is_plugin_active_for_network( 'advanced-ads-adsense-in-feed/advanced-ads-in-feed.php' ) ) { |
| 539 | |
| 540 | // Grab all slugs from the api results. |
| 541 | $result_slugs = wp_list_pluck( $result->plugins, 'slug' ); |
| 542 | |
| 543 | if ( in_array( 'advanced-ads-adsense-in-feed', $result_slugs, true ) ) { |
| 544 | return $result; |
| 545 | } |
| 546 | |
| 547 | $query_args = array( |
| 548 | 'slug' => 'advanced-ads-adsense-in-feed', |
| 549 | 'fields' => array( |
| 550 | 'icons' => true, |
| 551 | 'active_installs' => true, |
| 552 | 'short_description' => true, |
| 553 | 'group' => true, |
| 554 | ), |
| 555 | ); |
| 556 | $plugin_data = plugins_api( 'plugin_information', $query_args ); |
| 557 | |
| 558 | if ( ! is_wp_error( $plugin_data ) ) { |
| 559 | if ( 'featured' === $args->browse ) { |
| 560 | array_push( $result->plugins, $plugin_data ); |
| 561 | } else { |
| 562 | array_unshift( $result->plugins, $plugin_data ); |
| 563 | } |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | // Recommend Genesis Ads add-on. |
| 568 | if ( defined( 'PARENT_THEME_NAME' ) && 'Genesis' === PARENT_THEME_NAME |
| 569 | && ! is_plugin_active( 'advanced-ads-genesis/genesis-ads.php' ) |
| 570 | && ! is_plugin_active_for_network( 'advanced-ads-genesis/genesis-ads.php' ) ) { |
| 571 | |
| 572 | // Grab all slugs from the api results. |
| 573 | $result_slugs = wp_list_pluck( $result->plugins, 'slug' ); |
| 574 | |
| 575 | if ( in_array( 'advanced-ads-genesis', $result_slugs, true ) ) { |
| 576 | return $result; |
| 577 | } |
| 578 | |
| 579 | $query_args = array( |
| 580 | 'slug' => 'advanced-ads-genesis', |
| 581 | 'fields' => array( |
| 582 | 'icons' => true, |
| 583 | 'active_installs' => true, |
| 584 | 'short_description' => true, |
| 585 | 'group' => true, |
| 586 | ), |
| 587 | ); |
| 588 | $plugin_data = plugins_api( 'plugin_information', $query_args ); |
| 589 | |
| 590 | if ( ! is_wp_error( $plugin_data ) ) { |
| 591 | if ( 'featured' === $args->browse ) { |
| 592 | array_push( $result->plugins, $plugin_data ); |
| 593 | } else { |
| 594 | array_unshift( $result->plugins, $plugin_data ); |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | // Recommend WP Bakery (former Visual Composer) add-on. |
| 600 | if ( defined( 'WPB_VC_VERSION' ) |
| 601 | && ! is_plugin_active( 'ads-for-visual-composer/advanced-ads-vc.php' ) |
| 602 | && ! is_plugin_active_for_network( 'ads-for-visual-composer/advanced-ads-vc.php' ) ) { |
| 603 | |
| 604 | // Grab all slugs from the api results. |
| 605 | $result_slugs = wp_list_pluck( $result->plugins, 'slug' ); |
| 606 | |
| 607 | if ( in_array( 'ads-for-visual-composer', $result_slugs, true ) ) { |
| 608 | return $result; |
| 609 | } |
| 610 | |
| 611 | $query_args = array( |
| 612 | 'slug' => 'ads-for-visual-composer', |
| 613 | 'fields' => array( |
| 614 | 'icons' => true, |
| 615 | 'active_installs' => true, |
| 616 | 'short_description' => true, |
| 617 | 'group' => true, |
| 618 | ), |
| 619 | ); |
| 620 | $plugin_data = plugins_api( 'plugin_information', $query_args ); |
| 621 | |
| 622 | if ( ! is_wp_error( $plugin_data ) ) { |
| 623 | if ( 'featured' === $args->browse ) { |
| 624 | array_push( $result->plugins, $plugin_data ); |
| 625 | } else { |
| 626 | array_unshift( $result->plugins, $plugin_data ); |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | return $result; |
| 632 | } |
| 633 | |
| 634 | /** |
| 635 | * Overrides WordPress text in Footer |
| 636 | * |
| 637 | * @param String $default_text The default footer text. |
| 638 | * |
| 639 | * @return string |
| 640 | */ |
| 641 | public function admin_footer_text( $default_text ) { |
| 642 | if ( $this->screen_belongs_to_advanced_ads() ) { |
| 643 | |
| 644 | /* translators: %s is the URL to add a new review, https://wordpress.org/support/plugin/advanced-ads/reviews/?filter=5#new-post */ |
| 645 | return sprintf( __( 'Thank the developer with a ★★★★★ review on <a href="%s" target="_blank">wordpress.org</a>', 'advanced-ads' ), 'https://wordpress.org/support/plugin/advanced-ads/reviews/?filter=5#new-post' ); |
| 646 | |
| 647 | } |
| 648 | return $default_text; |
| 649 | } |
| 650 | |
| 651 | } |
| 652 |