export.php
233 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Export functionality. |
| 4 | */ |
| 5 | class Advanced_Ads_Export { |
| 6 | /** |
| 7 | * @var Advanced_Ads_Export |
| 8 | */ |
| 9 | private static $instance; |
| 10 | |
| 11 | /** |
| 12 | * Status messages |
| 13 | */ |
| 14 | private $messages = []; |
| 15 | |
| 16 | private function __construct() { |
| 17 | |
| 18 | $page_hook = 'admin_page_advanced-ads-import-export'; |
| 19 | // execute before headers are sent |
| 20 | add_action( 'load-' . $page_hook, [ $this, 'download_export_file' ] ); |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Return an instance of this class. |
| 25 | */ |
| 26 | public static function get_instance() { |
| 27 | if ( null == self::$instance ) { |
| 28 | self::$instance = new self; |
| 29 | } |
| 30 | return self::$instance; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Handle form submissions |
| 35 | */ |
| 36 | public function download_export_file() { |
| 37 | $action = Advanced_Ads_Admin::get_instance()->current_action(); |
| 38 | |
| 39 | if ( $action === 'export' ) { |
| 40 | if ( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_manage_options') ) ) { |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | check_admin_referer( 'advads-export' ); |
| 45 | |
| 46 | if ( isset( $_POST['content'] ) ) { |
| 47 | $this->process( $_POST['content'] ); |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Generate XML file. |
| 54 | * |
| 55 | * @param array $content Types of content to be exported. |
| 56 | */ |
| 57 | private function process( array $content ) { |
| 58 | global $wpdb; |
| 59 | |
| 60 | @set_time_limit( 0 ); |
| 61 | @ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) ); |
| 62 | |
| 63 | $export = []; |
| 64 | $advads_ad_groups = get_option( 'advads-ad-groups', [] ); |
| 65 | |
| 66 | if ( in_array( 'ads', $content ) ) { |
| 67 | $advads_ad_weights = get_option( 'advads-ad-weights', [] ); |
| 68 | |
| 69 | $ads = []; |
| 70 | $export_fields = implode( ', ', [ |
| 71 | 'ID', |
| 72 | 'post_date', |
| 73 | 'post_date_gmt', |
| 74 | 'post_content', |
| 75 | 'post_title', |
| 76 | 'post_password', |
| 77 | 'post_name', |
| 78 | 'post_status', |
| 79 | 'post_modified', |
| 80 | 'post_modified_gmt', |
| 81 | 'guid' |
| 82 | ] ); |
| 83 | |
| 84 | $posts = $wpdb->get_results( $wpdb->prepare( "SELECT $export_fields FROM {$wpdb->posts} where post_type = '%s' and post_status not in ('trash', 'auto-draft')", Advanced_Ads::POST_TYPE_SLUG ), ARRAY_A ); |
| 85 | |
| 86 | $mime_types = array_filter( get_allowed_mime_types(), function( $mime_type ) { |
| 87 | return preg_match( '/image\//', $mime_type ); |
| 88 | } ); |
| 89 | $search = '/' . preg_quote( home_url(), '/' ) . '(\S+?)\.(' . implode( '|', array_keys( $mime_types ) ) . ')/i'; |
| 90 | foreach ( $posts as $k => $post ) { |
| 91 | if ( ! empty( $post['post_content'] ) ) { |
| 92 | // wrap images in <advads_import_img></advads_import_img> tags |
| 93 | $post['post_content'] = preg_replace( $search, '<advads_import_img>\\0</advads_import_img>', $post['post_content'] ); |
| 94 | } |
| 95 | |
| 96 | $ads[$k] = $post; |
| 97 | |
| 98 | if ( in_array( 'groups', $content ) ) { |
| 99 | $terms = wp_get_object_terms( $post['ID'], 'advanced_ads_groups' ); |
| 100 | |
| 101 | foreach ( (array) $terms as $term ) { |
| 102 | $group_info = [ |
| 103 | 'term_id' => $term->term_id, |
| 104 | 'slug' => $term->slug, |
| 105 | 'name' => $term->name, |
| 106 | ]; |
| 107 | |
| 108 | if ( isset( $advads_ad_groups[ $term->term_id ] ) ) { |
| 109 | $group_info += $advads_ad_groups[ $term->term_id ]; |
| 110 | } |
| 111 | |
| 112 | if ( isset( $advads_ad_weights[ $term->term_id ][ $post['ID'] ] ) ) { |
| 113 | $group_info['weight'] = $advads_ad_weights[ $term->term_id ][ $post['ID'] ]; |
| 114 | } |
| 115 | |
| 116 | $ads[ $k ]['groups'][] = $group_info; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | $postmeta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->postmeta} WHERE post_id = %d", absint( $post['ID'] ) ) ); |
| 121 | |
| 122 | foreach ( $postmeta as $meta ) { |
| 123 | if ( $meta->meta_key === '_edit_lock' ) { |
| 124 | continue; |
| 125 | } |
| 126 | if ( $meta->meta_key === Advanced_Ads_Ad::$options_meta_field ) { |
| 127 | $ad_options = maybe_unserialize( $meta->meta_value ); |
| 128 | if ( isset( $ad_options['output']['image_id'] ) ) { |
| 129 | $image_id = absint( $ad_options['output']['image_id'] ); |
| 130 | if ( $atached_img = wp_get_attachment_url( $image_id) ) { |
| 131 | $ads[ $k ]['attached_img_url'] = $atached_img; |
| 132 | } |
| 133 | } |
| 134 | $ads[ $k ]['meta_input'][ $meta->meta_key ] = $ad_options; |
| 135 | } else { |
| 136 | $ads[ $k ]['meta_input'] [$meta->meta_key ] = $meta->meta_value; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | if ( $ads ) { |
| 142 | $export['ads'] = $ads; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | if ( in_array( 'groups', $content ) ) { |
| 147 | $terms = Advanced_Ads::get_instance()->get_model()->get_ad_groups(); |
| 148 | foreach ( $terms as $term ) { |
| 149 | $group_info = [ |
| 150 | 'term_id' => $term->term_id, |
| 151 | 'slug' => $term->slug, |
| 152 | 'name' => $term->name, |
| 153 | ]; |
| 154 | |
| 155 | if ( isset( $advads_ad_groups[ $term->term_id ] ) ) { |
| 156 | $group_info += $advads_ad_groups[ $term->term_id ]; |
| 157 | } |
| 158 | |
| 159 | $export['groups'][] = $group_info; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | if ( in_array( 'placements', $content ) ) { |
| 164 | $placements = Advanced_Ads::get_instance()->get_model()->get_ad_placements_array(); |
| 165 | |
| 166 | // prevent nodes starting with number |
| 167 | foreach ( $placements as $key => &$placement ) { |
| 168 | $placement['key'] = $key; |
| 169 | } |
| 170 | |
| 171 | $export['placements'] = array_values( $placements ); |
| 172 | } |
| 173 | |
| 174 | if ( in_array( 'options', $content, true ) ) { |
| 175 | /** |
| 176 | * Filters the list of options to be exported. |
| 177 | * |
| 178 | * @param $options An array of options |
| 179 | */ |
| 180 | $export['options'] = array_filter( apply_filters( 'advanced-ads-export-options', [ |
| 181 | ADVADS_SLUG => get_option( ADVADS_SLUG ), |
| 182 | GADSENSE_OPT_NAME => get_option( GADSENSE_OPT_NAME ), |
| 183 | Advanced_Ads_Privacy::OPTION_KEY => get_option( Advanced_Ads_Privacy::OPTION_KEY ), |
| 184 | Advanced_Ads_Ads_Txt_Strategy::OPTION => get_option( Advanced_Ads_Ads_Txt_Strategy::OPTION ), |
| 185 | ] ) ); |
| 186 | } |
| 187 | |
| 188 | do_action_ref_array( 'advanced-ads-export', [ $content, &$export ] ); |
| 189 | |
| 190 | if ( $export ) { |
| 191 | if ( defined( 'IMPORT_DEBUG' ) && IMPORT_DEBUG ) { |
| 192 | error_log( print_r( 'Array to decode', true ) ); |
| 193 | error_log( print_r( $export, true) ); |
| 194 | } |
| 195 | |
| 196 | // add the root domain and the current date to the filename. |
| 197 | $filename = sprintf( |
| 198 | '%s-advanced-ads-export-%s.xml', |
| 199 | sanitize_title( preg_replace( |
| 200 | '#^(?:[^:]+:)?//(?:www\.)?([^/]+)#', |
| 201 | '$1', |
| 202 | get_bloginfo( 'url' ) |
| 203 | ) ), |
| 204 | gmdate( 'Y-m-d' ) |
| 205 | ); |
| 206 | |
| 207 | try { |
| 208 | $encoded = Advanced_Ads_XmlEncoder::get_instance()->encode( $export, [ 'encoding' => get_option( 'blog_charset' ) ] ); |
| 209 | |
| 210 | header( 'Content-Description: File Transfer' ); |
| 211 | header( 'Content-Disposition: attachment; filename=' . $filename ); |
| 212 | header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), true ); |
| 213 | echo $encoded; |
| 214 | |
| 215 | if ( defined( 'IMPORT_DEBUG' ) && IMPORT_DEBUG ) { |
| 216 | error_log( print_r( $encoded, true ) ); |
| 217 | $decoded = Advanced_Ads_XmlEncoder::get_instance()->decode( $encoded ); |
| 218 | error_log( 'result ' . var_export( $export === $decoded , true ) ); |
| 219 | } |
| 220 | |
| 221 | exit(); |
| 222 | |
| 223 | } catch ( Exception $e ) { |
| 224 | $this->messages[] = [ 'error', $e->getMessage() ]; |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | public function get_messages(){ |
| 230 | return $this->messages; |
| 231 | } |
| 232 | } |
| 233 |