PluginProbe ʕ •ᴥ•ʔ
Advanced Ads – Ad Manager & AdSense / 2.0.25
Advanced Ads – Ad Manager & AdSense v2.0.25
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 3 months ago class-ad-inserter.php 3 months ago class-ads-wp-ads.php 3 months ago class-amp-wp-ads.php 3 months ago class-api-ads.php 1 year ago class-google-sheet.php 5 months ago class-manager.php 1 year ago class-plugin-exporter.php 1 week ago class-quick-adsense.php 1 year ago class-tutorials.php 1 year ago class-wp-quads.php 1 year ago class-xml-encoder.php 1 year ago class-xml-importer.php 5 days ago
class-plugin-exporter.php
392 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 WP_Error;
13 use Exception;
14 use XML_Encoder;
15 use AdvancedAds\Options;
16 use Advanced_Ads_Privacy;
17 use AdvancedAds\Constants;
18 use Advanced_Ads_Ads_Txt_Strategy;
19 use AdvancedAds\Ads\Ad_Repository;
20 use AdvancedAds\Utilities\Conditional;
21 use AdvancedAds\Framework\Utilities\Params;
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
38 */
39 public $data = [];
40
41 /**
42 * Types of content to be exported.
43 *
44 * @var array
45 */
46 public $options = false;
47
48 /**
49 * Download export file
50 *
51 * @return array|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
92 } catch ( Exception $e ) {
93 return new WP_Error( 'error', $e->getMessage() );
94 }
95 }
96
97 return false;
98 }
99
100 /**
101 * Generate XML file
102 *
103 * @return void
104 */
105 private function process(): void {
106 @set_time_limit( 0 );
107 @ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) ); // phpcs:ignore WordPress.PHP.IniSet.memory_limit_Disallowed
108
109 foreach ( $this->options as $option ) {
110 $method = "process_{$option}";
111 if ( method_exists( $this, $method ) ) {
112 $this->$method();
113 }
114 }
115
116 do_action_ref_array( 'advanced-ads-export', [ $this->options, &$this->data ] );
117 }
118
119 /**
120 * Process ads
121 *
122 * @return void
123 */
124 private function process_ads(): void {
125 $ads = [];
126 $mime_types = $this->get_mime_types();
127 $search = '/' . preg_quote( home_url(), '/' ) . '(\S+?)\.(' . implode( '|', array_keys( $mime_types ) ) . ')/i';
128
129 $posts = $this->get_posts( Constants::POST_TYPE_AD );
130
131 if ( ! empty( $posts ) ) {
132 update_meta_cache( 'post', wp_list_pluck( $posts, 'ID' ) );
133 }
134
135 foreach ( $posts as $index => $post ) {
136 if ( ! empty( $post['post_content'] ) ) {
137 // Wrap images in <advads_import_img></advads_import_img> tags.
138 $post['post_content'] = preg_replace( $search, '<advads_import_img>\\0</advads_import_img>', $post['post_content'] );
139 }
140
141 if ( in_array( 'groups', $this->options, true ) ) {
142 $group_ids = get_post_meta( $post['ID'], Constants::AD_META_GROUP_IDS, true );
143
144 if ( is_array( $group_ids ) && ! empty( $group_ids ) ) {
145 $post['groups'] = [];
146 foreach ( $group_ids as $group_id ) {
147 $post['groups'][] = $this->get_group( (int) $group_id );
148 }
149 }
150 }
151
152 $this->get_post_meta( $post );
153
154 $ads[] = $post;
155 }
156
157 if ( $ads ) {
158 $this->data['ads'] = $ads;
159 }
160 }
161
162 /**
163 * Process placements
164 *
165 * @return void
166 */
167 private function process_placements(): void {
168 $placements = [];
169 $posts = $this->get_posts( Constants::POST_TYPE_PLACEMENT );
170 foreach ( $posts as $index => $post ) {
171 $this->get_post_meta( $post );
172
173 $placements[] = $post;
174 }
175
176 if ( $placements ) {
177 $this->data['placements'] = $placements;
178 }
179 }
180
181 /**
182 * Process groups
183 *
184 * @return void
185 */
186 private function process_groups(): void {
187 $this->data['groups'] = [];
188 foreach ( wp_advads_get_groups_dropdown() as $term_id => $name ) {
189 $this->data['groups'][] = $this->get_group( $term_id );
190 }
191 }
192
193 /**
194 * Process options
195 *
196 * @return void
197 */
198 private function process_options(): void {
199 /**
200 * Filters the list of options to be exported.
201 *
202 * @param $options An array of options
203 */
204 $this->data['options'] = array_filter(
205 apply_filters(
206 'advanced-ads-export-options',
207 [
208 ADVADS_SLUG => get_option( ADVADS_SLUG ),
209 GADSENSE_OPT_NAME => get_option( GADSENSE_OPT_NAME ),
210 Advanced_Ads_Privacy::OPTION_KEY => get_option( Advanced_Ads_Privacy::OPTION_KEY ),
211 Advanced_Ads_Ads_Txt_Strategy::OPTION => get_option( Advanced_Ads_Ads_Txt_Strategy::OPTION ),
212 Constants::OPTION_ADBLOCKER_SETTINGS => Options::instance()->get( 'adblocker', [] ),
213 ]
214 )
215 );
216 }
217
218 /**
219 * Get the filename
220 *
221 * @return string
222 */
223 private function get_filename(): string {
224 return sprintf(
225 '%s-advanced-ads-export-%s.xml',
226 sanitize_title(
227 preg_replace(
228 '#^(?:[^:]+:)?//(?:www\.)?([^/]+)#',
229 '$1',
230 get_bloginfo( 'url' )
231 )
232 ),
233 gmdate( 'Y-m-d' )
234 );
235 }
236
237 /**
238 * Get group info
239 *
240 * @param int $group_id Group id.
241 *
242 * @return array
243 */
244 private function get_group( $group_id ): array {
245 static $advads_groups;
246 if ( null === $advads_groups ) {
247 $advads_groups = [];
248 }
249
250 if ( ! isset( $advads_groups[ $group_id ] ) ) {
251 $group = wp_advads_get_group( $group_id );
252
253 $advads_groups[ $group_id ] = [
254 'term_id' => $group->get_id(),
255 'slug' => $group->get_slug(),
256 'name' => $group->get_name(),
257 'type' => $group->get_type(),
258 'ad_count' => $group->get_ad_count(),
259 'options' => $group->get_options(),
260 'weight' => $group->get_ad_weights(),
261 ];
262 }
263
264 return $advads_groups[ $group_id ];
265 }
266
267 /**
268 * Get post IDs to export from cached summaries.
269 *
270 * @param string $post_type Post type to fetch.
271 *
272 * @return int[]
273 */
274 private function get_export_post_ids( $post_type ): array {
275 if ( Constants::POST_TYPE_AD === $post_type ) {
276 return array_map( 'absint', array_keys( wp_advads_get_ad_summaries() ) );
277 }
278
279 if ( Constants::POST_TYPE_PLACEMENT === $post_type ) {
280 return array_map( 'absint', array_keys( wp_advads_get_placement_summaries() ) );
281 }
282
283 return [];
284 }
285
286 /**
287 * Get posts for export
288 *
289 * @param string $post_type Post type to fetch.
290 *
291 * @return array
292 */
293 private function get_posts( $post_type ): array {
294 global $wpdb;
295
296 $post_ids = $this->get_export_post_ids( $post_type );
297
298 if ( empty( $post_ids ) ) {
299 return [];
300 }
301
302 $export_fields = implode(
303 ', ',
304 [
305 'ID',
306 'post_date',
307 'post_date_gmt',
308 'post_content',
309 'post_title',
310 'post_password',
311 'post_name',
312 'post_status',
313 'post_modified',
314 'post_modified_gmt',
315 'guid',
316 ]
317 );
318
319 $placeholders = implode( ', ', array_fill( 0, count( $post_ids ), '%d' ) );
320
321 // phpcs:disable
322 return $wpdb->get_results(
323 $wpdb->prepare(
324 "SELECT $export_fields FROM {$wpdb->posts} WHERE ID IN ($placeholders) AND post_status NOT IN ('trash', 'auto-draft')",
325 ...$post_ids
326 ),
327 ARRAY_A
328 );
329 // phpcs:enable
330 }
331
332 /**
333 * Get mime types
334 *
335 * @return array
336 */
337 private function get_mime_types(): array {
338 static $mime_types;
339
340 if ( null === $mime_types ) {
341 $mime_types = array_filter(
342 get_allowed_mime_types(),
343 function ( $mime_type ) {
344 return preg_match( '/image\//', $mime_type );
345 }
346 );
347 }
348
349 return $mime_types;
350 }
351
352 /**
353 * Get ads meta
354 *
355 * @param array $post Post object array.
356 *
357 * @return void
358 */
359 private function get_post_meta( &$post ) {
360 global $wpdb;
361
362 $postmeta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->postmeta} WHERE post_id = %d", absint( $post['ID'] ) ) ); // phpcs:ignore
363 foreach ( $postmeta as $meta ) {
364 if ( '_edit_lock' === $meta->meta_key ) {
365 continue;
366 }
367
368 if ( Ad_Repository::OPTION_METAKEY === $meta->meta_key ) {
369 $image_id = false;
370 $ad_options = maybe_unserialize( $meta->meta_value );
371 if ( isset( $ad_options['output']['image_id'] ) ) {
372 $image_id = absint( $ad_options['output']['image_id'] );
373 }
374 if ( isset( $ad_options['image_id'] ) ) {
375 $image_id = absint( $ad_options['image_id'] );
376 }
377
378 if ( $image_id ) {
379 $atached_img = wp_get_attachment_url( $image_id );
380 if ( $atached_img ) {
381 $post['attached_img_url'] = $atached_img;
382 }
383 }
384
385 $post['meta_input'][ $meta->meta_key ] = maybe_unserialize( $ad_options );
386 } else {
387 $post['meta_input'][ $meta->meta_key ] = maybe_unserialize( $meta->meta_value );
388 }
389 }
390 }
391 }
392