EDD_SL_Plugin_Updater.php
6 years ago
ad-ajax.php
6 years ago
ad-debug.php
8 years ago
ad-health-notices.php
6 years ago
ad-model.php
6 years ago
ad-select.php
9 years ago
ad.php
6 years ago
ad_ajax_callbacks.php
6 years ago
ad_group.php
6 years ago
ad_placements.php
6 years ago
ad_type_abstract.php
6 years ago
ad_type_content.php
6 years ago
ad_type_dummy.php
6 years ago
ad_type_group.php
6 years ago
ad_type_image.php
6 years ago
ad_type_plain.php
6 years ago
checks.php
6 years ago
compatibility.php
6 years ago
display-conditions.php
6 years ago
filesystem.php
8 years ago
frontend-notices.php
6 years ago
frontend_checks.php
6 years ago
plugin.php
6 years ago
upgrades.php
7 years ago
utils.php
6 years ago
visitor-conditions.php
6 years ago
widget.php
6 years ago
frontend-notices.php
133 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Container class for frontend notice handling |
| 5 | * |
| 6 | * @package WordPress |
| 7 | * @subpackage Advanced Ads Plugin |
| 8 | * @since 1.16 |
| 9 | * |
| 10 | * related scripts / functions |
| 11 | * @todo build interface or parent class to share with other notice management in Advanced Ads, e.g., Ad Health Notices |
| 12 | * |
| 13 | */ |
| 14 | class Advanced_Ads_Frontend_Notices { |
| 15 | |
| 16 | /** |
| 17 | * Instance of this class. |
| 18 | * |
| 19 | * @var object |
| 20 | */ |
| 21 | protected static $instance = null; |
| 22 | |
| 23 | /** |
| 24 | * Options |
| 25 | * |
| 26 | * @var array |
| 27 | */ |
| 28 | protected $options; |
| 29 | |
| 30 | /** |
| 31 | * All detected notices |
| 32 | * |
| 33 | * @var array |
| 34 | */ |
| 35 | public $notices = array(); |
| 36 | |
| 37 | /** |
| 38 | * Advanced_Ads_Ad_Health_Notices constructor. |
| 39 | */ |
| 40 | public function __construct() { |
| 41 | |
| 42 | // failsafe for there were some reports of 502 errors. |
| 43 | if ( 1 < did_action( 'plugins_loaded' ) ) { |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Return an instance of this class. |
| 51 | * |
| 52 | * @return object A single instance of this class. |
| 53 | */ |
| 54 | public static function get_instance() { |
| 55 | |
| 56 | // If the single instance hasn't been set, set it now. |
| 57 | if ( null === self::$instance ) { |
| 58 | self::$instance = new self(); |
| 59 | } |
| 60 | |
| 61 | return self::$instance; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Updating an existing notice or add it, if it doesn’t exist, yet |
| 66 | * |
| 67 | * @param string $notice_key notice key to be added to the notice array. |
| 68 | * @param array $atts additional attributes. |
| 69 | * |
| 70 | * attributes: |
| 71 | * - append_text – text added to the default message |
| 72 | */ |
| 73 | public function update( $notice_key, $atts = array() ) { |
| 74 | |
| 75 | // check if the notice already exists. |
| 76 | $notice_key = esc_attr( $notice_key ); |
| 77 | $options_before = $options = $this->options(); |
| 78 | |
| 79 | // load notices from "queue". |
| 80 | $notices = isset( $options['notices'] ) ? $options['notices'] : array(); |
| 81 | |
| 82 | // check if notice_key was already saved, this prevents the same notice from showing up in different forms. |
| 83 | if ( ! isset( $notices[ $notice_key ] ) ) { |
| 84 | $notices[ $notice_key ] = array(); |
| 85 | } else { |
| 86 | // add `closed` marker, if given. |
| 87 | if ( ! empty( $atts['closed'] ) ) { |
| 88 | $notices[ $notice_key ]['closed'] = absint( $atts['closed'] ); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // update db. |
| 93 | $options['notices'] = $notices; |
| 94 | |
| 95 | // update db if changed. |
| 96 | if ( $options_before !== $options ) { |
| 97 | $this->update_options( $options ); |
| 98 | } |
| 99 | |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Return notices option from DB |
| 104 | * |
| 105 | * @return array $options |
| 106 | */ |
| 107 | public function options() { |
| 108 | if ( ! isset( $this->options ) ) { |
| 109 | $this->options = get_option( ADVADS_SLUG . '-frontend-notices', array() ); |
| 110 | } |
| 111 | if ( ! is_array( $this->options ) ) { |
| 112 | $this->options = array(); |
| 113 | } |
| 114 | |
| 115 | return $this->options; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Update notice options |
| 120 | * |
| 121 | * @param array $options new options. |
| 122 | */ |
| 123 | public function update_options( array $options ) { |
| 124 | // do not allow to clear options. |
| 125 | if ( array() === $options ) { |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | $this->options = $options; |
| 130 | update_option( ADVADS_SLUG . '-frontend-notices', $options ); |
| 131 | } |
| 132 | } |
| 133 |