import.php
667 lines
| 1 | <?php |
| 2 | class Advanced_Ads_Import { |
| 3 | /** |
| 4 | * @var Advanced_Ads_Export |
| 5 | */ |
| 6 | private static $instance; |
| 7 | |
| 8 | /** |
| 9 | * uploaded XML file path |
| 10 | */ |
| 11 | private $import_id; |
| 12 | |
| 13 | /** |
| 14 | * status messages |
| 15 | */ |
| 16 | private $messages = array(); |
| 17 | |
| 18 | /** |
| 19 | * imported data mapped with previous data, e.g. ['ads'][ new_ad_id => old_ad_id (or null if does not exist) ] |
| 20 | */ |
| 21 | public $imported_data = array( |
| 22 | 'ads' => array(), |
| 23 | 'groups' => array(), |
| 24 | 'placements' => array() |
| 25 | ); |
| 26 | |
| 27 | /** |
| 28 | * Created groups during this import session ['slug' => 'id'] |
| 29 | */ |
| 30 | private $created_groups = array(); |
| 31 | |
| 32 | /** |
| 33 | * attachments, created for Image Ads and images in ad content |
| 34 | */ |
| 35 | private $created_attachments = array(); |
| 36 | |
| 37 | private function __construct() {} |
| 38 | |
| 39 | /** |
| 40 | * Return an instance of this class. |
| 41 | */ |
| 42 | public static function get_instance() { |
| 43 | if ( null == self::$instance ) { |
| 44 | self::$instance = new self; |
| 45 | } |
| 46 | return self::$instance; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Manages stages of the XML import process |
| 51 | */ |
| 52 | public function dispatch() { |
| 53 | if ( ! isset( $_POST['_wpnonce'] ) |
| 54 | || ! wp_verify_nonce( $_POST['_wpnonce'], 'advads-import' ) |
| 55 | || ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_manage_options') ) ) { |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | if ( ! isset( $_POST['import_type'] ) ) { |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | switch ( $_POST['import_type'] ) { |
| 64 | case 'xml_content': |
| 65 | if ( empty( $_POST['xml_textarea'] ) ) { |
| 66 | $this->messages[] = array( 'error', __( 'Please enter XML content', 'advanced-ads' ) ); |
| 67 | return; |
| 68 | } |
| 69 | // see wp_magic_quotes() |
| 70 | $this->import( stripslashes( $_POST['xml_textarea'] ) ); |
| 71 | break; |
| 72 | case 'xml_file': |
| 73 | if ( $this->handle_upload() ) { |
| 74 | $content = file_get_contents( $this->import_id ); |
| 75 | $this->import( $content ); |
| 76 | @unlink( $this->import_id ); |
| 77 | } |
| 78 | break; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * The main controller for the actual import stage |
| 84 | * |
| 85 | * @param string $xml_content |
| 86 | */ |
| 87 | public function import( &$xml_content ) { |
| 88 | @set_time_limit( 0 ); |
| 89 | @ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) ); |
| 90 | |
| 91 | if ( defined( 'IMPORT_DEBUG' ) && IMPORT_DEBUG ) { |
| 92 | error_log( 'source XML:' ); |
| 93 | error_log( $xml_content ); |
| 94 | } |
| 95 | |
| 96 | try { |
| 97 | $decoded = Advanced_Ads_XmlEncoder::get_instance()->decode( $xml_content ); |
| 98 | } catch ( Exception $e ) { |
| 99 | error_log( $e->getMessage() ); |
| 100 | $this->messages[] = array ( 'error', $e->getMessage() ); |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | if ( defined( 'IMPORT_DEBUG' ) && IMPORT_DEBUG ) { |
| 105 | error_log( 'decoded XML:' ); |
| 106 | error_log(print_r($decoded, true)); |
| 107 | } |
| 108 | |
| 109 | $this->import_ads_and_groups( $decoded ); |
| 110 | $this->import_empty_groups( $decoded ); |
| 111 | $this->import_placements( $decoded ); |
| 112 | $this->import_options( $decoded ); |
| 113 | |
| 114 | do_action_ref_array( 'advanced-ads-import', array( &$decoded, &$this->imported_data, &$this->messages ) ); |
| 115 | |
| 116 | wp_cache_flush(); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Create new ads and groups based on import information |
| 121 | * |
| 122 | * @param array $decoded decoded XML |
| 123 | */ |
| 124 | private function import_ads_and_groups( &$decoded ) { |
| 125 | if ( isset( $decoded['ads'] ) && is_array( $decoded['ads'] ) ) { |
| 126 | foreach ( $decoded['ads'] as $k => $ad ) { |
| 127 | $ad_title = isset( $ad['post_title'] ) ? $ad['post_title'] : ''; |
| 128 | $ad_date = isset( $ad['post_date'] ) ? $ad['post_date'] : ''; |
| 129 | |
| 130 | if ( isset( $ad['meta_input'] ) && is_array( $ad['meta_input'] ) ) { |
| 131 | foreach ( $ad['meta_input'] as $meta_k => &$meta_v ) { |
| 132 | if ( $meta_k !== Advanced_Ads_Ad::$options_meta_field ) { |
| 133 | $meta_v = maybe_unserialize( $meta_v ); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // upload images for Image ad ad type |
| 139 | if ( isset( $ad['attached_img_url'] ) && isset( $ad['meta_input']['advanced_ads_ad_options']['output']['image_id'] ) ) { |
| 140 | $attached_img_url = $this->replace_placeholders( $ad['attached_img_url'] ); |
| 141 | $attachment_id = null; |
| 142 | |
| 143 | if ( isset( $this->created_attachments[ $attached_img_url ] ) ) { |
| 144 | $attachment_id = $this->created_attachments[ $attached_img_url ]['post_id']; |
| 145 | } else if ( $attachment = $this->upload_image_from_url( $attached_img_url ) ) { |
| 146 | $link = ( $link = get_attachment_link( $attachment['post_id'] ) ) ? sprintf( '<a href="%s">%s</a>', esc_url( $link ), __( 'Edit', 'advanced-ads' ) ) : ''; |
| 147 | $this->messages[] = array( 'update', sprintf( __( 'New attachment created <em>%s</em> %s', 'advanced-ads' ), $attachment['post_id'], $link ) ); |
| 148 | $this->created_attachments[ $attached_img_url ] = $attachment; |
| 149 | $attachment_id = $attachment['post_id']; |
| 150 | } |
| 151 | |
| 152 | if ( $attachment_id ) { |
| 153 | $ad['meta_input']['advanced_ads_ad_options']['output']['image_id'] = $attachment_id; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | $insert_ad = array( |
| 158 | 'post_title' => $ad_title, |
| 159 | 'post_date' => $ad_date, |
| 160 | 'post_date_gmt' => isset( $ad['post_date_gmt'] ) ? $ad['post_date_gmt'] : '', |
| 161 | 'post_content' => isset( $ad['post_content'] ) ? $this->process_ad_content( $ad['post_content'] ) : '', |
| 162 | 'post_password' => isset( $ad['post_password'] ) ? $ad['post_password'] : '', |
| 163 | 'post_name' => isset( $ad['post_name'] ) ? $ad['post_name'] : '', |
| 164 | 'post_status' => isset( $ad['post_status'] ) ? $ad['post_status'] : 'publish', |
| 165 | 'post_modified' => isset( $ad['post_modified'] ) ? $ad['post_modified'] : '', |
| 166 | 'post_modified_gmt' => isset( $ad['post_modified_gmt'] ) ? $ad['post_modified_gmt'] : '', |
| 167 | 'guid' => isset( $ad['guid'] ) ? $ad['guid'] :'', |
| 168 | 'post_author' => get_current_user_id(), |
| 169 | 'post_type' => Advanced_Ads::POST_TYPE_SLUG, |
| 170 | 'comment_status' => 'closed', |
| 171 | 'ping_status' => 'closed', |
| 172 | 'meta_input' => isset( $ad['meta_input'] ) ? $ad['meta_input'] : '', |
| 173 | ); |
| 174 | |
| 175 | |
| 176 | $post_id = wp_insert_post( $insert_ad, true ); |
| 177 | |
| 178 | if ( is_wp_error( $post_id ) ) { |
| 179 | $this->messages[] = array( 'error', sprintf( __( 'Failed to import <em>%s</em>', 'advanced-ads' ), esc_html($ad['post_title'] ) ) ); |
| 180 | if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG ) { |
| 181 | $this->messages[] = array( 'error', ' > ' . $post_id->get_error_message() ); |
| 182 | } |
| 183 | |
| 184 | continue; |
| 185 | } else { |
| 186 | $link = ( $link = get_edit_post_link( $post_id ) ) ? sprintf( '<a href="%s">%s</a>', esc_url( $link ), __( 'Edit', 'advanced-ads' ) ) : ''; |
| 187 | $this->messages[] = array( 'update', sprintf( __( 'New ad created: <em>%s</em> %s', 'advanced-ads' ), $post_id, $link ) ); |
| 188 | } |
| 189 | |
| 190 | // new ad id => old ad id, if exists |
| 191 | $this->imported_data['ads'][ $post_id ] = isset( $ad['ID'] ) ? absint( $ad['ID'] ) : null; |
| 192 | |
| 193 | // import ad groups |
| 194 | if ( ! empty( $ad['groups'] ) && is_array( $ad['groups'] ) ) { |
| 195 | $groups_to_set = array(); |
| 196 | $advads_ad_groups = get_option( 'advads-ad-groups', array() ); |
| 197 | $advads_ad_weights = get_option( 'advads-ad-weights', array() ); |
| 198 | |
| 199 | foreach ( $ad['groups'] as $_group ) { |
| 200 | if ( ! $group_id = $this->create_group_term( $_group ) ) { |
| 201 | continue; |
| 202 | } |
| 203 | |
| 204 | $ad_group_id = null; |
| 205 | if ( isset( $ad['meta_input']['advanced_ads_ad_options']['output']['group_id'] ) ) { |
| 206 | $ad_group_id = $ad['meta_input']['advanced_ads_ad_options']['output']['group_id']; |
| 207 | } |
| 208 | |
| 209 | // do not save the ad group, if this is the group assigned as ad content |
| 210 | if ( $ad_group_id !== $group_id ) { |
| 211 | $groups_to_set[] = intval( $group_id ); |
| 212 | } |
| 213 | |
| 214 | if ( ! isset( $advads_ad_groups[ $group_id ] ) ) { |
| 215 | $advads_ad_groups[ $group_id ] = array ( |
| 216 | 'type' => isset( $_group['type']) ? $_group['type'] : 'default', |
| 217 | 'ad_count' => isset($_group['ad_count']) ? $_group['ad_count'] : 1, |
| 218 | 'options' => isset($_group['options']) ? $_group['options'] : array() |
| 219 | ); |
| 220 | |
| 221 | update_option( 'advads-ad-groups', $advads_ad_groups ); |
| 222 | } |
| 223 | |
| 224 | $ad_weight = isset( $_group['weight'] ) ? absint( $_group['weight'] ) : Advanced_Ads_Group::MAX_AD_GROUP_DEFAULT_WEIGHT; |
| 225 | $advads_ad_weights[ $group_id ][ $post_id ] = $ad_weight; |
| 226 | } |
| 227 | |
| 228 | update_option( 'advads-ad-weights', $advads_ad_weights ); |
| 229 | |
| 230 | $this->messages[] = array( 'update', sprintf( __( 'Assigned terms: <em>%s</em>, to post: <em>%s</em>', 'advanced-ads' ), implode(',',$groups_to_set), $post_id ) ); |
| 231 | |
| 232 | $tt_ids = wp_set_post_terms( $post_id, $groups_to_set, Advanced_Ads::AD_GROUP_TAXONOMY ); |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Create new empty groups based on import information |
| 240 | * |
| 241 | * @param array $decoded decoded XML |
| 242 | */ |
| 243 | private function import_empty_groups( &$decoded ) { |
| 244 | if ( isset( $decoded['groups'] ) && is_array( $decoded['groups'] ) ) { |
| 245 | $advads_ad_groups = get_option( 'advads-ad-groups', array() ); |
| 246 | |
| 247 | foreach ( $decoded['groups'] as $_group ) { |
| 248 | if ( $group_id = $this->create_group_term( $_group ) ) { |
| 249 | if ( ! isset( $advads_ad_groups[ $group_id ] ) ) { |
| 250 | $advads_ad_groups[ $group_id ] = array ( |
| 251 | 'type' => isset( $_group['type']) ? $_group['type'] : 'default', |
| 252 | 'ad_count' => isset( $_group['ad_count'] ) ? $_group['ad_count'] : 1, |
| 253 | 'options' => isset( $_group['options'] ) ? $_group['options'] : array() |
| 254 | ); |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | update_option( 'advads-ad-groups', $advads_ad_groups ); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Create new group term if it haven't already been created |
| 264 | * |
| 265 | * @param array $_group decoded XML |
| 266 | * @return int group_id, false on failure |
| 267 | */ |
| 268 | private function create_group_term( $_group ) { |
| 269 | if ( empty( $_group['slug'] ) || empty( $_group['name'] ) ) { |
| 270 | return false; |
| 271 | } |
| 272 | |
| 273 | $slug = $original_slug = $_group['slug']; |
| 274 | |
| 275 | if ( isset( $this->created_groups[ $original_slug ] ) ) { |
| 276 | return $this->created_groups[ $slug ]; |
| 277 | } |
| 278 | |
| 279 | if ( term_exists( $slug, Advanced_Ads::AD_GROUP_TAXONOMY ) ) { |
| 280 | $count = 1; |
| 281 | while ( term_exists( $slug . '_' . $count, Advanced_Ads::AD_GROUP_TAXONOMY ) ) { |
| 282 | $count++; |
| 283 | } |
| 284 | $slug = $slug . '_' . $count; |
| 285 | } |
| 286 | |
| 287 | $t = wp_insert_term( $_group['name'], Advanced_Ads::AD_GROUP_TAXONOMY, array( 'slug' => $slug) ); |
| 288 | |
| 289 | if ( ! is_wp_error( $t ) ) { |
| 290 | $this->created_groups[ $original_slug ] = $t['term_id']; |
| 291 | $group_id = $t['term_id']; |
| 292 | $this->messages[] = array( 'update', sprintf( __( 'New group created, id: <em>%s</em>, name: <em>%s</em>', 'advanced-ads' ), $group_id, esc_html( $_group['name'] ) ) ); |
| 293 | } else { |
| 294 | $this->messages[] = array( 'error', sprintf( __( 'Failed to import taxonomy: <em>%s</em>, term: <em>%s</em>', 'advanced-ads' ), esc_html(Advanced_Ads::AD_GROUP_TAXONOMY), esc_html($_group['name'] ) ) ); |
| 295 | if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG ) { |
| 296 | $this->messages[] = array( 'error', ' > ' . $t->get_error_message() ); |
| 297 | } |
| 298 | |
| 299 | return false; |
| 300 | } |
| 301 | |
| 302 | // new group id => old group id, if exists |
| 303 | $this->imported_data['groups'][ $group_id ] = isset( $_group['term_id'] ) ? absint( $_group['term_id'] ) : null; |
| 304 | |
| 305 | return $group_id; |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Create new placements based on import information |
| 310 | * |
| 311 | * @param array $decoded decoded XML |
| 312 | */ |
| 313 | private function import_placements( &$decoded ) { |
| 314 | if ( isset( $decoded['placements'] ) && is_array( $decoded['placements'] ) ) { |
| 315 | |
| 316 | $existing_placements = $updated_placements = Advanced_Ads::get_instance()->get_model()->get_ad_placements_array(); |
| 317 | $placement_types = Advanced_Ads_Placements::get_placement_types(); |
| 318 | |
| 319 | foreach ( $decoded['placements'] as &$placement ) { |
| 320 | $use_existing = ! empty( $placement['use_existing'] ); |
| 321 | |
| 322 | // use existing placement |
| 323 | if ( $use_existing ) { |
| 324 | if ( empty( $placement['key'] ) ) { |
| 325 | continue; |
| 326 | } |
| 327 | |
| 328 | $placement_key_uniq = sanitize_title( $placement['key'] ); |
| 329 | if ( ! isset( $existing_placements[ $placement_key_uniq ] ) ) { |
| 330 | continue; |
| 331 | } |
| 332 | |
| 333 | $existing_placement = $existing_placements[ $placement_key_uniq ]; |
| 334 | $existing_placement['key'] = $placement_key_uniq; |
| 335 | |
| 336 | // create new placement |
| 337 | } else { |
| 338 | if ( empty( $placement['key'] ) || empty( $placement['name'] ) || empty( $placement['type'] ) ) { |
| 339 | continue; |
| 340 | } |
| 341 | |
| 342 | $placement_key_uniq = sanitize_title( $placement['key'] ); |
| 343 | if ( $placement_key_uniq === '' ) { |
| 344 | continue; |
| 345 | } |
| 346 | |
| 347 | $placement['type'] = ( isset( $placement_types[ $placement['type'] ] ) ) ? $placement['type'] : 'default'; |
| 348 | $placement['name'] = esc_attr( $placement['name'] ); |
| 349 | |
| 350 | // make sure the key in placement array is unique |
| 351 | if ( isset( $existing_placements[ $placement_key_uniq ] ) ) { |
| 352 | $count = 1; |
| 353 | while ( isset( $existing_placements[ $placement_key_uniq . '_' . $count ] ) ) { |
| 354 | $count++; |
| 355 | } |
| 356 | $placement_key_uniq .= '_' . $count; |
| 357 | } |
| 358 | |
| 359 | $this->messages[] = array( 'update', sprintf( __( 'Placement <em>%s</em> created', 'advanced-ads' ), esc_html( $placement['name'] ) ) ); |
| 360 | |
| 361 | // new placement key => old placement key |
| 362 | $this->imported_data['placements'][ $placement_key_uniq ] = $placement['key']; |
| 363 | } |
| 364 | |
| 365 | // try to set "Item" (ad or group) |
| 366 | if ( ! empty( $placement['item'] ) ) { |
| 367 | $_item = explode( '_', $placement['item'] ); |
| 368 | if ( ! empty( $_item[1] ) ) { |
| 369 | switch ( $_item[0] ) { |
| 370 | case 'ad': |
| 371 | case Advanced_Ads_Select::AD : |
| 372 | |
| 373 | $found = $this->search_item( $_item[1], Advanced_Ads_Select::AD ); |
| 374 | if ( $found === false ) { break; } |
| 375 | |
| 376 | if ( $use_existing ) { |
| 377 | // assign new ad to an existing placement |
| 378 | // - if the placement has no or a single ad assigned, it will be swapped against the new one |
| 379 | // - if a group is assigned to the placement, the new ad will be added to this group with a weight of 1 |
| 380 | $placement = $existing_placement; |
| 381 | |
| 382 | if ( ! empty( $placement['item'] ) ) { |
| 383 | // get the item from the existing placement |
| 384 | $_item_existing = explode( '_', $placement['item'] ); |
| 385 | |
| 386 | if ( ! empty( $_item_existing[1] ) && $_item_existing[0] === Advanced_Ads_Select::GROUP ) { |
| 387 | $advads_ad_weights = get_option( 'advads-ad-weights', array() ); |
| 388 | |
| 389 | if ( term_exists( absint( $_item_existing[1] ), Advanced_Ads::AD_GROUP_TAXONOMY ) ) { |
| 390 | wp_set_post_terms( $found, $_item_existing[1], Advanced_Ads::AD_GROUP_TAXONOMY, true ); |
| 391 | $advads_ad_weights[ $_item_existing[1] ][ $found ] = 1; |
| 392 | update_option( 'advads-ad-weights', $advads_ad_weights ); |
| 393 | // new placement key => old placement key |
| 394 | $this->imported_data['placements'][ $placement_key_uniq ] = $placement_key_uniq; |
| 395 | break; |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | $placement['item'] = 'ad_' . $found; |
| 402 | // new placement key => old placement key |
| 403 | $this->imported_data['placements'][ $placement_key_uniq ] = $placement_key_uniq; |
| 404 | break; |
| 405 | case Advanced_Ads_Select::GROUP : |
| 406 | $found = $this->search_item( $_item[1], Advanced_Ads_Select::GROUP ); |
| 407 | if ( $found === false ) { break; } |
| 408 | |
| 409 | $placement['item'] = 'group_' . $found; |
| 410 | // new placement key => old placement key |
| 411 | $this->imported_data['placements'][ $placement_key_uniq ] = $placement_key_uniq; |
| 412 | break; |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | $updated_placements[ $placement_key_uniq ] = apply_filters( 'advanced-ads-import-placement', $placement, $this ); |
| 418 | } |
| 419 | |
| 420 | if ( $existing_placements !== $updated_placements ) { |
| 421 | Advanced_Ads::get_instance()->get_model()->update_ad_placements_array( $updated_placements ); |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * Search for ad/group id |
| 428 | * |
| 429 | * @param string $id ad/group id |
| 430 | * @param string $type |
| 431 | * @return |
| 432 | * - int id of the imported ad/group if exists |
| 433 | * - or int id of the existing ad/group if exists |
| 434 | * - or bool false |
| 435 | */ |
| 436 | public function search_item( $id, $type ) { |
| 437 | $found = false; |
| 438 | |
| 439 | switch ( $type ) { |
| 440 | case 'ad': |
| 441 | case Advanced_Ads_Select::AD : |
| 442 | // if the ad was was imported |
| 443 | if ( ! $found = array_search( $id, $this->imported_data['ads'] ) ) { |
| 444 | // if the ad already exists |
| 445 | if ( get_post_type( $id ) === Advanced_Ads::POST_TYPE_SLUG ) { |
| 446 | $found = $id; |
| 447 | } |
| 448 | } |
| 449 | break; |
| 450 | case Advanced_Ads_Select::GROUP : |
| 451 | if ( ! $found = array_search( $id, $this->imported_data['groups'] ) ) { |
| 452 | if ( term_exists( absint( $id ), Advanced_Ads::AD_GROUP_TAXONOMY ) ) { |
| 453 | $found = $id; |
| 454 | } |
| 455 | } |
| 456 | break; |
| 457 | } |
| 458 | |
| 459 | return (int) $found; |
| 460 | } |
| 461 | |
| 462 | /** |
| 463 | * Create new options based on import information |
| 464 | * |
| 465 | * @param array $decoded decoded XML |
| 466 | */ |
| 467 | private function import_options( &$decoded ) { |
| 468 | if ( isset( $decoded['options'] ) && is_array( $decoded['options'] ) ) { |
| 469 | foreach ( $decoded['options'] as $option_name => $option ) { |
| 470 | // ignore options not belonging to advanced ads |
| 471 | if ( 0 !== strpos( $option_name, 'advads-' ) && 0 !== strpos( $option_name, 'advanced-ads' ) ) { |
| 472 | continue; |
| 473 | } |
| 474 | |
| 475 | if( ! get_option( $option_name ) ) { |
| 476 | $this->messages[] = array( 'error', sprintf(__( 'Option was updated: <em>%s</em>', 'advanced-ads' ), $option_name ) ); |
| 477 | update_option( $option_name, maybe_unserialize( $option ) ); |
| 478 | } else { |
| 479 | $this->messages[] = array( 'error', sprintf(__( 'Option already exists: <em>%s</em>', 'advanced-ads' ), $option_name ) ); |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | /** |
| 486 | * Handles the XML upload |
| 487 | * |
| 488 | * @return bool false if error, true otherwise |
| 489 | */ |
| 490 | private function handle_upload() { |
| 491 | $uploads_dir = wp_upload_dir(); |
| 492 | if ( ! empty( $uploads_dir['error'] ) ) { |
| 493 | $this->messages[] = array( 'error', $uploads_dir['error'] ); |
| 494 | return; |
| 495 | } |
| 496 | |
| 497 | $import_dir = $uploads_dir['basedir'] . '/advads-import'; |
| 498 | $this->import_id = $import_dir . '/' . md5( time() . NONCE_SALT ); |
| 499 | |
| 500 | if ( ! is_dir( $import_dir) && ! wp_mkdir_p( $import_dir ) ) { |
| 501 | $this->messages[] = array( 'error', sprintf( __( 'Failed to create import directory <em>%s</em>', 'advanced-ads' ), $import_dir ) ); |
| 502 | return; |
| 503 | } |
| 504 | |
| 505 | if ( ! is_writable( $import_dir ) ) { |
| 506 | $this->messages[] = array( 'error', sprintf( __( 'Import directory is not writable: <em>%s</em>', 'advanced-ads' ), $import_dir ) ); |
| 507 | return; |
| 508 | } |
| 509 | |
| 510 | if ( ! @file_exists( $import_dir . '/index.php') ) @touch( $import_dir . '/index.php' ); |
| 511 | |
| 512 | |
| 513 | if ( ! isset( $_FILES['import'] ) ) { |
| 514 | $this->messages[] = array( 'error', __( 'File is empty, uploads are disabled or post_max_size is smaller than upload_max_filesize in php.ini', 'advanced-ads' ) ); |
| 515 | return; |
| 516 | } |
| 517 | |
| 518 | $file = $_FILES['import']; |
| 519 | |
| 520 | // determine if uploaded file exceeds space quota. |
| 521 | $file = apply_filters( "wp_handle_upload_prefilter", $file ); |
| 522 | |
| 523 | if ( ! empty( $file['error'] ) ) { |
| 524 | $this->messages[] = array( 'error', sprintf( __( 'Failed to upload file, error: <em>%s</em>', 'advanced-ads' ), $file['error'] ) ); |
| 525 | return; |
| 526 | } |
| 527 | |
| 528 | if ( ! ( $file['size'] > 0 ) ) { |
| 529 | $this->messages[] = array( 'error', __( 'File is empty.', 'advanced-ads' ), $file['error'] ); |
| 530 | return; |
| 531 | } |
| 532 | |
| 533 | if ( ! is_uploaded_file( $file['tmp_name'] ) || ! @ move_uploaded_file( $file['tmp_name'], $this->import_id ) || ! is_readable( $this->import_id ) ) { |
| 534 | $this->messages[] = array( 'error', sprintf( __( 'The file could not be created: <em>%s</em>. This is probably a permissions problem', 'advanced-ads' ), $this->import_id ) ); |
| 535 | return; |
| 536 | } |
| 537 | |
| 538 | // Set correct file permissions. |
| 539 | $stat = stat( dirname( $import_dir ) ); |
| 540 | $perms = $stat['mode'] & 0000666; |
| 541 | @ chmod( $this->import_id, $perms ); |
| 542 | |
| 543 | // cleanup in case of failed import |
| 544 | wp_schedule_single_event( time() + 10 * MINUTE_IN_SECONDS, 'advanced-ads-cleanup-import-file', array( $this->import_id ) ); |
| 545 | |
| 546 | return true; |
| 547 | } |
| 548 | |
| 549 | /** |
| 550 | * Ad content manipulations |
| 551 | * |
| 552 | * @param string $content |
| 553 | * @return string $content |
| 554 | */ |
| 555 | private function process_ad_content( $content ) { |
| 556 | // download images, replace old image urls with urls of these new images |
| 557 | $replacement_map = array(); |
| 558 | |
| 559 | if ( preg_match_all( '/\<advads_import_img\>(\S+?)\<\/advads_import_img\>/i', $content, $matches ) ) { |
| 560 | foreach ( $matches[1] as $k => $url ) { |
| 561 | if ( isset( $this->created_attachments[ $url ] ) ) { |
| 562 | $replacement_map[ $url ] = $this->created_attachments[ $url ]['attachment_url']; |
| 563 | } else if ( $attachment = $this->upload_image_from_url( $url ) ) { |
| 564 | $link = ( $link = get_attachment_link( $attachment['post_id'] ) ) ? sprintf( '<a href="%s">%s</a>', esc_url( $link ), __( 'Edit', 'advanced-ads' ) ) : ''; |
| 565 | $this->messages[] = array( 'update', sprintf( __( 'New attachment created <em>%s</em> %s', 'advanced-ads' ), $attachment['post_id'], $link ) ); |
| 566 | $this->created_attachments[ $url ] = $attachment; |
| 567 | $replacement_map[ $url ] = $attachment['attachment_url']; |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | $content = str_replace( array( '<advads_import_img>', '</advads_import_img>' ), '', $content ); |
| 573 | |
| 574 | if ( count( $replacement_map ) ) { |
| 575 | $content = str_replace( array_keys( $replacement_map ), array_values( $replacement_map ), $content ); |
| 576 | } |
| 577 | |
| 578 | // replace placeholders |
| 579 | return $this->replace_placeholders( $content ); |
| 580 | } |
| 581 | |
| 582 | /** |
| 583 | * Replace placeholders |
| 584 | * |
| 585 | * @param string $content |
| 586 | * @return string with replaced placeholders |
| 587 | */ |
| 588 | private function replace_placeholders( $content ) { |
| 589 | $content = str_replace( '{ADVADS_BASE_URL}', ADVADS_BASE_URL, $content ); |
| 590 | return $content; |
| 591 | } |
| 592 | |
| 593 | /** |
| 594 | * Upload image from URL and create attachment |
| 595 | * |
| 596 | * @param string $image_url |
| 597 | * @return array with indices: post_id, attachment_url, false on failure |
| 598 | */ |
| 599 | private function upload_image_from_url( $image_url ) { |
| 600 | $file_name = basename( current( explode( '?', $image_url ) ) ); |
| 601 | $wp_filetype = wp_check_filetype( $file_name, null ); |
| 602 | $parsed_url = @parse_url( $image_url ); |
| 603 | $image_url = str_replace( ' ', '%20', $image_url ); |
| 604 | |
| 605 | |
| 606 | if ( ! $wp_filetype['type'] ) { |
| 607 | $this->messages[] = array( 'error', sprintf( __( 'Invalid filetype <em>%s</em>', 'advanced-ads' ), $image_url ) ); |
| 608 | return false; |
| 609 | } |
| 610 | |
| 611 | if ( ! $parsed_url || ! is_array( $parsed_url ) ) { |
| 612 | $this->messages[] = array( 'error', sprintf( __( 'Error getting remote image <em>%s</em>', 'advanced-ads' ), $image_url ) ); |
| 613 | return false; |
| 614 | } |
| 615 | |
| 616 | $response = wp_safe_remote_get( $image_url, array( 'timeout' => 20 ) ); |
| 617 | |
| 618 | if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { |
| 619 | $this->messages[] = array( 'error', sprintf( __( 'Error getting remote image <em>%s</em>', 'advanced-ads' ), $image_url ) ); |
| 620 | return false; |
| 621 | } |
| 622 | |
| 623 | // Upload the file. |
| 624 | $upload = wp_upload_bits( $file_name, '', wp_remote_retrieve_body( $response ) ); |
| 625 | |
| 626 | if ( $upload['error'] ) { |
| 627 | $this->messages[] = array( 'error', sprintf( __( 'Error getting remote image <em>%s</em>', 'advanced-ads' ), $image_url ) ); |
| 628 | return false; |
| 629 | } |
| 630 | |
| 631 | // Get filesize. |
| 632 | $filesize = filesize( $upload['file'] ); |
| 633 | |
| 634 | if ( 0 == $filesize ) { |
| 635 | @unlink( $upload['file'] ); |
| 636 | $this->messages[] = array( 'error', sprintf( __( 'Zero size file downloaded <em>%s</em>', 'advanced-ads' ), $image_url ) ); |
| 637 | return false; |
| 638 | } |
| 639 | |
| 640 | if ( ! ( $fileinfo = @getimagesize( $upload['file'] ) ) || ! in_array( $fileinfo[2], array( IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_GIF ) ) ) { |
| 641 | @unlink( $upload['file'] ); |
| 642 | $this->messages[] = array( 'error', sprintf( __( 'Error getting remote image <em>%s</em>', 'advanced-ads' ), $image_url ) ); |
| 643 | return false; |
| 644 | } |
| 645 | |
| 646 | // create new post |
| 647 | $new_post = array( |
| 648 | 'post_title' => $file_name, |
| 649 | 'post_mime_type' => $wp_filetype['type'], |
| 650 | 'guid' => $upload['url'], |
| 651 | ); |
| 652 | |
| 653 | if ( ! function_exists( 'wp_generate_attachment_metadata' ) ) { |
| 654 | require_once( ABSPATH . 'wp-admin/includes/image.php' ); |
| 655 | } |
| 656 | |
| 657 | $post_id = wp_insert_attachment( $new_post, $upload['file'] ); |
| 658 | wp_update_attachment_metadata( $post_id, wp_generate_attachment_metadata( $post_id, $upload['file'] ) ); |
| 659 | |
| 660 | return array( 'post_id' => $post_id, 'attachment_url' => wp_get_attachment_url( $post_id ) ); |
| 661 | } |
| 662 | |
| 663 | public function get_messages(){ |
| 664 | return $this->messages; |
| 665 | } |
| 666 | } |
| 667 |