PluginProbe
Advanced Ads – Ad Manager & AdSense / 2.0.22
Advanced Ads – Ad Manager & AdSense v2.0.22
2.0.26 2.0.25 2.0.24 2.0.23 2.0.22 2.0.21 1.38.0 1.39.0 1.39.1 1.39.2 1.39.3 1.39.4 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.40.0 1.40.1 1.40.2 All 348 releases
advanced-ads / includes / admin / class-assets.php
class-assets.php
199 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Assets manages the enqueuing of styles and scripts for the administration area.
4 *
5 * @package AdvancedAds
6 * @author Advanced Ads <info@wpadvancedads.com>
7 * @since 1.47.0
8 */
9
10 namespace AdvancedAds\Admin;
11
12 use AdvancedAds\Constants;
13 use Advanced_Ads_AdSense_Admin;
14 use Advanced_Ads_Display_Conditions;
15 use AdvancedAds\Utilities\Conditional;
16 use AdvancedAds\Framework\Interfaces\Integration_Interface;
17
18 defined( 'ABSPATH' ) || exit;
19
20 /**
21 * Admin Assets.
22 */
23 class Assets implements Integration_Interface {
24
25 /**
26 * Hook into WordPress.
27 *
28 * @return void
29 */
30 public function hooks(): void {
31 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_styles' ], 10, 0 );
32 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ], 9, 0 );
33 }
34
35 /**
36 * Enqueue styles
37 *
38 * @return void
39 */
40 public function enqueue_styles(): void {
41 $wp_screen = get_current_screen();
42
43 // Bail if we should bail.
44 if ( $this->should_bail() ) {
45 return;
46 }
47
48 // TODO: made them load conditionaly.
49 if ( 'dashboard' !== $wp_screen->id ) {
50 wp_advads()->registry->enqueue_style( 'ui' );
51 wp_advads()->registry->enqueue_style( 'admin' );
52 }
53
54 if ( Conditional::is_screen_advanced_ads() ) {
55 wp_advads()->registry->enqueue_style( 'notifications' );
56 }
57 }
58
59 /**
60 * Enqueue scripts
61 *
62 * @return void
63 */
64 public function enqueue_scripts(): void {
65 global $post;
66
67 $screen = get_current_screen();
68 $this->enqueue_endpoints();
69 $this->enqueue_site_info();
70 $this->enqueue_i18n();
71
72 // Bail if we should bail.
73 if ( $this->should_bail() ) {
74 return;
75 }
76
77 // TODO: add conditional loading.
78 wp_advads()->registry->enqueue_script( 'admin-global' );
79 wp_advads()->registry->enqueue_script( 'commands' );
80
81 $params = [
82 'ajax_nonce' => wp_create_nonce( 'advanced-ads-admin-ajax-nonce' ),
83 'create_ad_url' => esc_url( admin_url( 'post-new.php?post_type=advanced_ads' ) ),
84 'create_your_first_ad' => __( 'Create your first ad', 'advanced-ads' ),
85 ];
86 wp_advads_json_add( $params, 'advadsglobal' );
87
88 // TODO: remove later start using global data variable.
89 wp_advads_json_add( 'ajax_nonce', wp_create_nonce( 'advanced-ads-admin-ajax-nonce' ), 'advadsglobal' );
90
91 if ( Conditional::is_screen_advanced_ads() ) {
92 wp_advads()->registry->enqueue_script( 'admin' );
93 wp_advads()->registry->enqueue_script( 'conditions' );
94 wp_advads()->registry->enqueue_script( 'adblocker-image-data' );
95 wp_advads()->registry->enqueue_script( 'notifications-center' );
96
97 $translation_array = [
98 'condition_or' => __( 'or', 'advanced-ads' ),
99 'condition_and' => __( 'and', 'advanced-ads' ),
100 'after_paragraph_promt' => __( 'After which paragraph?', 'advanced-ads' ),
101 'page_level_ads_enabled' => Advanced_Ads_AdSense_Admin::get_auto_ads_messages()['enabled'],
102 'today' => __( 'Today', 'advanced-ads' ),
103 'yesterday' => __( 'Yesterday', 'advanced-ads' ),
104 'this_month' => __( 'This Month', 'advanced-ads' ),
105 /* translators: 1: The number of days. */
106 'last_n_days' => __( 'Last %1$d days', 'advanced-ads' ),
107 /* translators: 1: An error message. */
108 'error_message' => __( 'An error occurred: %1$s', 'advanced-ads' ),
109 'all' => __( 'All', 'advanced-ads' ),
110 'active' => __( 'Active', 'advanced-ads' ),
111 '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' ),
112 'show_inactive_ads' => __( 'Show inactive ads', 'advanced-ads' ),
113 'hide_inactive_ads' => __( 'Hide inactive ads', 'advanced-ads' ),
114 'display_conditions_form_name' => Advanced_Ads_Display_Conditions::FORM_NAME, // not meant for translation.
115 'close' => __( 'Close', 'advanced-ads' ),
116 'close_save' => __( 'Close and save', 'advanced-ads' ),
117 'confirmation' => __( 'Data you have entered has not been saved. Are you sure you want to discard your changes?', 'advanced-ads' ),
118 'admin_page' => $screen->id,
119 'placements_allowed_ads' => [
120 'action' => 'advads_placements_allowed_ads',
121 'nonce' => wp_create_nonce( 'advads-create-new-placement' ),
122 ],
123 ];
124
125 // TODO: remove later start using global data variable.
126 wp_advads_json_add( $translation_array, 'advadstxt' );
127 }
128
129 if ( Constants::POST_TYPE_AD === $screen->id ) {
130 wp_enqueue_media( [ 'post' => $post ] );
131 }
132
133 // Ad edit screen.
134 if ( 'post' === $screen->base && Constants::POST_TYPE_AD === $screen->post_type ) {
135 wp_advads()->registry->enqueue_script( 'ad-positioning' );
136 }
137
138 if ( in_array( $screen->id, [ 'edit-post', 'edit-page' ], true ) && current_user_can( 'edit_posts' ) ) {
139 wp_advads()->registry->enqueue_script( 'page-quick-edit' );
140 wp_advads_json_add( 'page_quick_edit', [ 'nonce' => wp_create_nonce( 'advads-post-quick-edit' ) ] );
141 }
142 }
143
144 /**
145 * Global variables: advancedAds
146 */
147 private function enqueue_site_info() {
148 $endpoints = [
149 'blogId' => get_current_blog_id(),
150 'homeUrl' => get_home_url(),
151 ];
152
153 wp_advads_json_add( 'siteInfo', $endpoints );
154 }
155
156 /**
157 * Global variables: advancedAds
158 */
159 private function enqueue_endpoints() {
160 $endpoints = [
161 'adminUrl' => esc_url( admin_url( '/' ) ),
162 'ajaxUrl' => esc_url( admin_url( 'admin-ajax.php' ) ),
163 'assetsUrl' => esc_url( ADVADS_BASE_URL ),
164 'editAd' => esc_url( admin_url( 'post.php?action=edit&post=' ) ),
165 ];
166
167 wp_advads_json_add( 'endpoints', $endpoints );
168 }
169
170 /**
171 * Localize i18n strings
172 *
173 * @return void
174 */
175 private function enqueue_i18n() {
176 $data = [
177 'btnCloseLabel' => __( 'Close', 'advanced-ads' ),
178 'searchResultsLabel' => __( 'Showing search results for', 'advanced-ads' ),
179 ];
180
181 wp_advads_json_add( 'i18n', $data );
182 }
183
184 /**
185 * Check if we should bail from enqueueing assets.
186 *
187 * @return bool
188 */
189 private function should_bail(): bool {
190 $wp_screen = get_current_screen();
191
192 $bail_screens = [
193 'advanced-ads_page_advanced-ads-tools',
194 ];
195
196 return in_array( $wp_screen->id, $bail_screens, true );
197 }
198 }
199