PluginProbe
Wp-Insert / 2.6.0
Wp-Insert v2.6.0
trunk 1.0 1.1 1.2 1.2.1 1.2.2 1.3.0 1.3.1 1.3.3 1.4 1.5.0 1.5.1 1.5.2 1.5.3 1.6.0 1.6.1 1.6.2 1.7 1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 2.0 All 63 releases
wp-insert / includes / modules / core / adstxt / adsense.php

adsense.php in Wp-Insert 2.6.0, at includes/modules/core/adstxt/adsense.php

245 lines 10.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /* Begin Admin Notice */
3 add_action( 'init', 'wp_insert_adstxt_adsense_admin_notice_reactivate' );
4 function wp_insert_adstxt_adsense_admin_notice_reactivate() {
5 if ( ! isset( $_GET['wp_insert_adstxt_adsense_reset'] ) ) {
6 return;
7 }
8 if ( ! current_user_can( 'manage_options' ) ) {
9 return;
10 }
11 $nonce = isset( $_GET['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ) : '';
12 if ( ! wp_verify_nonce( $nonce, 'wp_insert_adstxt_adsense_reset' ) ) {
13 return;
14 }
15 wp_insert_adstxt_adsense_admin_notice_reset();
16 wp_safe_redirect( admin_url( '/admin.php?page=wp-insert' ) );
17 exit;
18 }
19
20 function wp_insert_adstxt_adsense_admin_notice_reset() {
21 delete_option( 'wp_insert_adstxt_adsense_admin_notice_dismissed' );
22 delete_transient( 'wp_insert_adstxt_adsense_autocheck_content' );
23 }
24
25 add_action( 'admin_notices', 'wp_insert_adstxt_adsense_admin_notice' );
26 function wp_insert_adstxt_adsense_admin_notice() {
27 if ( current_user_can( 'manage_options' ) ) {
28 if ( ! get_option( 'wp_insert_adstxt_adsense_admin_notice_dismissed' ) ) {
29 $adstxtNewAdsenseEntries = get_transient( 'wp_insert_adstxt_adsense_autocheck_content' );
30 if ( $adstxtNewAdsenseEntries != '###CHECKED###' ) {
31 if ( $adstxtNewAdsenseEntries === false ) {
32 $adstxtNewAdsenseEntries = wp_insert_adstxt_adsense_get_status();
33 }
34 if ( $adstxtNewAdsenseEntries !== false ) {
35 set_transient( 'wp_insert_adstxt_adsense_autocheck_content', $adstxtNewAdsenseEntries, DAY_IN_SECONDS );
36 echo '<div class="notice notice-error wp_insert_adstxt_adsense_notice is-dismissible" style="padding: 15px;">';
37 echo '<p><b>Wp-Insert</b> had detected that your ads.txt file does not have all your Google Adsense Publisher IDs.<br />This will severely impact your adsense earnings and your immediate attention is required.</p>';
38 echo '<p>Your recommended google entries for ads.txt is as given below.<br />You can manually copy this to your ads.txt file or ';
39 $screen = get_current_screen();
40 if ( $screen->id != 'toplevel_page_wp-insert' ) {
41 echo '<a href="' . esc_url( admin_url( '/admin.php?page=wp-insert#wp_insert_adstxt_adsense_auto_update' ) ) . '">CLICK HERE</a>';
42 } else {
43 echo '<a href="javascript:;" onclick="wp_insert_adstxt_adsense_auto_update()">CLICK HERE</a>';
44 }
45 echo ' to instruct Wp-Insert to try and add the entries automatically.</p>';
46 echo '<p><code style="display: block; padding: 2px 10px;">' . implode( '<br />', array_map( 'esc_html', $adstxtNewAdsenseEntries ) ) . '</code></p>';
47 echo '<p><small><i><b>We recommend you not to dismiss this notice for continued daily ads.txt monitoring. This notice will stop appearing automatically once Wp-Insert detects correct entries in ads.txt (rechecked daily).</b></i></small></p>';
48 echo '<div class="clear"></div>';
49 echo '<input type="hidden" id="wp_insert_adstxt_adsense_admin_notice_nonce" name="wp_insert_adstxt_adsense_admin_notice_nonce" value="' . esc_attr( wp_create_nonce( 'wp-insert-adstxt-adsense-admin-notice' ) ) . '" />';
50 echo '<input type="hidden" id="wp_insert_adstxt_adsense_admin_notice_ajax" name="wp_insert_adstxt_adsense_admin_notice_ajax" value="' . esc_url( admin_url( 'admin-ajax.php' ) ) . '" />';
51 echo '</div>';
52 } else {
53 set_transient( 'wp_insert_adstxt_adsense_autocheck_content', '###CHECKED###', DAY_IN_SECONDS );
54 }
55 }
56 }
57 }
58 }
59
60 add_action( 'wp_ajax_wp_insert_adstxt_adsense_admin_notice_dismiss', 'wp_insert_adstxt_adsense_admin_notice_dismiss' );
61 function wp_insert_adstxt_adsense_admin_notice_dismiss() {
62 check_ajax_referer( 'wp-insert-adstxt-adsense-admin-notice', 'wp_insert_adstxt_adsense_admin_notice_nonce' );
63 update_option( 'wp_insert_adstxt_adsense_admin_notice_dismissed', 'true' );
64 wp_die();
65 }
66 /* End Admin Notice */
67
68 /* Begin Auto Update Ads.txt (Adsense) */
69 add_action( 'wp_ajax_wp_insert_adstxt_adsense_auto_update', 'wp_insert_adstxt_adsense_auto_update' );
70 function wp_insert_adstxt_adsense_auto_update() {
71 check_ajax_referer( 'wp-insert-adstxt-adsense-admin-notice', 'wp_insert_adstxt_adsense_admin_notice_nonce' );
72 $adstxtNewAdsenseEntries = wp_insert_adstxt_adsense_get_status();
73 if ( $adstxtNewAdsenseEntries !== false ) {
74 $adstxtContent = wp_insert_adstxt_get_content();
75 $adstxtContentData = array_filter( explode( "\n", trim( $adstxtContent ) ), 'trim' );
76 $adstxtUpdatedContent = array_filter( array_merge( $adstxtContentData, $adstxtNewAdsenseEntries ), 'trim' );
77 }
78
79 if ( isset( $adstxtUpdatedContent ) && is_array( $adstxtUpdatedContent ) && ( count( $adstxtUpdatedContent ) > 0 ) ) {
80 $adstxtUpdatedContent = implode( "\n", $adstxtUpdatedContent );
81 if ( wp_insert_adstxt_update_content( $adstxtUpdatedContent ) ) {
82 echo '###SUCCESS###';
83 } else {
84 wp_insert_echo_html( wp_insert_adstxt_updation_failed_message( $adstxtUpdatedContent ) );
85 }
86 }
87 wp_die();
88 }
89 /* End Auto Update Ads.txt (Adsense) */
90
91 /* Begin ads.txt Adsense Check */
92 function wp_insert_adstxt_adsense_get_status() {
93 if ( wp_insert_adstxt_file_exists() ) {
94 $adsensePublisherIds = wp_insert_adstxt_adsense_get_publisherids();
95 $adstxtContent = wp_insert_adstxt_get_content();
96 $adstxtContentData = array_filter( explode( "\n", trim( $adstxtContent ) ), 'trim' );
97 $adstxtExistingAdsenseEntries = [];
98 foreach ( $adstxtContentData as $line ) {
99 if ( strpos( $line, 'google.com' ) !== false ) {
100 $adstxtExistingAdsenseEntries[] = $line;
101 }
102 }
103
104 $adstxtNewAdsenseEntries = [];
105 if ( count( $adstxtExistingAdsenseEntries ) == 0 ) {
106 if ( is_array( $adsensePublisherIds ) && ( count( $adsensePublisherIds ) > 0 ) ) {
107 foreach ( $adsensePublisherIds as $adsensePublisherId ) {
108 $adstxtNewAdsenseEntries[] = 'google.com, ' . $adsensePublisherId . ', DIRECT, f08c47fec0942fa0';
109 }
110 }
111 } elseif ( is_array( $adsensePublisherIds ) && ( count( $adsensePublisherIds ) > 0 ) ) {
112 foreach ( $adsensePublisherIds as $adsensePublisherId ) {
113 $entryExists = false;
114 foreach ( $adstxtExistingAdsenseEntries as $adstxtExistingAdsenseEntry ) {
115 if ( strpos( $adstxtExistingAdsenseEntry, $adsensePublisherId ) !== false ) {
116 $entryExists = true;
117 }
118 }
119 if ( $entryExists == false ) {
120 $adstxtNewAdsenseEntries[] = 'google.com, ' . $adsensePublisherId . ', DIRECT, f08c47fec0942fa0';
121 }
122 }
123 }
124 }
125 if ( isset( $adstxtNewAdsenseEntries ) && count( $adstxtNewAdsenseEntries ) > 0 ) {
126 return $adstxtNewAdsenseEntries;
127 }
128 return false;
129 }
130 /* End ads.txt Adsense Check */
131
132 /* Begin Extract Publisher Ids from Ads */
133 function wp_insert_adstxt_adsense_get_publisherids() {
134 $adsensePublisherIds = [];
135 $data = get_option( 'wp_insert_inpostads' );
136 if ( isset( $data ) && is_array( $data ) ) {
137 foreach ( $data as $adUnit ) {
138 $temp = wp_insert_adstxt_adsense_extract_publisherids( $adUnit );
139 if ( $temp !== false ) {
140 $adsensePublisherIds = array_merge( $adsensePublisherIds, $temp );
141 }
142 }
143 }
144
145 $data = get_option( 'wp_insert_adwidgets' );
146 if ( isset( $data ) && is_array( $data ) ) {
147 foreach ( $data as $adUnit ) {
148 $temp = wp_insert_adstxt_adsense_extract_publisherids( $adUnit );
149 if ( $temp !== false ) {
150 $adsensePublisherIds = array_merge( $adsensePublisherIds, $temp );
151 }
152 }
153 }
154
155 $data = get_option( 'wp_insert_inthemeads' );
156 if ( isset( $data ) && is_array( $data ) ) {
157 foreach ( $data as $adUnit ) {
158 $temp = wp_insert_adstxt_adsense_extract_publisherids( $adUnit );
159 if ( $temp !== false ) {
160 $adsensePublisherIds = array_merge( $adsensePublisherIds, $temp );
161 }
162 }
163 }
164
165 $data = get_option( 'wp_insert_shortcodeads' );
166 if ( isset( $data ) && is_array( $data ) ) {
167 foreach ( $data as $adUnit ) {
168 $temp = wp_insert_adstxt_adsense_extract_publisherids( $adUnit );
169 if ( $temp !== false ) {
170 $adsensePublisherIds = array_merge( $adsensePublisherIds, $temp );
171 }
172 }
173 }
174
175 $data = get_option( 'wp_insert_pagelevelads' );
176 if ( isset( $data ) && is_array( $data ) ) {
177 foreach ( $data as $adUnit ) {
178 $temp = wp_insert_adstxt_adsense_extract_publisherids( $adUnit );
179 if ( $temp !== false ) {
180 $adsensePublisherIds = array_merge( $adsensePublisherIds, $temp );
181 }
182 }
183 }
184 $adsensePublisherIds = array_unique( $adsensePublisherIds );
185
186 if ( count( $adsensePublisherIds ) > 0 ) {
187 return $adsensePublisherIds;
188 }
189 return false;
190 }
191
192 function wp_insert_adstxt_adsense_extract_publisherids( $adUnit ) {
193 $publisherIds = [];
194 if ( isset( $adUnit['primary_ad_code'] ) && ( $adUnit['primary_ad_code'] != '' ) ) {
195 if ( preg_match( '/googlesyndication.com/', $adUnit['primary_ad_code'] ) ) {
196 if ( preg_match( '/data-ad-client=/', $adUnit['primary_ad_code'] ) ) { //ASYNS AD CODE
197 $adCodeParts = explode( 'data-ad-client', $adUnit['primary_ad_code'] );
198 } else {
199 $adCodeParts = explode( 'google_ad_client', $adUnit['primary_ad_code'] ); //ORDINARY AD CODE
200 }
201 if ( isset( $adCodeParts[1] ) && ( $adCodeParts[1] != '' ) ) {
202 preg_match( '#"([a-zA-Z0-9-\s]+)"#', stripslashes( $adCodeParts[1] ), $matches );
203 if ( isset( $matches[1] ) && ( $matches[1] != '' ) ) {
204 $publisherIds[] = str_replace( [ '"', ' ', 'ca-' ], [ '' ], $matches[1] );
205 }
206 }
207 }
208 }
209 if ( isset( $adUnit['secondary_ad_code'] ) && ( $adUnit['secondary_ad_code'] != '' ) ) {
210 if ( preg_match( '/googlesyndication.com/', $adUnit['secondary_ad_code'] ) ) {
211 if ( preg_match( '/data-ad-client=/', $adUnit['secondary_ad_code'] ) ) { //ASYNS AD CODE
212 $adCodeParts = explode( 'data-ad-client', $adUnit['secondary_ad_code'] );
213 } else {
214 $adCodeParts = explode( 'google_ad_client', $adUnit['secondary_ad_code'] ); //ORDINARY AD CODE
215 }
216 if ( isset( $adCodeParts[1] ) && ( $adCodeParts[1] != '' ) ) {
217 preg_match( '#"([a-zA-Z0-9-\s]+)"#', stripslashes( $adCodeParts[1] ), $matches );
218 if ( isset( $matches[1] ) && ( $matches[1] != '' ) ) {
219 $publisherIds[] = str_replace( [ '"', ' ', 'ca-' ], [ '' ], $matches[1] );
220 }
221 }
222 }
223 }
224 if ( isset( $adUnit['tertiary_ad_code'] ) && ( $adUnit['tertiary_ad_code'] != '' ) ) {
225 if ( preg_match( '/googlesyndication.com/', $adUnit['tertiary_ad_code'] ) ) {
226 if ( preg_match( '/data-ad-client=/', $adUnit['tertiary_ad_code'] ) ) { //ASYNS AD CODE
227 $adCodeParts = explode( 'data-ad-client', $adUnit['tertiary_ad_code'] );
228 } else {
229 $adCodeParts = explode( 'google_ad_client', $adUnit['tertiary_ad_code'] ); //ORDINARY AD CODE
230 }
231 if ( isset( $adCodeParts[1] ) && ( $adCodeParts[1] != '' ) ) {
232 preg_match( '#"([a-zA-Z0-9-\s]+)"#', stripslashes( $adCodeParts[1] ), $matches );
233 if ( isset( $matches[1] ) && ( $matches[1] != '' ) ) {
234 $publisherIds[] = str_replace( [ '"', ' ', 'ca-' ], [ '' ], $matches[1] );
235 }
236 }
237 }
238 }
239 if ( count( $publisherIds ) > 0 ) {
240 return $publisherIds;
241 }
242 return false;
243 }
244 /* End Extract Publisher Ids from Ads */
245