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