PluginProbe ʕ •ᴥ•ʔ
Advanced Ads – Ad Manager & AdSense / 2.0.26
Advanced Ads – Ad Manager & AdSense v2.0.26
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 1.41.0 1.42.0 1.42.1 1.42.2 1.43.0 1.44.0 1.44.1 1.45.0 1.45.1 1.46.0 1.47.0 1.47.1 1.47.2 1.47.3 1.47.4 1.47.5 1.48.0 1.48.1 1.49.0 1.5.0 1.5.0.1 1.5.1 1.5.2 1.5.2.1 1.5.4 1.5.4.1 1.5.5 1.50.0 1.51.0 1.51.1 1.51.2 1.51.3 1.52.0 1.52.1 1.52.2 1.52.3 1.52.4 1.53.0 1.53.1 1.53.2 1.54.0 1.54.1 1.55.0 1.56.0 1.56.1 1.56.2 1.56.3 1.56.4 1.6 1.6.1 1.6.10 1.6.10.1 1.6.10.2 1.6.11 1.6.11.1 1.6.12 1.6.13 1.6.14 1.6.15 1.6.16 1.6.17 1.6.17.1 1.6.17.2 1.6.2 1.6.2.1 1.6.3 1.6.4 1.6.4.1 1.6.5 1.6.6 1.6.6.1 1.6.7 1.6.7.1 1.6.8 1.6.8.1 1.6.8.2 1.6.8.3 1.6.9 1.6.9.1 1.6.9.2 1.6.9.3 1.6.9.4 1.7 1.7.0.1 1.7.0.2 1.7.0.3 1.7.1 1.7.1.1 1.7.1.2 1.7.1.3 1.7.1.4 1.7.1.5 1.7.10 trunk 1.7.11 1.0.1 1.7.12 1.0.2 1.7.13 1.0.3 1.7.14 1.1.0 1.7.15 1.1.1 1.7.16 1.1.2 1.7.17 1.1.3 1.7.18 1.10 1.7.19 1.10.1 1.7.2 1.10.10 1.7.2.1 1.10.11 1.7.20 1.10.12 1.7.21 1.10.2 1.7.22 1.10.3 1.7.23 1.10.4 1.7.24 1.10.5 1.7.25 1.10.6 1.7.3 1.10.7 1.7.4 1.10.8 1.7.4.1 1.10.9 1.7.4.2 1.11 1.7.4.3 1.11.1 1.7.4.4 1.11.2 1.7.4.5 1.12 1.7.5 1.13 1.7.5.1 1.13.1 1.7.6 1.13.2 1.7.7 1.13.3 1.7.8 1.13.4 1.7.9 1.13.5 1.7.9.1 1.13.6 1.7.9.2 1.13.7 1.7.9.3 1.13.8 1.8 1.14 1.8.1 1.14.1 1.8.10 1.14.10 1.8.11 1.14.11 1.8.12 1.14.2 1.8.13 1.14.3 1.8.14 1.14.4 1.8.15 1.14.5 1.8.16 1.14.6 1.8.17 1.14.7 1.8.18 1.14.8 1.8.19 1.14.9 1.8.2 1.15 1.8.20 1.16 1.8.21 1.16.1 1.8.22 1.17 1.8.23 1.17.1 1.8.24 1.17.10 1.8.25 1.17.10-rc.1 1.8.26 1.17.11 1.8.27 1.17.12 1.8.28 1.17.12-rc.1 1.8.29 1.17.2 1.8.3 1.17.3 1.8.30 1.17.4 1.8.4 1.17.5 1.8.5 1.17.6 1.8.6 1.17.7 1.8.7 1.17.8 1.8.8 1.17.9 1.8.9 1.17.9-beta.1 1.9 1.18.0 2.0.0 1.19.0 2.0.1 1.19.1 2.0.10 1.2 2.0.11 1.2.1 2.0.12 1.2.2 2.0.13 1.2.3 2.0.14 1.2.4 2.0.15 1.2.5 2.0.16 1.2.6 2.0.17 1.2.7 2.0.18 1.20.0 2.0.19 1.20.0-rc.1 2.0.2 1.20.0-rc.2 2.0.20 1.20.1 2.0.3 1.20.2 2.0.4 1.20.3 2.0.5 1.21.0 2.0.6 1.21.1 2.0.7 1.22.0 2.0.8 1.22.1 2.0.9 1.22.2 1.23.0 1.23.1 1.23.2 1.24.0 1.24.1 1.24.2 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.29.0 1.29.1 1.3 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.14 1.3.15 1.3.16 1.3.17 1.3.18 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.30.0 1.30.1 1.30.2 1.30.2-rc.1 1.30.3 1.30.4 1.30.4-rc.1 1.30.5 1.31.0 1.31.1 1.32.0 1.32.0-rc.1 1.33.0 1.33.1 1.33.2 1.34.0 1.35.0 1.35.1 1.36.0 1.36.1 1.36.2 1.36.3 1.37.0 1.37.1 1.37.2
advanced-ads / includes / importers / class-plugin-exporter.php
advanced-ads / includes / importers Last commit date
abstract-importer.php 5 months ago class-ad-inserter.php 3 weeks ago class-ads-wp-ads.php 3 weeks ago class-amp-wp-ads.php 3 weeks ago class-api-ads.php 3 weeks ago class-google-sheet.php 3 weeks ago class-manager.php 3 weeks ago class-plugin-exporter.php 3 weeks ago class-quick-adsense.php 3 weeks ago class-tutorials.php 1 year ago class-wp-quads.php 3 weeks ago class-xml-encoder.php 3 weeks ago class-xml-importer.php 3 weeks ago
class-plugin-exporter.php
391 lines
1 <?php
2 /**
3 * Plugin exporter.
4 *
5 * @package AdvancedAds
6 * @author Advanced Ads <info@wpadvancedads.com>
7 * @since 1.50.0
8 */
9
10 namespace AdvancedAds\Importers;
11
12 use Advanced_Ads_Ads_Txt_Strategy;
13 use Advanced_Ads_Privacy;
14 use AdvancedAds\Ads\Ad_Repository;
15 use AdvancedAds\Constants;
16 use AdvancedAds\Framework\Utilities\Params;
17 use AdvancedAds\Options;
18 use AdvancedAds\Utilities\Conditional;
19 use Exception;
20 use WP_Error;
21 use XML_Encoder;
22
23 defined( 'ABSPATH' ) || exit;
24
25 /**
26 * Plugin exporter.
27 *
28 * phpcs:disable WordPress.PHP.NoSilencedErrors.Discouraged
29 * phpcs:disable WordPress.PHP.DevelopmentFunctions.error_log_error_log
30 * phpcs:disable WordPress.PHP.DevelopmentFunctions.error_log_print_r
31 * phpcs:disable WordPress.PHP.DevelopmentFunctions.error_log_var_export
32 */
33 class Plugin_Exporter {
34 /**
35 * Hold data to make export file
36 *
37 * @var array<string, mixed>
38 */
39 public $data = [];
40
41 /**
42 * Types of content to be exported.
43 *
44 * @var array<string, mixed>
45 */
46 public $options = false;
47
48 /**
49 * Download export file
50 *
51 * @return array<string, mixed>|string|WP_Error
52 */
53 public function download_file() {
54 if ( ! Conditional::user_can( 'advanced_ads_manage_options' ) ) {
55 return new WP_Error( 'no_permission', __( 'User dont have premission to export.', 'advanced-ads' ) );
56 }
57
58 $this->options = Params::post( 'content', false, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY );
59 if ( empty( $this->options ) ) {
60 return new WP_Error( 'no_option_selected', __( 'No content option selected to export.', 'advanced-ads' ) );
61 }
62
63 $this->process();
64
65 if ( ! empty( $this->data ) ) {
66 if ( defined( 'IMPORT_DEBUG' ) && IMPORT_DEBUG ) {
67 error_log( print_r( 'Array to decode', true ) );
68 error_log( print_r( $this->data, true ) );
69 }
70
71 $filename = $this->get_filename();
72
73 try {
74 $encoded = XML_Encoder::get_instance()->encode(
75 $this->data,
76 [ 'encoding' => get_option( 'blog_charset' ) ]
77 );
78
79 header( 'Content-Description: File Transfer' );
80 header( 'Content-Disposition: attachment; filename=' . $filename );
81 header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), true );
82 echo $encoded; // phpcs:ignore
83
84 if ( defined( 'IMPORT_DEBUG' ) && IMPORT_DEBUG ) {
85 error_log( print_r( $encoded, true ) );
86 $decoded = XML_Encoder::get_instance()->decode( $encoded );
87 error_log( 'result ' . var_export( $this->data === $decoded, true ) );
88 }
89
90 exit();
91 } catch ( Exception $e ) {
92 return new WP_Error( 'error', $e->getMessage() );
93 }
94 }
95
96 return false;
97 }
98
99 /**
100 * Generate XML file
101 *
102 * @return void
103 */
104 private function process(): void {
105 @set_time_limit( 0 );
106 @ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) ); // phpcs:ignore WordPress.PHP.IniSet.memory_limit_Disallowed
107
108 foreach ( $this->options as $option ) {
109 $method = "process_{$option}";
110 if ( method_exists( $this, $method ) ) {
111 $this->$method();
112 }
113 }
114
115 do_action_ref_array( 'advanced-ads-export', [ $this->options, &$this->data ] );
116 }
117
118 /**
119 * Process ads
120 *
121 * @return void
122 */
123 private function process_ads(): void {
124 $ads = [];
125 $mime_types = $this->get_mime_types();
126 $search = '/' . preg_quote( home_url(), '/' ) . '(\S+?)\.(' . implode( '|', array_keys( $mime_types ) ) . ')/i';
127
128 $posts = $this->get_posts( Constants::POST_TYPE_AD );
129
130 if ( ! empty( $posts ) ) {
131 update_meta_cache( 'post', wp_list_pluck( $posts, 'ID' ) );
132 }
133
134 foreach ( $posts as $index => $post ) {
135 if ( ! empty( $post['post_content'] ) ) {
136 // Wrap images in <advads_import_img></advads_import_img> tags.
137 $post['post_content'] = preg_replace( $search, '<advads_import_img>\\0</advads_import_img>', $post['post_content'] );
138 }
139
140 if ( in_array( 'groups', $this->options, true ) ) {
141 $group_ids = get_post_meta( $post['ID'], Constants::AD_META_GROUP_IDS, true );
142
143 if ( is_array( $group_ids ) && ! empty( $group_ids ) ) {
144 $post['groups'] = [];
145 foreach ( $group_ids as $group_id ) {
146 $post['groups'][] = $this->get_group( (int) $group_id );
147 }
148 }
149 }
150
151 $this->get_post_meta( $post );
152
153 $ads[] = $post;
154 }
155
156 if ( $ads ) {
157 $this->data['ads'] = $ads;
158 }
159 }
160
161 /**
162 * Process placements
163 *
164 * @return void
165 */
166 private function process_placements(): void {
167 $placements = [];
168 $posts = $this->get_posts( Constants::POST_TYPE_PLACEMENT );
169 foreach ( $posts as $index => $post ) {
170 $this->get_post_meta( $post );
171
172 $placements[] = $post;
173 }
174
175 if ( $placements ) {
176 $this->data['placements'] = $placements;
177 }
178 }
179
180 /**
181 * Process groups
182 *
183 * @return void
184 */
185 private function process_groups(): void {
186 $this->data['groups'] = [];
187 foreach ( wp_advads_get_groups_dropdown() as $term_id => $name ) {
188 $this->data['groups'][] = $this->get_group( $term_id );
189 }
190 }
191
192 /**
193 * Process options
194 *
195 * @return void
196 */
197 private function process_options(): void {
198 /**
199 * Filters the list of options to be exported.
200 *
201 * @param $options An array of options
202 */
203 $this->data['options'] = array_filter(
204 apply_filters(
205 'advanced-ads-export-options',
206 [
207 ADVADS_SLUG => get_option( ADVADS_SLUG ),
208 GADSENSE_OPT_NAME => get_option( GADSENSE_OPT_NAME ),
209 Advanced_Ads_Privacy::OPTION_KEY => get_option( Advanced_Ads_Privacy::OPTION_KEY ),
210 Advanced_Ads_Ads_Txt_Strategy::OPTION => get_option( Advanced_Ads_Ads_Txt_Strategy::OPTION ),
211 Constants::OPTION_ADBLOCKER_SETTINGS => Options::instance()->get( 'adblocker', [] ),
212 ]
213 )
214 );
215 }
216
217 /**
218 * Get the filename
219 *
220 * @return string
221 */
222 private function get_filename(): string {
223 return sprintf(
224 '%s-advanced-ads-export-%s.xml',
225 sanitize_title(
226 preg_replace(
227 '#^(?:[^:]+:)?//(?:www\.)?([^/]+)#',
228 '$1',
229 get_bloginfo( 'url' )
230 )
231 ),
232 gmdate( 'Y-m-d' )
233 );
234 }
235
236 /**
237 * Get group info
238 *
239 * @param int $group_id Group id.
240 *
241 * @return array<string, mixed>
242 */
243 private function get_group( $group_id ): array {
244 static $advads_groups;
245 if ( null === $advads_groups ) {
246 $advads_groups = [];
247 }
248
249 if ( ! isset( $advads_groups[ $group_id ] ) ) {
250 $group = wp_advads_get_group( $group_id );
251
252 $advads_groups[ $group_id ] = [
253 'term_id' => $group->get_id(),
254 'slug' => $group->get_slug(),
255 'name' => $group->get_name(),
256 'type' => $group->get_type(),
257 'ad_count' => $group->get_ad_count(),
258 'options' => $group->get_options(),
259 'weight' => $group->get_ad_weights(),
260 ];
261 }
262
263 return $advads_groups[ $group_id ];
264 }
265
266 /**
267 * Get post IDs to export from cached summaries.
268 *
269 * @param string $post_type Post type to fetch.
270 *
271 * @return int[]
272 */
273 private function get_export_post_ids( $post_type ): array {
274 if ( Constants::POST_TYPE_AD === $post_type ) {
275 return array_map( 'absint', array_keys( wp_advads_get_ad_summaries() ) );
276 }
277
278 if ( Constants::POST_TYPE_PLACEMENT === $post_type ) {
279 return array_map( 'absint', array_keys( wp_advads_get_placement_summaries() ) );
280 }
281
282 return [];
283 }
284
285 /**
286 * Get posts for export
287 *
288 * @param string $post_type Post type to fetch.
289 *
290 * @return array<string, mixed>
291 */
292 private function get_posts( $post_type ): array {
293 global $wpdb;
294
295 $post_ids = $this->get_export_post_ids( $post_type );
296
297 if ( empty( $post_ids ) ) {
298 return [];
299 }
300
301 $export_fields = implode(
302 ', ',
303 [
304 'ID',
305 'post_date',
306 'post_date_gmt',
307 'post_content',
308 'post_title',
309 'post_password',
310 'post_name',
311 'post_status',
312 'post_modified',
313 'post_modified_gmt',
314 'guid',
315 ]
316 );
317
318 $placeholders = implode( ', ', array_fill( 0, count( $post_ids ), '%d' ) );
319
320 // phpcs:disable
321 return $wpdb->get_results(
322 $wpdb->prepare(
323 "SELECT $export_fields FROM {$wpdb->posts} WHERE ID IN ($placeholders) AND post_status NOT IN ('trash', 'auto-draft')",
324 ...$post_ids
325 ),
326 ARRAY_A
327 );
328 // phpcs:enable
329 }
330
331 /**
332 * Get mime types
333 *
334 * @return array<string, mixed>
335 */
336 private function get_mime_types(): array {
337 static $mime_types;
338
339 if ( null === $mime_types ) {
340 $mime_types = array_filter(
341 get_allowed_mime_types(),
342 function ( $mime_type ) {
343 return preg_match( '/image\//', $mime_type );
344 }
345 );
346 }
347
348 return $mime_types;
349 }
350
351 /**
352 * Get ads meta
353 *
354 * @param array<string, mixed> $post Post object array.
355 *
356 * @return void
357 */
358 private function get_post_meta( &$post ) {
359 global $wpdb;
360
361 $postmeta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->postmeta} WHERE post_id = %d", absint( $post['ID'] ) ) ); // phpcs:ignore
362 foreach ( $postmeta as $meta ) {
363 if ( '_edit_lock' === $meta->meta_key ) {
364 continue;
365 }
366
367 if ( Ad_Repository::OPTION_METAKEY === $meta->meta_key ) {
368 $image_id = false;
369 $ad_options = maybe_unserialize( $meta->meta_value );
370 if ( isset( $ad_options['output']['image_id'] ) ) {
371 $image_id = absint( $ad_options['output']['image_id'] );
372 }
373 if ( isset( $ad_options['image_id'] ) ) {
374 $image_id = absint( $ad_options['image_id'] );
375 }
376
377 if ( $image_id ) {
378 $atached_img = wp_get_attachment_url( $image_id );
379 if ( $atached_img ) {
380 $post['attached_img_url'] = $atached_img;
381 }
382 }
383
384 $post['meta_input'][ $meta->meta_key ] = maybe_unserialize( $ad_options );
385 } else {
386 $post['meta_input'][ $meta->meta_key ] = maybe_unserialize( $meta->meta_value );
387 }
388 }
389 }
390 }
391