class-advanced-ads-admin.php
757 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 | * |
| 76 | * @return mixed 1 on success or string with error message. |
| 77 | * @since version 1.7.16 (early January 2017) |
| 78 | * |
| 79 | */ |
| 80 | public function deactivate_license( $addon = '', $plugin_name = '', $options_slug = '' ) { |
| 81 | return Advanced_Ads_Admin_Licenses::get_instance()->deactivate_license( $addon, $plugin_name, $options_slug ); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Get license status. |
| 86 | * |
| 87 | * @param string $slug slug of the add-on. |
| 88 | * |
| 89 | * @return string license status |
| 90 | */ |
| 91 | public function get_license_status( $slug = '' ) { |
| 92 | return Advanced_Ads_Admin_Licenses::get_instance()->get_license_status( $slug ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Actions and filter available after all plugins are initialized. |
| 97 | */ |
| 98 | public function wp_plugins_loaded() { |
| 99 | /* |
| 100 | * Call $plugin_slug from public plugin class. |
| 101 | * |
| 102 | */ |
| 103 | $plugin = Advanced_Ads::get_instance(); |
| 104 | $this->plugin_slug = $plugin->get_plugin_slug(); |
| 105 | |
| 106 | add_action( 'current_screen', array( $this, 'current_screen' ) ); |
| 107 | |
| 108 | // Load admin style sheet and JavaScript. |
| 109 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ) ); |
| 110 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ), 9 ); |
| 111 | |
| 112 | // update placements. |
| 113 | add_action( 'admin_init', array( 'Advanced_Ads_Placements', 'update_placements' ) ); |
| 114 | |
| 115 | // add Advanced Ads admin notices |
| 116 | // removes admin notices from other plugins |
| 117 | // `in_admin_header` is the last hook to run before àdmin_notices` according to https://codex.wordpress.org/Plugin_API/Action_Reference. |
| 118 | add_action( 'in_admin_header', array( $this, 'register_admin_notices' ) ); |
| 119 | |
| 120 | // add links to plugin page. |
| 121 | add_filter( 'plugin_action_links_' . ADVADS_BASE, array( $this, 'add_plugin_links' ) ); |
| 122 | |
| 123 | // display information when user is going to disable the plugin. |
| 124 | add_filter( 'admin_footer', array( $this, 'add_deactivation_logic' ) ); |
| 125 | // add_filter( 'after_plugin_row_' . ADVADS_BASE, array( $this, 'display_deactivation_message' ) ); |
| 126 | // disable adding rel="noopener noreferrer" to link added through TinyMCE for rich content ads. |
| 127 | add_filter( 'tiny_mce_before_init', array( $this, 'tinymce_allow_unsafe_link_target' ) ); |
| 128 | |
| 129 | add_action( 'plugins_api_result', array( $this, 'recommend_suitable_add_ons' ), 11, 3 ); |
| 130 | |
| 131 | // register dynamic action to load a starter setup. |
| 132 | add_action( 'admin_action_advanced_ads_starter_setup', array( $this, 'import_starter_setup' ) ); |
| 133 | |
| 134 | Advanced_Ads_Admin_Meta_Boxes::get_instance(); |
| 135 | Advanced_Ads_Admin_Menu::get_instance(); |
| 136 | Advanced_Ads_Admin_Ad_Type::get_instance(); |
| 137 | Advanced_Ads_Admin_Settings::get_instance(); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Actions and filters that should also be available for ajax |
| 142 | */ |
| 143 | public function wp_plugins_loaded_ajax() { |
| 144 | // needed here in order to work with Quick Edit option on ad list page. |
| 145 | Advanced_Ads_Admin_Ad_Type::get_instance(); |
| 146 | |
| 147 | add_action( 'wp_ajax_advads_send_feedback', array( $this, 'send_feedback' ) ); |
| 148 | add_action( 'wp_ajax_advads_load_rss_widget_content', array( 'Advanced_Ads_Admin_Meta_Boxes', 'dashboard_widget_function_output' ) ); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Return an instance of this class. |
| 153 | * |
| 154 | * @return object A single instance of this class. |
| 155 | * @since 1.0.0 |
| 156 | * |
| 157 | */ |
| 158 | public static function get_instance() { |
| 159 | // If the single instance hasn't been set, set it now. |
| 160 | if ( null === self::$instance ) { |
| 161 | self::$instance = new self(); |
| 162 | } |
| 163 | |
| 164 | return self::$instance; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * General stuff after page is loaded and screen variable is available |
| 169 | */ |
| 170 | public function current_screen() { |
| 171 | $screen = get_current_screen(); |
| 172 | |
| 173 | if ( ! isset( $screen->id ) ) { |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | switch ( $screen->id ) { |
| 178 | case 'edit-advanced_ads': // ad overview page. |
| 179 | case 'advanced_ads': // ad edit page. |
| 180 | // remove notice about missing first ad. |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Register and enqueue admin-specific style sheet. |
| 187 | * |
| 188 | * @since 1.0.0 |
| 189 | */ |
| 190 | public function enqueue_admin_styles() { |
| 191 | wp_enqueue_style( $this->plugin_slug . '-admin-styles', plugins_url( 'assets/css/admin.css', __FILE__ ), array(), ADVADS_VERSION ); |
| 192 | if ( self::screen_belongs_to_advanced_ads() ) { |
| 193 | // jQuery ui smoothness style 1.11.4. |
| 194 | wp_enqueue_style( $this->plugin_slug . '-jquery-ui-styles', plugins_url( 'assets/jquery-ui/jquery-ui.min.css', __FILE__ ), array(), '1.11.4' ); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Register and enqueue admin-specific JavaScript. |
| 200 | * |
| 201 | * @since 1.0.0 |
| 202 | */ |
| 203 | public function enqueue_admin_scripts() { |
| 204 | |
| 205 | // global js script. |
| 206 | wp_enqueue_script( $this->plugin_slug . '-admin-global-script', plugins_url( 'assets/js/admin-global.js', __FILE__ ), array( 'jquery' ), ADVADS_VERSION, false ); |
| 207 | wp_enqueue_script( $this->plugin_slug . '-admin-find-adblocker', plugins_url( 'assets/js/advertisement.js', __FILE__ ), array(), ADVADS_VERSION, false ); |
| 208 | |
| 209 | // register ajax nonce. |
| 210 | $params = array( |
| 211 | 'ajax_nonce' => wp_create_nonce( 'advanced-ads-admin-ajax-nonce' ), |
| 212 | ); |
| 213 | wp_localize_script( $this->plugin_slug . '-admin-global-script', 'advadsglobal', $params ); |
| 214 | |
| 215 | if ( self::screen_belongs_to_advanced_ads() ) { |
| 216 | 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 ); |
| 217 | wp_register_script( $this->plugin_slug . '-conditions-script', plugins_url( 'assets/js/conditions.js', __FILE__ ), array( 'jquery', 'jquery-ui-autocomplete', 'jquery-ui-button' ), ADVADS_VERSION, false ); |
| 218 | wp_register_script( $this->plugin_slug . '-wizard-script', plugins_url( 'assets/js/wizard.js', __FILE__ ), array( 'jquery' ), ADVADS_VERSION, false ); |
| 219 | |
| 220 | // jquery ui. |
| 221 | wp_enqueue_script( 'jquery-ui-accordion' ); |
| 222 | wp_enqueue_script( 'jquery-ui-button' ); |
| 223 | wp_enqueue_script( 'jquery-ui-tooltip' ); |
| 224 | |
| 225 | // just register this script for later inclusion on ad group list page. |
| 226 | wp_register_script( 'inline-edit-group-ads', plugins_url( 'assets/js/inline-edit-group-ads.js', __FILE__ ), array( 'jquery' ), ADVADS_VERSION, false ); |
| 227 | |
| 228 | $auto_ads_strings = Advanced_Ads_AdSense_Admin::get_auto_ads_messages(); |
| 229 | |
| 230 | // register admin.js translations. |
| 231 | $translation_array = array( |
| 232 | 'condition_or' => __( 'or', 'advanced-ads' ), |
| 233 | 'condition_and' => __( 'and', 'advanced-ads' ), |
| 234 | 'after_paragraph_promt' => __( 'After which paragraph?', 'advanced-ads' ), |
| 235 | 'page_level_ads_enabled' => $auto_ads_strings['enabled'], |
| 236 | 'today' => __( 'Today', 'advanced-ads' ), |
| 237 | 'yesterday' => __( 'Yesterday', 'advanced-ads' ), |
| 238 | 'this_month' => __( 'This Month', 'advanced-ads' ), |
| 239 | /* translators: 1: The number of days. */ |
| 240 | 'last_n_days' => __( 'Last %1$d days', 'advanced-ads' ), |
| 241 | /* translators: 1: An error message. */ |
| 242 | 'error_message' => __( 'An error occurred: %1$s' ), |
| 243 | 'all' => __( 'All', 'advanced-ads' ), |
| 244 | 'no_results' => __( 'There were no results returned for this ad. Please make sure it is active, generating impressions and double check your ad parameters.', 'advanced-ads' ), |
| 245 | 'show_inactive_ads' => __( 'Show inactive ads', 'advanced-ads' ), |
| 246 | 'hide_inactive_ads' => __( 'Hide inactive ads', 'advanced-ads' ), |
| 247 | 'display_conditions_form_name' => Advanced_Ads_Display_Conditions::FORM_NAME, // not meant for translation. |
| 248 | ); |
| 249 | |
| 250 | wp_localize_script( $this->plugin_slug . '-admin-script', 'advadstxt', $translation_array ); |
| 251 | |
| 252 | wp_enqueue_script( $this->plugin_slug . '-admin-script' ); |
| 253 | wp_enqueue_script( $this->plugin_slug . '-conditions-script' ); |
| 254 | wp_enqueue_script( $this->plugin_slug . '-wizard-script' ); |
| 255 | } |
| 256 | |
| 257 | // call media manager for image upload only on ad edit pages. |
| 258 | $screen = get_current_screen(); |
| 259 | if ( isset( $screen->id ) && Advanced_Ads::POST_TYPE_SLUG === $screen->id ) { |
| 260 | // the 'wp_enqueue_media' function can be executed only once and should be called with the 'post' parameter |
| 261 | // 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. |
| 262 | // since other plugins can call the 'wp_enqueue_media' function without the 'post' parameter, Advanced Ads should call it earlier. |
| 263 | global $post; |
| 264 | wp_enqueue_media( array( 'post' => $post ) ); |
| 265 | } |
| 266 | |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Check if the current screen belongs to Advanced Ads |
| 271 | * |
| 272 | * @return bool true if screen belongs to Advanced Ads |
| 273 | * @since 1.6.6 |
| 274 | */ |
| 275 | public static function screen_belongs_to_advanced_ads() { |
| 276 | |
| 277 | if ( ! function_exists( 'get_current_screen' ) ) { |
| 278 | return false; |
| 279 | } |
| 280 | |
| 281 | $screen = get_current_screen(); |
| 282 | if ( ! isset( $screen->id ) ) { |
| 283 | return false; |
| 284 | } |
| 285 | |
| 286 | $advads_pages = apply_filters( |
| 287 | 'advanced-ads-dashboard-screens', array( |
| 288 | 'advanced-ads_page_advanced-ads-groups', // ad groups. |
| 289 | 'edit-advanced_ads', // ads overview. |
| 290 | 'advanced_ads', // ad edit page. |
| 291 | 'advanced-ads_page_advanced-ads-placements', // placements. |
| 292 | 'advanced-ads_page_advanced-ads-settings', // settings. |
| 293 | 'toplevel_page_advanced-ads', // overview. |
| 294 | 'admin_page_advanced-ads-debug', // debug. |
| 295 | // 'advanced-ads_page_advanced-ads-support', // support. |
| 296 | 'admin_page_advanced-ads-import-export', // import & export. |
| 297 | ) |
| 298 | ); |
| 299 | |
| 300 | if ( in_array( $screen->id, $advads_pages, true ) ) { |
| 301 | return true; |
| 302 | } |
| 303 | |
| 304 | return false; |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Get action from the params |
| 309 | * |
| 310 | * @since 1.0.0 |
| 311 | */ |
| 312 | public function current_action() { |
| 313 | $request = wp_unslash( $_REQUEST ); |
| 314 | if ( isset( $request['action'] ) && - 1 !== $request['action'] ) { |
| 315 | return $request['action']; |
| 316 | } |
| 317 | |
| 318 | return false; |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Get DateTimeZone object for the WP installation |
| 323 | */ |
| 324 | public static function get_wp_timezone() { |
| 325 | $_time_zone = get_option( 'timezone_string' ); |
| 326 | $time_zone = new DateTimeZone( 'UTC' ); |
| 327 | if ( $_time_zone ) { |
| 328 | $time_zone = new DateTimeZone( $_time_zone ); |
| 329 | } else { |
| 330 | $gmt_offset = floatval( get_option( 'gmt_offset' ) ); |
| 331 | $sign = ( 0 > $gmt_offset ) ? '-' : '+'; |
| 332 | $int = floor( abs( $gmt_offset ) ); |
| 333 | $frac = abs( $gmt_offset ) - $int; |
| 334 | |
| 335 | $gmt = ''; |
| 336 | if ( $gmt_offset ) { |
| 337 | $gmt .= $sign . zeroise( $int, 2 ) . ':' . zeroise( 60 * $frac, 2 ); |
| 338 | $time_zone = date_create( '2017-10-01T12:00:00' . $gmt )->getTimezone(); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | return $time_zone; |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Get literal expression of timezone |
| 347 | * |
| 348 | * @param DateTimeZone $date_time_zone the DateTimeZone object to get literal value from. |
| 349 | */ |
| 350 | public static function timezone_get_name( $date_time_zone ) { |
| 351 | if ( $date_time_zone instanceof DateTimeZone ) { |
| 352 | $time_zone = timezone_name_get( $date_time_zone ); |
| 353 | if ( 'UTC' === $time_zone ) { |
| 354 | return 'UTC+0'; |
| 355 | } |
| 356 | if ( false === strpos( $time_zone, '/' ) ) { |
| 357 | $time_zone = 'UTC' . $time_zone; |
| 358 | } else { |
| 359 | // translators: time zone name. |
| 360 | $time_zone = sprintf( __( 'time of %s', 'advanced-ads' ), $time_zone ); |
| 361 | } |
| 362 | |
| 363 | return $time_zone; |
| 364 | } |
| 365 | |
| 366 | return 'UTC+0'; |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Registers Advanced Ads admin notices |
| 371 | * prevents other notices from showing up on our own pages |
| 372 | */ |
| 373 | public function register_admin_notices() { |
| 374 | |
| 375 | /** |
| 376 | * remove all registered admin_notices from AA screens |
| 377 | * - we need to use this or some users have half or more of their viewports cluttered with unrelated notices |
| 378 | */ |
| 379 | if ( $this->screen_belongs_to_advanced_ads() ) { |
| 380 | remove_all_actions( 'admin_notices' ); |
| 381 | } |
| 382 | |
| 383 | // register our own notices |
| 384 | add_action( 'admin_notices', array( $this, 'admin_notices' ) ); |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Initiate the admin notices class |
| 389 | * |
| 390 | * @since 1.5.3 |
| 391 | */ |
| 392 | public function admin_notices() { |
| 393 | // display ad block warning to everyone who can edit ads. |
| 394 | if ( current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads' ) ) ) { |
| 395 | if ( $this->screen_belongs_to_advanced_ads() ) { |
| 396 | include ADVADS_BASE_PATH . 'admin/views/notices/adblock.php'; |
| 397 | include ADVADS_BASE_PATH . 'admin/views/notices/jqueryui_error.php'; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | // Show success notice after starter setup was imported. Registered here because it will be visible only once. |
| 402 | if ( isset( $_GET['message'] ) && 'advanced-ads-starter-setup-success' === $_GET['message'] ) { |
| 403 | add_action( 'advanced-ads-admin-notices', array( $this, 'starter_setup_success_message' ) ); |
| 404 | } |
| 405 | |
| 406 | // register our own notices on Advanced Ads pages, except from the overview page where they should appear in the notices section. |
| 407 | $screen = get_current_screen(); |
| 408 | if ( class_exists( 'Advanced_Ads_Admin_Notices' ) |
| 409 | && current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads' ) ) |
| 410 | && ( ! isset( $screen->id ) || 'toplevel_page_advanced-ads' !== $screen->id ) ) { |
| 411 | $this->notices = Advanced_Ads_Admin_Notices::get_instance()->notices; |
| 412 | Advanced_Ads_Admin_Notices::get_instance()->display_notices(); |
| 413 | |
| 414 | // allow other Advanced Ads plugins to show admin notices at this late stage. |
| 415 | do_action( 'advanced-ads-admin-notices' ); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * Add links to the plugins list |
| 421 | * |
| 422 | * @param arr $links array of links for the plugins, adapted when the current plugin is found. |
| 423 | * |
| 424 | * @return array $links |
| 425 | * @since 1.6.14 |
| 426 | */ |
| 427 | public function add_plugin_links( $links ) { |
| 428 | |
| 429 | if ( ! is_array( $links ) ) { |
| 430 | return $links; |
| 431 | } |
| 432 | |
| 433 | // add link to support page. |
| 434 | $support_link = '<a href="' . esc_url( admin_url( 'admin.php?page=advanced-ads-settings#top#support' ) ) . '">' . __( 'Support', 'advanced-ads' ) . '</a>'; |
| 435 | array_unshift( $links, $support_link ); |
| 436 | |
| 437 | // add link to add-ons. |
| 438 | $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>'; |
| 439 | array_unshift( $links, $extend_link ); |
| 440 | |
| 441 | return $links; |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * Display deactivation logic on plugins page |
| 446 | * |
| 447 | * @since 1.7.14 |
| 448 | */ |
| 449 | public function add_deactivation_logic() { |
| 450 | |
| 451 | $screen = get_current_screen(); |
| 452 | if ( ! isset( $screen->id ) || ! in_array( $screen->id, array( 'plugins', 'plugins-network' ), true ) ) { |
| 453 | return; |
| 454 | } |
| 455 | |
| 456 | $current_user = wp_get_current_user(); |
| 457 | if ( ! ( $current_user instanceof WP_User ) ) { |
| 458 | $from = ''; |
| 459 | $email = ''; |
| 460 | } else { |
| 461 | $from = $current_user->user_nicename . ' <' . trim( $current_user->user_email ) . '>'; |
| 462 | $email = $current_user->user_email; |
| 463 | } |
| 464 | |
| 465 | include ADVADS_BASE_PATH . 'admin/views/feedback-disable.php'; |
| 466 | } |
| 467 | |
| 468 | /** |
| 469 | * Send feedback via email |
| 470 | * |
| 471 | * @since 1.7.14 |
| 472 | */ |
| 473 | public function send_feedback() { |
| 474 | /** |
| 475 | * We first need to get the form data from the string and can verify the nonce afterwards |
| 476 | * This throws an issue with the WP Coding Standards though |
| 477 | */ |
| 478 | if ( isset( $_POST['formdata'] ) ) { |
| 479 | parse_str( wp_unslash( $_POST['formdata'] ), $form ); |
| 480 | } |
| 481 | |
| 482 | if ( ! wp_verify_nonce( $form['advanced_ads_disable_form_nonce'], 'advanced_ads_disable_form' ) ) { |
| 483 | die(); |
| 484 | } |
| 485 | |
| 486 | $text = ''; |
| 487 | if ( isset( $form['advanced_ads_disable_text'] ) ) { |
| 488 | $text = implode( "\n\r", $form['advanced_ads_disable_text'] ); |
| 489 | } |
| 490 | |
| 491 | // get first version to see if this is a new problem or might be an older on. |
| 492 | $options = Advanced_Ads_Plugin::get_instance()->internal_options(); |
| 493 | $installed = isset( $options['installed'] ) ? date( 'd.m.Y', $options['installed'] ) : '–'; |
| 494 | |
| 495 | $text .= "\n\n" . home_url() . " ($installed)"; |
| 496 | |
| 497 | $headers = array(); |
| 498 | |
| 499 | $from = isset( $form['advanced_ads_disable_from'] ) ? $form['advanced_ads_disable_from'] : ''; |
| 500 | // the user clicked on the "don’t disable" button or if an address is given in the form then use that one. |
| 501 | if ( isset( $form['advanced_ads_disable_reason'] ) |
| 502 | && 'get help' === $form['advanced_ads_disable_reason'] |
| 503 | && ! empty( $form['advanced_ads_disable_reply_email'] ) ) { |
| 504 | $email = isset( $form['advanced_ads_disable_reply_email'] ) ? trim( $form['advanced_ads_disable_reply_email'] ) : $current_user->email; |
| 505 | $current_user = wp_get_current_user(); |
| 506 | $name = ( $current_user instanceof WP_User ) ? $current_user->user_nicename : ''; |
| 507 | $from = $name . ' <' . $email . '>'; |
| 508 | $is_german = ( preg_match( '/\.de$/', $from ) || "de_" === substr( get_locale(), 0, 3 ) || "de_" === substr( get_user_locale(), 0, 3 ) ); |
| 509 | if ( isset( $form['advanced_ads_disable_text'][0] ) |
| 510 | && trim( $form['advanced_ads_disable_text'][0] ) !== '' ) { // is a text given then ask for help. |
| 511 | // send German text |
| 512 | if ( $is_german ) { |
| 513 | $text .= "\n\n Hilfe ist auf dem Weg."; |
| 514 | } else { |
| 515 | $text .= "\n\n Help is on its way."; |
| 516 | } |
| 517 | } else { // if no text is given, just reply. |
| 518 | if ( $is_german ) { |
| 519 | $text .= "\n\n Vielen Dank für das Feedback."; |
| 520 | } else { |
| 521 | $text .= "\n\n Thank you for your feedback."; |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | if ( $from ) { |
| 526 | $headers[] = "From: $from"; |
| 527 | $headers[] = "Reply-To: $from"; |
| 528 | } |
| 529 | |
| 530 | $subject = isset( $form['advanced_ads_disable_reason'] ) ? $form['advanced_ads_disable_reason'] : '(no reason given)'; |
| 531 | // append plugin name to get a better subject. |
| 532 | $subject .= ' (Advanced Ads)'; |
| 533 | |
| 534 | $success = wp_mail( 'improve@wpadvancedads.com', $subject, $text, $headers ); |
| 535 | |
| 536 | die(); |
| 537 | |
| 538 | } |
| 539 | |
| 540 | /** |
| 541 | * Configure TinyMCE to allow unsafe link target. |
| 542 | * |
| 543 | * @param boolean $mce_init the tinyMce initialization array. |
| 544 | * |
| 545 | * @return boolean |
| 546 | */ |
| 547 | public function tinymce_allow_unsafe_link_target( $mce_init ) { |
| 548 | |
| 549 | // check if we are on the ad edit screen. |
| 550 | if ( ! function_exists( 'get_current_screen' ) ) { |
| 551 | return $mce_init; |
| 552 | } |
| 553 | |
| 554 | $screen = get_current_screen(); |
| 555 | if ( isset( $screen->id ) && 'advanced_ads' === $screen->id ) { |
| 556 | $mce_init['allow_unsafe_link_target'] = true; |
| 557 | } |
| 558 | |
| 559 | return $mce_init; |
| 560 | } |
| 561 | |
| 562 | /** |
| 563 | * Sort visitor and display condition arrays alphabetically by their label. |
| 564 | * |
| 565 | * @param array $a array to be compared. |
| 566 | * @param array $b array to be compared. |
| 567 | * |
| 568 | * @since 1.8.12 |
| 569 | */ |
| 570 | public static function sort_condition_array_by_label( $a, $b ) { |
| 571 | if ( ! isset( $a['label'] ) || ! isset( $b['label'] ) ) { |
| 572 | return; |
| 573 | } |
| 574 | |
| 575 | return strcmp( strtolower( $a['label'] ), strtolower( $b['label'] ) ); |
| 576 | } |
| 577 | |
| 578 | /** |
| 579 | * Recommend additional add-ons |
| 580 | * |
| 581 | * @param object $result original result object. |
| 582 | * @param unknown $action action. |
| 583 | * @param object $args additional arguments. |
| 584 | */ |
| 585 | public function recommend_suitable_add_ons( $result, $action, $args ) { |
| 586 | if ( empty( $args->browse ) ) { |
| 587 | return $result; |
| 588 | } |
| 589 | |
| 590 | if ( 'featured' !== $args->browse && 'recommended' !== $args->browse && 'popular' !== $args->browse ) { |
| 591 | return $result; |
| 592 | } |
| 593 | |
| 594 | if ( ! isset( $result->info['page'] ) || 1 < $result->info['page'] ) { |
| 595 | return $result; |
| 596 | } |
| 597 | |
| 598 | // Recommend AdSense In-Feed add-on. |
| 599 | if ( ! is_plugin_active( 'advanced-ads-adsense-in-feed/advanced-ads-in-feed.php' ) |
| 600 | && ! is_plugin_active_for_network( 'advanced-ads-adsense-in-feed/advanced-ads-in-feed.php' ) ) { |
| 601 | |
| 602 | // Grab all slugs from the api results. |
| 603 | $result_slugs = wp_list_pluck( $result->plugins, 'slug' ); |
| 604 | |
| 605 | if ( in_array( 'advanced-ads-adsense-in-feed', $result_slugs, true ) ) { |
| 606 | return $result; |
| 607 | } |
| 608 | |
| 609 | $query_args = array( |
| 610 | 'slug' => 'advanced-ads-adsense-in-feed', |
| 611 | 'fields' => array( |
| 612 | 'icons' => true, |
| 613 | 'active_installs' => true, |
| 614 | 'short_description' => true, |
| 615 | 'group' => true, |
| 616 | ), |
| 617 | ); |
| 618 | $plugin_data = plugins_api( 'plugin_information', $query_args ); |
| 619 | |
| 620 | if ( ! is_wp_error( $plugin_data ) ) { |
| 621 | if ( 'featured' === $args->browse ) { |
| 622 | array_push( $result->plugins, $plugin_data ); |
| 623 | } else { |
| 624 | array_unshift( $result->plugins, $plugin_data ); |
| 625 | } |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | // Recommend Genesis Ads add-on. |
| 630 | if ( defined( 'PARENT_THEME_NAME' ) && 'Genesis' === PARENT_THEME_NAME |
| 631 | && ! is_plugin_active( 'advanced-ads-genesis/genesis-ads.php' ) |
| 632 | && ! is_plugin_active_for_network( 'advanced-ads-genesis/genesis-ads.php' ) ) { |
| 633 | |
| 634 | // Grab all slugs from the api results. |
| 635 | $result_slugs = wp_list_pluck( $result->plugins, 'slug' ); |
| 636 | |
| 637 | if ( in_array( 'advanced-ads-genesis', $result_slugs, true ) ) { |
| 638 | return $result; |
| 639 | } |
| 640 | |
| 641 | $query_args = array( |
| 642 | 'slug' => 'advanced-ads-genesis', |
| 643 | 'fields' => array( |
| 644 | 'icons' => true, |
| 645 | 'active_installs' => true, |
| 646 | 'short_description' => true, |
| 647 | 'group' => true, |
| 648 | ), |
| 649 | ); |
| 650 | $plugin_data = plugins_api( 'plugin_information', $query_args ); |
| 651 | |
| 652 | if ( ! is_wp_error( $plugin_data ) ) { |
| 653 | if ( 'featured' === $args->browse ) { |
| 654 | array_push( $result->plugins, $plugin_data ); |
| 655 | } else { |
| 656 | array_unshift( $result->plugins, $plugin_data ); |
| 657 | } |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | // Recommend WP Bakery (former Visual Composer) add-on. |
| 662 | if ( defined( 'WPB_VC_VERSION' ) |
| 663 | && ! is_plugin_active( 'ads-for-visual-composer/advanced-ads-vc.php' ) |
| 664 | && ! is_plugin_active_for_network( 'ads-for-visual-composer/advanced-ads-vc.php' ) ) { |
| 665 | |
| 666 | // Grab all slugs from the api results. |
| 667 | $result_slugs = wp_list_pluck( $result->plugins, 'slug' ); |
| 668 | |
| 669 | if ( in_array( 'ads-for-visual-composer', $result_slugs, true ) ) { |
| 670 | return $result; |
| 671 | } |
| 672 | |
| 673 | $query_args = array( |
| 674 | 'slug' => 'ads-for-visual-composer', |
| 675 | 'fields' => array( |
| 676 | 'icons' => true, |
| 677 | 'active_installs' => true, |
| 678 | 'short_description' => true, |
| 679 | 'group' => true, |
| 680 | ), |
| 681 | ); |
| 682 | $plugin_data = plugins_api( 'plugin_information', $query_args ); |
| 683 | |
| 684 | if ( ! is_wp_error( $plugin_data ) ) { |
| 685 | if ( 'featured' === $args->browse ) { |
| 686 | array_push( $result->plugins, $plugin_data ); |
| 687 | } else { |
| 688 | array_unshift( $result->plugins, $plugin_data ); |
| 689 | } |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | return $result; |
| 694 | } |
| 695 | |
| 696 | /** |
| 697 | * Overrides WordPress text in Footer |
| 698 | * |
| 699 | * @param String $default_text The default footer text. |
| 700 | * |
| 701 | * @return string |
| 702 | */ |
| 703 | public function admin_footer_text( $default_text ) { |
| 704 | if ( $this->screen_belongs_to_advanced_ads() ) { |
| 705 | |
| 706 | /* translators: %s is the URL to add a new review, https://wordpress.org/support/plugin/advanced-ads/reviews/#new-post */ |
| 707 | 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/#new-post' ); |
| 708 | |
| 709 | } |
| 710 | |
| 711 | return $default_text; |
| 712 | } |
| 713 | |
| 714 | /** |
| 715 | * Import a starter setup for new users |
| 716 | */ |
| 717 | public function import_starter_setup() { |
| 718 | |
| 719 | if ( |
| 720 | ! isset( $_GET['action'] ) |
| 721 | || 'advanced_ads_starter_setup' !== $_GET['action'] |
| 722 | || ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads' ) ) |
| 723 | ) { |
| 724 | return; |
| 725 | } |
| 726 | |
| 727 | check_admin_referer( 'advanced-ads-starter-setup' ); |
| 728 | |
| 729 | // remove_query_arg( 'inject-ad-setup' ); |
| 730 | |
| 731 | // start importing the ads. |
| 732 | $xml = file_get_contents( ADVADS_BASE_PATH . 'admin/assets/xml/starter-setup.xml' ); |
| 733 | |
| 734 | Advanced_Ads_Import::get_instance()->import( $xml ); |
| 735 | |
| 736 | // redirect to ad overview page. |
| 737 | wp_safe_redirect( admin_url( 'edit.php?post_type=advanced_ads&message=advanced-ads-starter-setup-success' ) ); |
| 738 | } |
| 739 | |
| 740 | /** |
| 741 | * Show success message after starter setup was created. |
| 742 | */ |
| 743 | public function starter_setup_success_message() { |
| 744 | |
| 745 | // load link to latest post. |
| 746 | |
| 747 | $args = array( |
| 748 | 'numberposts' => 1, |
| 749 | ); |
| 750 | $last_post = get_posts( $args ); |
| 751 | $last_post_link = isset( $last_post[0]->ID ) ? get_permalink( $last_post[0]->ID ) : false; |
| 752 | |
| 753 | include ADVADS_BASE_PATH . 'admin/views/notices/starter-setup-success.php'; |
| 754 | } |
| 755 | |
| 756 | } |
| 757 |