ad-health-notices.php
4 weeks ago
class-ad-network-ad-unit.php
5 months ago
class-ad-network.php
1 year ago
class-licenses.php
3 months ago
class-notices.php
5 months ago
class-overview-widgets.php
1 year ago
class-plugins-screen-updates.php
2 months ago
notices.php
5 months ago
class-ad-network.php
469 lines
| 1 | <?php // phpcs:ignore WordPress.Files.FileName |
| 2 | /** |
| 3 | * This class represents an ad network. It is used to manage the settings and the ad units of an ad network. |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | * @since 1.x.x |
| 8 | */ |
| 9 | |
| 10 | use AdvancedAds\Abstracts\Ad; |
| 11 | use AdvancedAds\Utilities\Conditional; |
| 12 | use AdvancedAds\Framework\Utilities\Params; |
| 13 | |
| 14 | /** |
| 15 | * Class Advanced_Ads_Ad_Network |
| 16 | */ |
| 17 | abstract class Advanced_Ads_Ad_Network { |
| 18 | /** |
| 19 | * The identifier will be used for generated ids, names etc. |
| 20 | * |
| 21 | * @var string |
| 22 | */ |
| 23 | protected $identifier; |
| 24 | |
| 25 | /** |
| 26 | * The name of the ad network |
| 27 | * |
| 28 | * @var string |
| 29 | */ |
| 30 | protected $name; |
| 31 | |
| 32 | /** |
| 33 | * The name of the hook for the advanced ads settings page |
| 34 | * |
| 35 | * @var string |
| 36 | */ |
| 37 | protected $settings_page_hook; |
| 38 | |
| 39 | /** |
| 40 | * The WordPress nonce (retrieve with the get_nonce method) |
| 41 | * |
| 42 | * @var string |
| 43 | */ |
| 44 | protected $nonce; |
| 45 | |
| 46 | /** |
| 47 | * The network’s settings section ID |
| 48 | * |
| 49 | * @var string |
| 50 | */ |
| 51 | protected $settings_section_id; |
| 52 | |
| 53 | /** |
| 54 | * The network’s settings init hook. |
| 55 | * |
| 56 | * @var string |
| 57 | */ |
| 58 | private $settings_init_hook; |
| 59 | |
| 60 | /** |
| 61 | * Advanced_Ads_Ad_Network constructor. |
| 62 | * |
| 63 | * @param string $identifier an identifier that will be used for hooks, settings, ids and much more - MAKE SURE IT IS UNIQUE. |
| 64 | * @param string $name - the (translateable) display name for this ad network. |
| 65 | */ |
| 66 | public function __construct( $identifier, $name ) { |
| 67 | $this->identifier = $identifier; |
| 68 | $this->name = $name; |
| 69 | $this->settings_page_hook = ADVADS_SLUG . '-' . $this->identifier . '-settings-page'; |
| 70 | $this->settings_section_id = ADVADS_SLUG . '-' . $this->identifier . '-settings-section'; |
| 71 | $this->settings_init_hook = ADVADS_SLUG . '-' . $this->identifier . '-settings-init'; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * The identifier for this network |
| 76 | * |
| 77 | * @return string |
| 78 | */ |
| 79 | public function get_identifier() { |
| 80 | return $this->identifier; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * The display name for this network |
| 85 | * |
| 86 | * @return string |
| 87 | */ |
| 88 | public function get_display_name() { |
| 89 | return $this->name; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * The display value for the settings tab |
| 94 | * |
| 95 | * @return string |
| 96 | */ |
| 97 | public function get_settings_tab_name() { |
| 98 | return $this->get_display_name(); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * URL for the settings page (admin) |
| 103 | * |
| 104 | * @return string |
| 105 | */ |
| 106 | public function get_settings_href() { |
| 107 | return admin_url( 'admin.php?page=advanced-ads-settings#top#' . $this->identifier ); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * The identifier / name for the javascript file that will be injected. |
| 112 | * |
| 113 | * @return string |
| 114 | */ |
| 115 | public function get_js_library_name() { |
| 116 | return 'advanced-ads-network' . $this->identifier; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Registers this ad network |
| 121 | */ |
| 122 | public function register() { |
| 123 | if ( is_admin() ) { |
| 124 | if ( wp_doing_ajax() ) { |
| 125 | // we need add all the actions for our ajax calls here. |
| 126 | // our ajax method that will trigger an update of the ad units of this network. |
| 127 | add_action( 'wp_ajax_advanced_ads_get_ad_units_' . $this->identifier, [ $this, 'update_external_ad_units' ] ); |
| 128 | add_action( 'wp_ajax_advanced_ads_toggle_idle_ads_' . $this->identifier, [ $this, 'toggle_idle_ads' ] ); |
| 129 | } else { |
| 130 | // find out if we need to register the settings. this is necessary |
| 131 | // 1) when viewing the settings (admin.php with page="advanced-ads-settings") |
| 132 | // 2) when posting the settings to options.php |
| 133 | // in all other cases, there is nothing to do. |
| 134 | global $pagenow; |
| 135 | $requires_settings = false; |
| 136 | $requires_javascript = false; |
| 137 | |
| 138 | if ( 'admin.php' === $pagenow ) { |
| 139 | $page = Params::request( 'page', null ); |
| 140 | switch ( $page ) { |
| 141 | case 'advanced-ads-settings': |
| 142 | $requires_settings = true; |
| 143 | $requires_javascript = true; |
| 144 | break; |
| 145 | case 'advanced-ads': |
| 146 | $requires_javascript = true; |
| 147 | break; |
| 148 | default: |
| 149 | break; |
| 150 | } |
| 151 | } elseif ( 'options.php' === $pagenow ) { |
| 152 | $requires_settings = true; |
| 153 | } elseif ( 'post.php' === $pagenow || 'post-new.php' === $pagenow ) { |
| 154 | add_action( 'advanced-ads-ad-pre-save', [ $this, 'sanitize_ad_settings' ], 10, 2 ); |
| 155 | |
| 156 | if ( 'edit' === Params::get( 'action' ) ) { |
| 157 | $requires_javascript = true; |
| 158 | } elseif ( 'advanced_ads' === Params::request( 'post_type', '' ) ) { |
| 159 | $requires_javascript = true; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | if ( $requires_settings ) { |
| 164 | // register the settings. |
| 165 | add_action( 'advanced-ads-settings-init', [ $this, 'register_settings_callback' ] ); |
| 166 | add_filter( 'advanced-ads-setting-tabs', [ $this, 'register_settings_tabs_callback' ] ); |
| 167 | } |
| 168 | if ( $requires_javascript ) { |
| 169 | add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts_callback' ] ); |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * This method will be called for the wp action "advanced-ads-settings-init" and therefore has to be public. |
| 177 | */ |
| 178 | public function register_settings_callback() { |
| 179 | // register new settings. |
| 180 | register_setting( |
| 181 | ADVADS_SLUG . '-' . $this->identifier, |
| 182 | ADVADS_SLUG . '-' . $this->identifier, |
| 183 | [ $this, 'sanitize_settings_callback' ] |
| 184 | ); |
| 185 | |
| 186 | /** |
| 187 | * Allow Ad Admin to save AdSense options. |
| 188 | * |
| 189 | * @param array $settings Array with allowed options. |
| 190 | * |
| 191 | * @return array |
| 192 | */ |
| 193 | add_filter( |
| 194 | 'advanced-ads-ad-admin-options', |
| 195 | function ( $options ) { |
| 196 | $options[] = ADVADS_SLUG . '-' . $this->identifier; |
| 197 | |
| 198 | return $options; |
| 199 | } |
| 200 | ); |
| 201 | |
| 202 | // add a new section. |
| 203 | add_settings_section( |
| 204 | $this->settings_section_id, |
| 205 | '', |
| 206 | '__return_empty_string', |
| 207 | $this->settings_page_hook |
| 208 | ); |
| 209 | |
| 210 | // register all the custom settings. |
| 211 | $this->register_settings( $this->settings_page_hook, $this->settings_section_id ); |
| 212 | |
| 213 | do_action( $this->settings_init_hook, $this->settings_page_hook ); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Create name of the object used for localized data |
| 218 | * |
| 219 | * @return string |
| 220 | */ |
| 221 | protected function get_localized_script_object_name() { |
| 222 | return $this->identifier . 'AdvancedAdsJS'; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Engueue scripts |
| 227 | */ |
| 228 | public function enqueue_scripts_callback() { |
| 229 | |
| 230 | if ( ! Conditional::is_screen_advanced_ads() ) { |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | $js_path = $this->get_javascript_base_path(); |
| 235 | if ( $js_path ) { |
| 236 | $id = $this->get_js_library_name(); |
| 237 | wp_enqueue_script( $id, $js_path, [ 'jquery' ], '1.7.3' ); // phpcs:ignore |
| 238 | // next we have to pass the data. |
| 239 | $data = [ |
| 240 | 'nonce' => $this->get_nonce(), |
| 241 | ]; |
| 242 | $data = $this->append_javascript_data( $data ); |
| 243 | wp_localize_script( $id, $this->get_localized_script_object_name(), $data ); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Get a nonce |
| 249 | * |
| 250 | * @return string |
| 251 | */ |
| 252 | public function get_nonce() { |
| 253 | if ( ! $this->nonce ) { |
| 254 | $this->nonce = wp_create_nonce( $this->get_nonce_action() ); |
| 255 | } |
| 256 | |
| 257 | return $this->nonce; |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Returns the action (name) of the nonce for this network |
| 262 | * in some cases you may want to override this method to faciliate |
| 263 | * integration with existing code |
| 264 | * |
| 265 | * @return string |
| 266 | */ |
| 267 | public function get_nonce_action() { |
| 268 | return 'advads-network-' . $this->identifier; |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * This method will be called for the wp action "advanced-ads-settings-tabs" and therefore has to be public. |
| 273 | * it simply adds a tab for this ad type. if you don't want that just override this method with an empty one. |
| 274 | * |
| 275 | * @param array $tabs tabs on Advanced Ads settings page. |
| 276 | * |
| 277 | * @return array |
| 278 | */ |
| 279 | public function register_settings_tabs_callback( $tabs ) { |
| 280 | $tab_id = $this->identifier; |
| 281 | $tabs[ $tab_id ] = [ |
| 282 | 'page' => $this->settings_page_hook, |
| 283 | 'group' => ADVADS_SLUG . '-' . $this->identifier, |
| 284 | 'tabid' => $tab_id, |
| 285 | 'title' => $this->get_settings_tab_name(), |
| 286 | ]; |
| 287 | |
| 288 | return $tabs; |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Callback to sanitize settings |
| 293 | * |
| 294 | * @param array $options options to be sanitized. |
| 295 | * |
| 296 | * @return mixed |
| 297 | */ |
| 298 | public function sanitize_settings_callback( $options ) { |
| 299 | $options = $this->sanitize_settings( $options ); |
| 300 | |
| 301 | return $options; |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Performs basic security checks for wp ajax requests (nonce, capabilities) |
| 306 | * dies, when a problem was detected |
| 307 | */ |
| 308 | protected function ajax_security_checks() { |
| 309 | if ( ! Conditional::user_can( 'advanced_ads_manage_options' ) ) { |
| 310 | $this->send_ajax_error_response_and_die( __( 'You don\'t have the permission to manage ads.', 'advanced-ads' ) ); |
| 311 | } |
| 312 | |
| 313 | $nonce = Params::request( 'nonce', '' ); |
| 314 | if ( ! wp_verify_nonce( $nonce, $this->get_nonce_action() ) ) { |
| 315 | $this->send_ajax_error_response_and_die( __( 'You sent an invalid request.', 'advanced-ads' ) ); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Send data via AJAX but don’t react on it. |
| 321 | * |
| 322 | * @param bool $json_serializable_response true if data can be serialized. |
| 323 | */ |
| 324 | protected function send_ajax_response_and_die( $json_serializable_response = false ) { |
| 325 | if ( ! $json_serializable_response ) { |
| 326 | $json_serializable_response = new stdClass(); |
| 327 | } |
| 328 | header( 'Content-Type: application/json' ); |
| 329 | echo wp_json_encode( $json_serializable_response ); |
| 330 | die(); |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Send message via AJAX but don’t react on it. |
| 335 | * |
| 336 | * @param string $message message string. |
| 337 | */ |
| 338 | protected function send_ajax_error_response_and_die( $message ) { |
| 339 | header( 'Content-Type: application/json' ); |
| 340 | $r = new stdClass(); |
| 341 | $r->error = $message; |
| 342 | echo wp_json_encode( $r ); |
| 343 | die(); |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * Toggle ad IDs |
| 348 | */ |
| 349 | public function toggle_idle_ads() { |
| 350 | $this->ajax_security_checks(); |
| 351 | global $external_ad_unit_id; |
| 352 | $hide_idle_ads = Params::post( 'hide' ); |
| 353 | $external_ad_unit_id = Params::post( 'ad_unit_id', '' ); |
| 354 | if ( ! $external_ad_unit_id ) { |
| 355 | $external_ad_unit_id = ''; |
| 356 | } |
| 357 | ob_start(); |
| 358 | |
| 359 | $this->print_external_ads_list( $hide_idle_ads ); |
| 360 | $ad_selector = ob_get_clean(); |
| 361 | |
| 362 | $response = [ |
| 363 | 'status' => true, |
| 364 | 'html' => $ad_selector, |
| 365 | ]; |
| 366 | $this->send_ajax_response_and_die( $response ); |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * When you need some kind of manual ad setup (meaning you can edit the custom inputs of this ad type) |
| 371 | * you should override this method to return true. this results in an additional link (Setup code manually) |
| 372 | * |
| 373 | * @return bool |
| 374 | */ |
| 375 | public function supports_manual_ad_setup() { |
| 376 | return false; |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * Print a list of ads. |
| 381 | * |
| 382 | * @param bool $hide_idle_ads true to hide idle ids. |
| 383 | * |
| 384 | * @return mixed |
| 385 | */ |
| 386 | abstract public function print_external_ads_list( $hide_idle_ads = true ); |
| 387 | |
| 388 | /** |
| 389 | * This method will be called via wp AJAX. |
| 390 | * it has to retrieve the list of ads from the ad network and store it as an option |
| 391 | * does not return ad units - use "get_external_ad_units" if you're looking for an array of ad units |
| 392 | */ |
| 393 | abstract public function update_external_ad_units(); |
| 394 | |
| 395 | /** |
| 396 | * Adds the custom wp settings to the tab for this ad unit |
| 397 | * |
| 398 | * @param string $hook hook for the settings page. |
| 399 | * @param string $section_id settings section ID. |
| 400 | */ |
| 401 | abstract protected function register_settings( $hook, $section_id ); |
| 402 | |
| 403 | /** |
| 404 | * Sanitize the network specific options |
| 405 | * |
| 406 | * @param array $options the options to sanitize. |
| 407 | * |
| 408 | * @return mixed the sanitizzed options |
| 409 | */ |
| 410 | abstract protected function sanitize_settings( $options ); |
| 411 | |
| 412 | /** |
| 413 | * Sanitize the settings for this ad network |
| 414 | * |
| 415 | * @param Ad $ad Ad instance. |
| 416 | * @param array $post_data Post data array. |
| 417 | * |
| 418 | * @return void |
| 419 | */ |
| 420 | abstract public function sanitize_ad_settings( Ad $ad, $post_data ); |
| 421 | |
| 422 | |
| 423 | /** |
| 424 | * Get external ad units from the given network. |
| 425 | * |
| 426 | * @return array of ad units (Advanced_Ads_Ad_Network_Ad_Unit) |
| 427 | */ |
| 428 | abstract public function get_external_ad_units(); |
| 429 | |
| 430 | /** |
| 431 | * Checks if the ad_unit is supported by Advanced Ads. |
| 432 | * this determines wheter it can be imported or not. |
| 433 | * |
| 434 | * @param object $ad_unit ad unit. |
| 435 | * |
| 436 | * @return boolean |
| 437 | */ |
| 438 | abstract public function is_supported( $ad_unit ); |
| 439 | |
| 440 | /** |
| 441 | * There is no common way to connect to an external account. you will have to implement it somehow, just |
| 442 | * like the whole setup process (usually done in the settings tab of this network). this method provides |
| 443 | * a way to return this account connection |
| 444 | * |
| 445 | * @return boolean true, when an account was successfully connected |
| 446 | */ |
| 447 | abstract public function is_account_connected(); |
| 448 | |
| 449 | /** |
| 450 | * External ad networks rely on the same javascript base code. however you still have to provide |
| 451 | * a javascript class that inherits from the AdvancedAdsAdNetwork js class |
| 452 | * this has to point to that file, or return false, |
| 453 | * if you don't have to include it in another way (NOT RECOMMENDED!) |
| 454 | * |
| 455 | * @return string path to the javascript file containing the javascriot class for this ad type |
| 456 | */ |
| 457 | abstract public function get_javascript_base_path(); |
| 458 | |
| 459 | /** |
| 460 | * Our script might need translations or other variables (llike a nonce, which is included automatically) |
| 461 | * add anything you need in this method and return the array |
| 462 | * |
| 463 | * @param array $data holding the data. |
| 464 | * |
| 465 | * @return array the data, that will be passed to the base javascript file containing the AdvancedAdsAdNetwork class |
| 466 | */ |
| 467 | abstract public function append_javascript_data( &$data ); |
| 468 | } |
| 469 |