ads
3 months ago
groups
3 months ago
metaboxes
1 year ago
pages
3 months ago
placements
2 months ago
class-action-links.php
1 year ago
class-addon-box.php
1 year ago
class-addon-updater.php
3 months ago
class-admin-menu.php
3 months ago
class-admin-notices.php
1 year ago
class-ajax.php
3 months ago
class-assets.php
3 months ago
class-authors.php
1 year ago
class-compatibility.php
1 year ago
class-edd-updater.php
3 months ago
class-list-filters.php
2 months ago
class-marketing.php
1 year ago
class-metabox-ad-settings.php
1 year ago
class-metabox-ad.php
1 year ago
class-misc.php
1 year ago
class-page-quick-edit.php
1 year ago
class-plugin-installer.php
1 year ago
class-post-list.php
1 year ago
class-post-types.php
3 months ago
class-screen-options.php
3 months ago
class-settings.php
1 year ago
class-shortcode-creator.php
1 year ago
class-system-info.php
1 year ago
class-tinymce.php
2 years ago
class-translation-promo.php
1 year ago
class-upgrades.php
1 year ago
class-version-control.php
3 months ago
class-welcome.php
1 year ago
class-wordpress-dashboard.php
1 year ago
index.php
2 years ago
class-misc.php
280 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Uncategorised functionality. |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | * @since 1.x.x |
| 8 | */ |
| 9 | |
| 10 | namespace AdvancedAds\Admin; |
| 11 | |
| 12 | use AdvancedAds\Constants; |
| 13 | use Advanced_Ads_Admin_Notices; |
| 14 | use AdvancedAds\Utilities\Conditional; |
| 15 | use AdvancedAds\Importers\XML_Importer; |
| 16 | use AdvancedAds\Framework\Utilities\Params; |
| 17 | use AdvancedAds\Framework\Interfaces\Integration_Interface; |
| 18 | |
| 19 | defined( 'ABSPATH' ) || exit; |
| 20 | |
| 21 | /** |
| 22 | * Class Misc |
| 23 | */ |
| 24 | class Misc implements Integration_Interface { |
| 25 | |
| 26 | /** |
| 27 | * Hook into WordPress. |
| 28 | * |
| 29 | * @return void |
| 30 | */ |
| 31 | public function hooks(): void { |
| 32 | add_filter( 'gettext', [ $this, 'replace_cheating_message' ], 20, 2 ); |
| 33 | add_filter( 'get_user_option_user-settings', [ $this, 'reset_view_mode_option' ] ); |
| 34 | add_action( 'in_admin_header', [ $this, 'register_admin_notices' ] ); |
| 35 | add_action( 'plugins_api_result', [ $this, 'recommend_suitable_add_ons' ], 11, 3 ); |
| 36 | add_action( 'admin_action_advanced_ads_starter_setup', [ $this, 'import_starter_setup' ] ); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Replace 'You need a higher level of permission.' message if user role does not have required permissions. |
| 41 | * |
| 42 | * @param string $translated_text Translated text. |
| 43 | * @param string $untranslated_text Text to translate. |
| 44 | * |
| 45 | * @return string $translation Translated text. |
| 46 | */ |
| 47 | public function replace_cheating_message( $translated_text, $untranslated_text ): string { |
| 48 | global $typenow; |
| 49 | |
| 50 | if ( |
| 51 | isset( $typenow ) |
| 52 | && 'You need a higher level of permission.' === $untranslated_text |
| 53 | && Constants::POST_TYPE_AD === $typenow |
| 54 | ) { |
| 55 | $translated_text = __( 'You don’t have access to ads. Please deactivate and re-enable Advanced Ads again to fix this.', 'advanced-ads' ) |
| 56 | . ' <a href="https://wpadvancedads.com/manual/user-capabilities/?utm_source=advanced-ads&utm_medium=link&utm_campaign=wrong-user-role#You_dont_have_access_to_ads" target="_blank">' . __( 'Get help', 'advanced-ads' ) . '</a>'; |
| 57 | } |
| 58 | |
| 59 | return (string) $translated_text; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Set the removed post list mode to "List", if it was set to "Excerpt". |
| 64 | * |
| 65 | * @param string $user_options Query string containing user options. |
| 66 | * |
| 67 | * @return string |
| 68 | */ |
| 69 | public function reset_view_mode_option( $user_options ): string { |
| 70 | return str_replace( '&posts_list_mode=excerpt', '&posts_list_mode=list', $user_options ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Registers Advanced Ads admin notices |
| 75 | * prevents other notices from showing up on our own pages |
| 76 | * |
| 77 | * @return void |
| 78 | */ |
| 79 | public function register_admin_notices(): void { |
| 80 | /** |
| 81 | * Remove all registered admin_notices from AA screens |
| 82 | * we need to use this or some users have half or more of their viewports cluttered with unrelated notices |
| 83 | */ |
| 84 | if ( Conditional::is_screen_advanced_ads() ) { |
| 85 | remove_all_actions( 'admin_notices' ); |
| 86 | } |
| 87 | |
| 88 | add_action( 'admin_notices', [ $this, 'admin_notices' ] ); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Initiate the admin notices class |
| 93 | * |
| 94 | * @return void |
| 95 | */ |
| 96 | public function admin_notices(): void { |
| 97 | // Display ad block warning to everyone who can edit ads. |
| 98 | if ( |
| 99 | Conditional::user_can( 'advanced_ads_edit_ads' ) |
| 100 | && Conditional::is_screen_advanced_ads() |
| 101 | ) { |
| 102 | $ad_blocker_notice_id = wp_advads()->get_frontend_prefix() . 'abcheck-' . md5( microtime() ); |
| 103 | wp_register_script( $ad_blocker_notice_id . '-adblocker-notice', false, [], ADVADS_VERSION, true ); |
| 104 | wp_enqueue_script( $ad_blocker_notice_id . '-adblocker-notice' ); |
| 105 | wp_add_inline_script( |
| 106 | $ad_blocker_notice_id . '-adblocker-notice', |
| 107 | "jQuery( document ).ready( function () { |
| 108 | if ( typeof advanced_ads_adblocker_test === 'undefined' ) { |
| 109 | jQuery( '#" . esc_attr( $ad_blocker_notice_id ) . ".message' ).show(); |
| 110 | } |
| 111 | } );" |
| 112 | ); |
| 113 | include_once ADVADS_ABSPATH . 'admin/views/notices/adblock.php'; |
| 114 | } |
| 115 | |
| 116 | // Show success notice after starter setup was imported. Registered here because it will be visible only once. |
| 117 | if ( 'advanced-ads-starter-setup-success' === Params::get( 'message' ) ) { |
| 118 | add_action( 'advanced-ads-admin-notices', [ $this, 'starter_setup_success_message' ] ); |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | Register our own notices on Advanced Ads pages, except |
| 123 | -> the overview page where they should appear in the notices section, |
| 124 | -> revision page to prevent duplicate revision controls. |
| 125 | */ |
| 126 | $screen = get_current_screen(); |
| 127 | if ( |
| 128 | Conditional::user_can( 'advanced_ads_edit_ads' ) |
| 129 | && ( ! isset( $screen->id ) || 'toplevel_page_advanced-ads' !== $screen->id ) |
| 130 | && 'revision' !== $screen->id |
| 131 | ) { |
| 132 | |
| 133 | echo '<div class="wrap">'; |
| 134 | Advanced_Ads_Admin_Notices::get_instance()->display_notices(); |
| 135 | |
| 136 | // Allow other Advanced Ads plugins to show admin notices at this late stage. |
| 137 | do_action( 'advanced-ads-admin-notices' ); |
| 138 | echo '</div>'; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Show success message after starter setup was created. |
| 144 | * |
| 145 | * @return void |
| 146 | */ |
| 147 | public function starter_setup_success_message(): void { |
| 148 | $last_post = get_posts( [ 'numberposts' => 1 ] ); |
| 149 | $last_post_link = isset( $last_post[0]->ID ) ? get_permalink( $last_post[0]->ID ) : false; |
| 150 | |
| 151 | include ADVADS_ABSPATH . 'admin/views/notices/starter-setup-success.php'; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Recommend additional add-ons |
| 156 | * |
| 157 | * @param object|WP_Error $result Response object or WP_Error. |
| 158 | * @param string $action The type of information being requested from the Plugin Installation API. |
| 159 | * @param object $args Plugin API arguments. |
| 160 | * |
| 161 | * @return object|WP_Error Response object or WP_Error. |
| 162 | */ |
| 163 | public function recommend_suitable_add_ons( $result, $action, $args ) { |
| 164 | if ( |
| 165 | empty( $args->browse ) |
| 166 | || ! in_array( $args->browse, [ 'featured', 'recommended', 'popular' ], true ) |
| 167 | || ( isset( $result->info['page'] ) && $result->info['page'] > 1 ) |
| 168 | ) { |
| 169 | return $result; |
| 170 | } |
| 171 | |
| 172 | // Grab all slugs from the api results. |
| 173 | $result_slugs = wp_list_pluck( $result->plugins, 'slug' ); |
| 174 | |
| 175 | // Recommend AdSense In-Feed add-on. |
| 176 | $result = $this->recommend_plugin( |
| 177 | 'advanced-ads-adsense-in-feed', |
| 178 | 'advanced-ads-adsense-in-feed/advanced-ads-in-feed.php', |
| 179 | $args, |
| 180 | $result, |
| 181 | $result_slugs |
| 182 | ); |
| 183 | |
| 184 | // Recommend Genesis Ads add-on. |
| 185 | if ( defined( 'PARENT_THEME_NAME' ) && 'Genesis' === PARENT_THEME_NAME ) { |
| 186 | $result = $this->recommend_plugin( |
| 187 | 'advanced-ads-genesis', |
| 188 | 'advanced-ads-genesis/genesis-ads.php', |
| 189 | $args, |
| 190 | $result, |
| 191 | $result_slugs |
| 192 | ); |
| 193 | } |
| 194 | |
| 195 | // Recommend WP Bakery (former Visual Composer) add-on. |
| 196 | if ( defined( 'WPB_VC_VERSION' ) ) { |
| 197 | $result = $this->recommend_plugin( |
| 198 | 'ads-for-visual-composer', |
| 199 | 'ads-for-visual-composer/advanced-ads-vc.php', |
| 200 | $args, |
| 201 | $result, |
| 202 | $result_slugs |
| 203 | ); |
| 204 | } |
| 205 | |
| 206 | return $result; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Import a starter setup for new users |
| 211 | * |
| 212 | * @return void |
| 213 | */ |
| 214 | public function import_starter_setup(): void { |
| 215 | if ( |
| 216 | 'advanced_ads_starter_setup' !== Params::get( 'action' ) |
| 217 | || ! Conditional::user_can( 'advanced_ads_edit_ads' ) |
| 218 | ) { |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | check_admin_referer( 'advanced-ads-starter-setup' ); |
| 223 | |
| 224 | $xml = file_get_contents( ADVADS_ABSPATH . 'admin/assets/xml/starter-setup.xml' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 225 | |
| 226 | ( new XML_Importer() )->import_content( $xml ); |
| 227 | |
| 228 | // Redirect to the ads overview page. |
| 229 | wp_safe_redirect( admin_url( 'edit.php?post_type=advanced_ads&message=advanced-ads-starter-setup-success' ) ); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Recommends a plugin based on the provided slug and file path. |
| 234 | * |
| 235 | * This function checks if the plugin is already active or recommended. If not, |
| 236 | * it fetches the plugin data using the WordPress Plugin API and adds it to the result. |
| 237 | * |
| 238 | * @param string $slug The slug of the plugin to recommend. |
| 239 | * @param string $file_path The file path of the plugin. |
| 240 | * @param object $args Additional arguments for the recommendation. |
| 241 | * @param object $result The current result object containing recommended plugins. |
| 242 | * @param array $result_slugs An array of slugs of already recommended plugins. |
| 243 | * |
| 244 | * @return object The updated result object with the recommended plugin added. |
| 245 | */ |
| 246 | private function recommend_plugin( $slug, $file_path, $args, $result, $result_slugs ) { |
| 247 | // Check if the plugin is already active or recommended. |
| 248 | if ( |
| 249 | is_plugin_active( $file_path ) |
| 250 | || is_plugin_active_for_network( $file_path ) |
| 251 | || in_array( $slug, $result_slugs, true ) |
| 252 | ) { |
| 253 | return $result; |
| 254 | } |
| 255 | |
| 256 | // Prepare query arguments to fetch plugin data. |
| 257 | $query_args = [ |
| 258 | 'slug' => $slug, |
| 259 | 'fields' => [ |
| 260 | 'icons' => true, |
| 261 | 'active_installs' => true, |
| 262 | 'short_description' => true, |
| 263 | 'group' => true, |
| 264 | ], |
| 265 | ]; |
| 266 | $plugin_data = plugins_api( 'plugin_information', $query_args ); |
| 267 | |
| 268 | // Add plugin data to the result if fetched successfully. |
| 269 | if ( ! is_wp_error( $plugin_data ) ) { |
| 270 | if ( 'featured' === $args->browse ) { |
| 271 | array_push( $result->plugins, $plugin_data ); |
| 272 | } else { |
| 273 | array_unshift( $result->plugins, $plugin_data ); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | return $result; |
| 278 | } |
| 279 | } |
| 280 |