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-overview-widgets.php
272 lines
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | |
| 3 | use AdvancedAds\Admin\Translation_Promo; |
| 4 | |
| 5 | /** |
| 6 | * Container class for callbacks for overview widgets |
| 7 | * |
| 8 | * @package AdvancedAds |
| 9 | * @author Advanced Ads <info@wpadvancedads.com> |
| 10 | * @since 1.x.x |
| 11 | */ |
| 12 | class Advanced_Ads_Overview_Widgets_Callbacks { |
| 13 | /** |
| 14 | * In case one wants to inject several dashboards into a page, we will prevent executing redundant javascript |
| 15 | * with the help of this little bool |
| 16 | * |
| 17 | * @var mixed |
| 18 | */ |
| 19 | private static $processed_adsense_stats_js = false; |
| 20 | |
| 21 | /** |
| 22 | * When doing ajax request (refreshing the dashboard), we need to have a nonce. |
| 23 | * one is enough, that's why we need to remember it. |
| 24 | * |
| 25 | * @var mixed |
| 26 | */ |
| 27 | private static $gadsense_dashboard_nonce = false; |
| 28 | |
| 29 | |
| 30 | /** |
| 31 | * Register the plugin overview widgets |
| 32 | */ |
| 33 | public static function setup_overview_widgets() { |
| 34 | |
| 35 | // initiate i18n notice. |
| 36 | ( new Translation_Promo( |
| 37 | [ |
| 38 | 'textdomain' => 'advanced-ads', |
| 39 | 'plugin_name' => 'Advanced Ads', |
| 40 | 'hook' => 'advanced-ads-overview-below-support', |
| 41 | 'glotpress_logo' => false, // disables the plugin icon so we don’t need to keep up with potential changes. |
| 42 | ] |
| 43 | ) ); |
| 44 | |
| 45 | // show errors. |
| 46 | if ( Advanced_Ads_Ad_Health_Notices::notices_enabled() ) { |
| 47 | self::add_meta_box( 'advads_overview_notices', __( 'Notifications', 'advanced-ads' ), 'right', 'render_notices' ); |
| 48 | } |
| 49 | |
| 50 | self::add_meta_box( |
| 51 | 'advads_overview_news', |
| 52 | __( 'Next Steps', 'advanced-ads' ), |
| 53 | 'left', |
| 54 | 'render_next_steps' |
| 55 | ); |
| 56 | |
| 57 | self::add_meta_box( |
| 58 | 'advads_overview_support', |
| 59 | __( 'Manual & Support', 'advanced-ads' ), |
| 60 | 'right', |
| 61 | 'render_support' |
| 62 | ); |
| 63 | |
| 64 | if ( |
| 65 | Advanced_Ads_AdSense_Data::get_instance()->is_setup() && |
| 66 | ! Advanced_Ads_AdSense_Data::get_instance()->is_hide_stats() |
| 67 | ) { |
| 68 | $disable_link_markup = '<span class="advads-hndlelinks hndle"><a href="' . esc_url( admin_url( 'admin.php?page=advanced-ads-settings#top#adsense' ) ) . '" target="_blank">' . esc_attr__( 'Disable', 'advanced-ads' ) . '</a></span>'; |
| 69 | |
| 70 | self::add_meta_box( |
| 71 | 'advads_overview_adsense_stats', |
| 72 | __( 'AdSense Earnings', 'advanced-ads' ) . $disable_link_markup, |
| 73 | 'full', |
| 74 | 'render_adsense_stats' |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | // add widgets for pro add ons. |
| 79 | self::add_meta_box( 'advads_overview_addons', __( 'Add-Ons', 'advanced-ads' ), 'full', 'render_addons' ); |
| 80 | |
| 81 | do_action( 'advanced-ads-overview-widgets-after' ); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Loads a meta box into output |
| 86 | * |
| 87 | * @param string $id meta box ID. |
| 88 | * @param string $title title of the meta box. |
| 89 | * @param string $position context in which to show the box. |
| 90 | * @param callable $callback function that fills the box with the desired content. |
| 91 | */ |
| 92 | public static function add_meta_box( $id, $title, $position, $callback ) { |
| 93 | ob_start(); |
| 94 | call_user_func( [ 'Advanced_Ads_Overview_Widgets_Callbacks', $callback ] ); |
| 95 | do_action( 'advanced-ads-overview-widget-content-' . $id, $id ); |
| 96 | $content = ob_get_clean(); |
| 97 | |
| 98 | include ADVADS_ABSPATH . 'admin/views/overview-widget.php'; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Render Ad Health notices widget |
| 103 | */ |
| 104 | public static function render_notices() { |
| 105 | Advanced_Ads_Ad_Health_Notices::get_instance()->render_widget(); |
| 106 | ?> |
| 107 | <script>jQuery( document ).ready( function(){ advads_ad_health_maybe_remove_list(); });</script> |
| 108 | <?php |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Render next steps widget |
| 113 | */ |
| 114 | public static function render_next_steps() { |
| 115 | include ADVADS_ABSPATH . 'views/admin/widgets/aa-dashboard/next-steps/widget.php'; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Support widget |
| 120 | */ |
| 121 | public static function render_support() { |
| 122 | include ADVADS_ABSPATH . 'views/admin/widgets/aa-dashboard/support.php'; |
| 123 | do_action( 'advanced-ads-overview-below-support' ); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Adsense stats widget |
| 128 | */ |
| 129 | public static function render_adsense_stats() { |
| 130 | $report_type = 'domain'; |
| 131 | $report_filter = get_option( 'advanced-ads-adsense-dashboard-filter', '' ); |
| 132 | |
| 133 | if ( ! $report_filter ) { |
| 134 | $report_filter = self::get_site_domain(); |
| 135 | } |
| 136 | |
| 137 | if ( '*' === $report_filter ) { |
| 138 | $report_filter = ''; |
| 139 | } |
| 140 | |
| 141 | include ADVADS_ABSPATH . 'views/admin/metaboxes/ads/ad-gadsense-dashboard.php'; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * JavaScript loaded in AdSense stats widget. |
| 146 | * |
| 147 | * @param string $pub_id AdSense publisher ID. |
| 148 | * |
| 149 | * @return string |
| 150 | * @todo move to JS file. |
| 151 | */ |
| 152 | final public static function adsense_stats_js( $pub_id ) { |
| 153 | if ( self::$processed_adsense_stats_js ) { |
| 154 | return; |
| 155 | } |
| 156 | self::$processed_adsense_stats_js = true; |
| 157 | $nonce = self::get_adsense_dashboard_nonce(); |
| 158 | ?> |
| 159 | <script> |
| 160 | window.gadsenseData = window.gadsenseData || {}; |
| 161 | window.Advanced_Ads_Adsense_Report_Helper = window.Advanced_Ads_Adsense_Report_Helper || {}; |
| 162 | window.Advanced_Ads_Adsense_Report_Helper.nonce = '<?php echo esc_html( $nonce ); ?>'; |
| 163 | gadsenseData['pubId'] = '<?php echo esc_html( $pub_id ); ?>'; |
| 164 | </script> |
| 165 | <?php |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Return a nonce used in the AdSense stats widget. |
| 170 | * |
| 171 | * @return false|mixed|string |
| 172 | */ |
| 173 | final public static function get_adsense_dashboard_nonce() { |
| 174 | if ( ! self::$gadsense_dashboard_nonce ) { |
| 175 | self::$gadsense_dashboard_nonce = wp_create_nonce( 'advads-gadsense-dashboard' ); |
| 176 | } |
| 177 | return self::$gadsense_dashboard_nonce; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Extracts the domain from the site url |
| 182 | * |
| 183 | * @return string the domain, that was extracted from get_site_url() |
| 184 | */ |
| 185 | public static function get_site_domain() { |
| 186 | $site = get_site_url(); |
| 187 | preg_match( '|^([\d\w]+://)?([^/]+)|', $site, $matches ); |
| 188 | |
| 189 | return count( $matches ) > 1 ? $matches[2] : null; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * This method is called when the dashboard data is requested via ajax |
| 194 | * it prints the relevant data as json, then dies. |
| 195 | */ |
| 196 | public static function ajax_gadsense_dashboard() { |
| 197 | $post_data = wp_unslash( $_POST ); |
| 198 | if ( wp_verify_nonce( $post_data['nonce'], 'advads-gadsense-dashboard' ) === false ) { |
| 199 | wp_send_json_error( 'Unauthorized request', 401 ); |
| 200 | } |
| 201 | $report_type = in_array( $post_data['type'], [ 'domain', 'unit' ], true ) ? $post_data['type'] : false; |
| 202 | |
| 203 | if ( ! $report_type ) { |
| 204 | wp_send_json_error( 'Invalid arguments', 400 ); |
| 205 | } |
| 206 | |
| 207 | $report_filter = wp_strip_all_tags( $post_data['filter'] ); |
| 208 | $report = new Advanced_Ads_AdSense_Report( $report_type, $report_filter ); |
| 209 | |
| 210 | if ( $report->get_data()->is_valid() ) { |
| 211 | wp_send_json_success( [ 'html' => $report->get_markup() ] ); |
| 212 | } |
| 213 | |
| 214 | if ( $report->refresh_report() ) { |
| 215 | wp_send_json_success( [ 'html' => $report->get_markup() ] ); |
| 216 | } |
| 217 | |
| 218 | $error_message = $report->get_last_api_error(); |
| 219 | // Send markup with error info. |
| 220 | wp_send_json_success( [ 'html' => '<div class="error"><p>' . wp_kses_post( $error_message ) . '</p></div>' ] ); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Render stats box |
| 225 | * |
| 226 | * @param string $title title of the box. |
| 227 | * @param string $main main content. |
| 228 | * @param string $footer footer content. |
| 229 | * |
| 230 | * @deprecated ? |
| 231 | */ |
| 232 | final public static function render_stats_box( $title, $main, $footer ) { |
| 233 | ?> |
| 234 | <div class="advanced-ads-stats-box flex1"> |
| 235 | <?php echo $title; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 236 | <div class="advanced-ads-stats-box-main"> |
| 237 | <?php |
| 238 | // phpcs:ignore |
| 239 | echo $main; |
| 240 | ?> |
| 241 | </div> |
| 242 | <?php echo $footer; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 243 | </div> |
| 244 | <?php |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Add-ons box |
| 249 | * |
| 250 | * @param bool $hide_activated if true, hide activated add-ons. |
| 251 | * @param bool $is_dashboard whether it is used in the AA dashboard. |
| 252 | * |
| 253 | * @return void |
| 254 | */ |
| 255 | public static function render_addons( $hide_activated = false, $is_dashboard = true ) { |
| 256 | $box = new \AdvancedAds\Admin\Addon_Box( $hide_activated ); |
| 257 | $box->display( $is_dashboard ); |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Sort by installed add-ons |
| 262 | * |
| 263 | * @param array $a argument a. |
| 264 | * @param array $b argument b. |
| 265 | * |
| 266 | * @return int |
| 267 | */ |
| 268 | protected static function sort_by_order( $a, $b ) { |
| 269 | return $a['order'] - $b['order']; |
| 270 | } |
| 271 | } |
| 272 |