Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like LSX_WETU_Importer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use LSX_WETU_Importer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class LSX_WETU_Importer { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Holds class instance |
||
| 18 | * |
||
| 19 | * @since 1.0.0 |
||
| 20 | * |
||
| 21 | * @var object|Module_Template |
||
| 22 | */ |
||
| 23 | protected static $instance = null; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The slug for this plugin |
||
| 27 | * |
||
| 28 | * @since 0.0.1 |
||
| 29 | * |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | public $plugin_slug = 'lsx-wetu-importer'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The url to list items from WETU |
||
| 36 | * |
||
| 37 | * @since 0.0.1 |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | public $tab_slug = 'default'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The options for the plugin |
||
| 45 | * |
||
| 46 | * @since 0.0.1 |
||
| 47 | * |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | public $options = false; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * The url to import images from WETU |
||
| 54 | * |
||
| 55 | * @since 0.0.1 |
||
| 56 | * |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | public $import_scaling_url = false; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * scale the images on import or not |
||
| 63 | * |
||
| 64 | * @since 0.0.1 |
||
| 65 | * |
||
| 66 | * @var boolean |
||
| 67 | */ |
||
| 68 | public $scale_images = false; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * The WETU API Key |
||
| 72 | */ |
||
| 73 | public $api_key = false; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * The post types this works with. |
||
| 77 | */ |
||
| 78 | public $post_types = array(); |
||
| 79 | |||
| 80 | /** |
||
| 81 | * The previously attached images |
||
| 82 | * |
||
| 83 | * @var array() |
||
| 84 | */ |
||
| 85 | public $found_attachments = array(); |
||
| 86 | |||
| 87 | /** |
||
| 88 | * The gallery ids for the found attachements |
||
| 89 | * |
||
| 90 | * @var array() |
||
| 91 | */ |
||
| 92 | public $gallery_meta = array(); |
||
| 93 | |||
| 94 | /** |
||
| 95 | * The post ids to clean up (make sure the connected items are only singular) |
||
| 96 | * |
||
| 97 | * @var array() |
||
| 98 | */ |
||
| 99 | public $cleanup_posts = array(); |
||
| 100 | |||
| 101 | /** |
||
| 102 | * A post => parent relationship array. |
||
| 103 | * |
||
| 104 | * @var array() |
||
| 105 | */ |
||
| 106 | public $relation_meta = array(); |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Image Limit |
||
| 110 | * |
||
| 111 | * @var int |
||
| 112 | */ |
||
| 113 | public $image_limit = false; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * the featured image id |
||
| 117 | * |
||
| 118 | * @var int |
||
| 119 | */ |
||
| 120 | public $featured_image = false; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * the banner image |
||
| 124 | * |
||
| 125 | * @var int |
||
| 126 | */ |
||
| 127 | public $banner_image = false; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Holds the current import to display |
||
| 131 | * |
||
| 132 | * @var int |
||
| 133 | */ |
||
| 134 | public $current_importer = false; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * if you ran a tour import then you will have accommodation and destination queued to sync as well. |
||
| 138 | * |
||
| 139 | * @var int |
||
| 140 | */ |
||
| 141 | public $queued_imports = array(); |
||
| 142 | |||
| 143 | /** |
||
| 144 | * An Array to hold the items to queue |
||
| 145 | * |
||
| 146 | * @var int |
||
| 147 | */ |
||
| 148 | public $import_queue = array(); |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Holds the current post that is being imported. Use to check the content and excerpt. |
||
| 152 | * |
||
| 153 | * @var int |
||
| 154 | */ |
||
| 155 | public $current_post = false; |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Holds the accommodation settings |
||
| 159 | * |
||
| 160 | * @var int |
||
| 161 | */ |
||
| 162 | public $accommodation_settings = false; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Holds the tour settings |
||
| 166 | * |
||
| 167 | * @var int |
||
| 168 | */ |
||
| 169 | public $tour_settings = false; |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Holds the destination settings |
||
| 173 | * |
||
| 174 | * @var int |
||
| 175 | */ |
||
| 176 | public $destination_settings = false; |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Hold the flag to let you know if the debug is enabled or not. |
||
| 180 | * |
||
| 181 | * @var int |
||
| 182 | */ |
||
| 183 | public $debug_enabled = false; |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Hold the post columns object |
||
| 187 | * |
||
| 188 | * @var object LSX_WETU_Importer_Post_Columns() |
||
| 189 | */ |
||
| 190 | public $post_columns = false; |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Initialize the plugin by setting localization, filters, and administration functions. |
||
| 194 | * |
||
| 195 | * @since 1.0.0 |
||
| 196 | * |
||
| 197 | * @access private |
||
| 198 | */ |
||
| 199 | public function __construct() { |
||
| 236 | |||
| 237 | // ACTIVATION FUNCTIONS. |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Load the plugin text domain for translation. |
||
| 241 | * |
||
| 242 | * @since 1.0.0 |
||
| 243 | */ |
||
| 244 | public function load_plugin_textdomain() { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Sets the variables used throughout the plugin. |
||
| 250 | */ |
||
| 251 | public function set_variables() { |
||
| 315 | |||
| 316 | // COMPATABILITY FUNCTIONS. |
||
| 317 | |||
| 318 | /** |
||
| 319 | * On plugin activation |
||
| 320 | * |
||
| 321 | * @since 1.0.0 |
||
| 322 | */ |
||
| 323 | public static function register_activation_hook() { |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Check if the PHP version is compatible. |
||
| 329 | * |
||
| 330 | * @since 1.0.0 |
||
| 331 | */ |
||
| 332 | public static function compatible_version() { |
||
| 339 | |||
| 340 | /** |
||
| 341 | * The backup sanity check, in case the plugin is activated in a weird way, |
||
| 342 | * or the versions change after activation. |
||
| 343 | * |
||
| 344 | * @since 1.0.0 |
||
| 345 | */ |
||
| 346 | public function compatible_version_check() { |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Display the notice related with the older version from PHP. |
||
| 361 | * |
||
| 362 | * @since 1.0.0 |
||
| 363 | */ |
||
| 364 | public function compatible_version_notice() { |
||
| 369 | |||
| 370 | /** |
||
| 371 | * The primary sanity check, automatically disable the plugin on activation if it doesn't |
||
| 372 | * meet minimum requirements. |
||
| 373 | * |
||
| 374 | * @since 1.0.0 |
||
| 375 | */ |
||
| 376 | public static function compatible_version_check_on_activation() { |
||
| 382 | |||
| 383 | // DISPLAY FUNCTIONS. |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Load the importer class you want to use |
||
| 387 | */ |
||
| 388 | public function load_class() { |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Registers the admin page which will house the importer form. |
||
| 414 | */ |
||
| 415 | public function register_importer_page() { |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Enqueue the JS needed to contact wetu and return your result. |
||
| 421 | */ |
||
| 422 | public function admin_scripts() { |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Display the importer administration screen |
||
| 454 | */ |
||
| 455 | public function display_page() { |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Outputs the post status navigation |
||
| 472 | * |
||
| 473 | * @return void |
||
| 474 | */ |
||
| 475 | public function post_status_navigation() { |
||
| 476 | ?> |
||
| 477 | <ul class="subsubsub"> |
||
| 478 | <li class="searchform"><a class="current" href="#search"><?php esc_attr_e( 'Search', 'lsx-wetu-importer' ); ?></a> | </li> |
||
| 479 | <li class="publish"><a href="#publish"><?php esc_attr_e( 'Published', 'lsx-wetu-importer' ); ?> <span class="count"> (<?php echo esc_attr( lsx_wetu_get_post_count( $this->tab_slug, 'publish ' ) ); ?>)</span></a> | </li> |
||
| 480 | <li class="pending"><a href="#pending"><?php esc_attr_e( 'Pending', 'lsx-wetu-importer' ); ?> <span class="count"> (<?php echo esc_attr( lsx_wetu_get_post_count( $this->tab_slug, 'pending ' ) ); ?>)</span></a>| </li> |
||
| 481 | <li class="draft"><a href="#draft"><?php esc_attr_e( 'Draft', 'lsx-wetu-importer' ); ?></a> <span class="count"> (<?php echo esc_attr( lsx_wetu_get_post_count( $this->tab_slug, 'draft ' ) ); ?>)</span></li> |
||
| 482 | |||
| 483 | <?php if ( 'tour' === $this->tab_slug ) { ?> |
||
| 484 | <li class="import"> | <a class="import search-toggle" href="#import"><?php esc_attr_e( 'WETU', 'lsx-wetu-importer' ); ?> <span class="count"> (<?php echo esc_attr( lsx_wetu_get_tour_count() ); ?>)</span></a></li> |
||
| 485 | <?php } else if ( ! empty( $this->queued_imports ) ) { ?> |
||
| 486 | <li class="import"> | <a class="import search-toggle" href="#import"><?php esc_attr_e( 'WETU Queue', 'lsx-wetu-importer' ); ?> <span class="count"> (<?php echo esc_attr( lsx_wetu_get_queue_count( $this->tab_slug ) ); ?>)</span></a></li> |
||
| 487 | <?php } ?> |
||
| 488 | </ul> |
||
| 489 | <a class="documentation" target="_blank"href="https://tour-operator.lsdev.biz/documentation/extension/wetu-importer/"><?php esc_attr_e( 'Documentation', 'lsx-wetu-importer' ); ?></a> |
||
| 490 | <?php |
||
| 491 | } |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Search Form |
||
| 495 | */ |
||
| 496 | public function search_form() { |
||
| 497 | ?> |
||
| 498 | <form class="ajax-form" id="<?php echo esc_attr( $this->plugin_slug ); ?>-search-form" method="get" action="tools.php" data-type="<?php echo esc_attr( $this->tab_slug ); ?>"> |
||
| 499 | <input type="hidden" name="page" value="<?php echo esc_attr( $this->tab_slug ); ?>" /> |
||
| 500 | |||
| 501 | <?php do_action( 'lsx_wetu_importer_search_form',$this ); ?> |
||
| 502 | |||
| 503 | <div class="normal-search"> |
||
| 504 | <input pattern=".{3,}" placeholder="3 characters minimum" class="keyword" name="keyword" value=""> <input class="button button-primary submit" type="submit" value="<?php esc_html_e( 'Search', 'lsx-wetu-importer' ); ?>" /> |
||
| 505 | </div> |
||
| 506 | |||
| 507 | <div class="advanced-search hidden" style="display:none;"> |
||
| 508 | <textarea rows="10" cols="40" name="bulk-keywords"></textarea> |
||
| 509 | <input class="button button-primary submit" type="submit" value="<?php esc_attr_e( 'Search', 'lsx-wetu-importer' ); ?>" /> |
||
| 510 | </div> |
||
| 511 | |||
| 512 | <div class="ajax-loader" style="display:none;width:100%;text-align:center;"> |
||
| 513 | <img style="width:64px;" src="<?php echo esc_url( LSX_WETU_IMPORTER_URL . 'assets/images/ajaxloader.gif' ); ?>" /> |
||
| 514 | </div> |
||
| 515 | |||
| 516 | <div class="ajax-loader-small" style="display:none;width:100%;text-align:center;"> |
||
| 517 | <img style="width:32px;" src="<?php echo esc_url( LSX_WETU_IMPORTER_URL . 'assets/images/ajaxloader.gif' ); ?>" /> |
||
| 518 | </div> |
||
| 519 | |||
| 520 | <a class="button advanced-search-toggle" href="#"><?php esc_html_e( 'Bulk Search', 'lsx-wetu-importer' ); ?></a> |
||
| 521 | </form> |
||
| 522 | <?php |
||
| 523 | } |
||
| 524 | |||
| 525 | /** |
||
| 526 | * The header of the item list |
||
| 527 | */ |
||
| 528 | public function table_header() { |
||
| 529 | ?> |
||
| 530 | <thead> |
||
| 531 | <tr> |
||
| 532 | <th style="" class="manage-column column-cb check-column no-sort" id="cb" scope="col"> |
||
| 533 | <label for="cb-select-all-1" class="screen-reader-text"><?php esc_attr_e( 'Select All', 'lsx-wetu-importer' ); ?></label> |
||
| 534 | <input type="checkbox" id="cb-select-all-1"> |
||
| 535 | </th> |
||
| 536 | <th style="" class="manage-column column-order " id="order"><?php esc_attr_e( 'Order', 'lsx-wetu-importer' ); ?></th> |
||
| 537 | <th style="" class="manage-column column-title " id="title" style="width:50%;" scope="col"><?php esc_attr_e( 'Title', 'lsx-wetu-importer' ); ?></th> |
||
| 538 | <th style="" class="manage-column column-date" id="date" scope="col"><?php esc_attr_e( 'Date', 'lsx-wetu-importer' ); ?></th> |
||
| 539 | <th style="" class="manage-column column-ssid" id="ssid" scope="col"><?php esc_attr_e( 'WETU ID', 'lsx-wetu-importer' ); ?></th> |
||
| 540 | </tr> |
||
| 541 | </thead> |
||
| 542 | <?php |
||
| 543 | } |
||
| 544 | |||
| 545 | /** |
||
| 546 | * The footer of the item list |
||
| 547 | */ |
||
| 548 | public function table_footer() { |
||
| 549 | ?> |
||
| 550 | <tfoot> |
||
| 551 | <tr> |
||
| 552 | <th style="" class="manage-column column-cb check-column" id="cb" scope="col"> |
||
| 553 | <label for="cb-select-all-1" class="screen-reader-text"><?php esc_attr_e( 'Select All', 'lsx-wetu-importer' ); ?></label> |
||
| 554 | <input type="checkbox" id="cb-select-all-1"> |
||
| 555 | </th> |
||
| 556 | <th style="" class="manage-column column-order "><?php esc_attr_e( 'Order', 'lsx-wetu-importer' ); ?></th> |
||
| 557 | <th style="" class="manage-column column-title" scope="col"><?php esc_attr_e( 'Title', 'lsx-wetu-importer' ); ?></th> |
||
| 558 | <th style="" class="manage-column column-date" scope="col"><?php esc_attr_e( 'Date', 'lsx-wetu-importer' ); ?></th> |
||
| 559 | <th style="" class="manage-column column-ssid" scope="col"><?php esc_attr_e( 'WETU ID', 'lsx-wetu-importer' ); ?></th> |
||
| 560 | </tr> |
||
| 561 | </tfoot> |
||
| 562 | <?php |
||
| 563 | } |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Displays the importers navigation. |
||
| 567 | * |
||
| 568 | * @param $tab string |
||
| 569 | */ |
||
| 570 | public function navigation( $tab = '' ) { |
||
| 571 | $post_types = array( |
||
| 572 | 'tour' => esc_attr( 'Tours', 'lsx-wetu-importer' ), |
||
| 573 | 'accommodation' => esc_attr( 'Accommodation', 'lsx-wetu-importer' ), |
||
| 574 | 'destination' => esc_attr( 'Destinations', 'lsx-wetu-importer' ), |
||
| 575 | ); |
||
| 576 | |||
| 577 | echo wp_kses_post( '<div class="wp-filter">' ); |
||
| 578 | echo wp_kses_post( '<ul class="filter-links">' ); |
||
| 579 | echo wp_kses_post( '<li><a class="' . $this->itemd( $tab, 'default', 'current', false ) . '" href="' . admin_url( 'admin.php' ) . '?page=' . $this->plugin_slug . '">' . esc_attr__( 'Home', 'lsx-wetu-importer' ) . '</a></li>' ); |
||
| 580 | |||
| 581 | foreach ( $post_types as $post_type => $label ) { |
||
| 582 | echo wp_kses_post( ' | <li><a class="' . $this->itemd( $tab, $post_type, 'current', false ) . '" href="' . admin_url( 'admin.php' ) . '?page=' . $this->plugin_slug . '&tab=' . $post_type . '">' . $label . '</a></li>' ); |
||
| 583 | } |
||
| 584 | |||
| 585 | echo wp_kses_post( ' | <li><a class="' . $this->itemd( $tab, 'settings', 'current', false ) . '" href="' . admin_url( 'admin.php' ) . '?page=' . $this->plugin_slug . '&tab=settings">' . esc_attr__( 'Settings', 'lsx-wetu-importer' ) . '</a></li>' ); |
||
| 586 | echo wp_kses_post( '</ul> </div>' ); |
||
| 587 | } |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Wetu Status Bar. |
||
| 591 | */ |
||
| 592 | public function wetu_status() { |
||
| 593 | $tours = get_transient( 'lsx_ti_tours' ); |
||
| 594 | echo '<div class="wetu-status tour-wetu-status"><h3>' . esc_html__( 'Wetu Status', 'lsx-wetu-importer' ) . ' - '; |
||
| 595 | |||
| 596 | if ( '' === $tours || false === $tours || isset( $_GET['refresh_tours'] ) ) { |
||
| 597 | $result = $this->update_options(); |
||
| 598 | if ( true === $result ) { |
||
| 599 | echo '<span style="color:green;">' . esc_attr( 'Connected', 'lsx-wetu-importer' ) . '</span>'; |
||
| 600 | echo ' - <small><a href="#">' . esc_attr( 'Refresh', 'lsx-wetu-importer' ) . '</a></small>'; |
||
| 601 | } else { |
||
| 602 | echo '<span style="color:red;">' . wp_kses_post( $result ) . '</span>'; |
||
| 603 | } |
||
| 604 | } else { |
||
| 605 | echo '<span style="color:green;">' . esc_attr( 'Connected', 'lsx-wetu-importer' ) . '</span> - <small><a href="#">' . esc_attr( 'Refresh', 'lsx-wetu-importer' ) . '</a></small>'; |
||
| 606 | } |
||
| 607 | echo '</h3>'; |
||
| 608 | echo '</div>'; |
||
| 609 | } |
||
| 610 | |||
| 611 | /** |
||
| 612 | * Set_taxonomy with some terms |
||
| 613 | */ |
||
| 614 | public function team_member_checkboxes( $selected = array() ) { |
||
| 640 | |||
| 641 | |||
| 642 | // GENERAL FUNCTIONS. |
||
| 643 | |||
| 644 | /** |
||
| 645 | * Checks to see if an item is checked. |
||
| 646 | * |
||
| 647 | * @param $haystack array|string |
||
| 648 | * @param $needle string |
||
| 649 | * @param $echo bool |
||
| 650 | */ |
||
| 651 | View Code Duplication | public function checked( $haystack = false, $needle = '', $echo = true ) { |
|
| 662 | |||
| 663 | /** |
||
| 664 | * Checks to see if an item is checked. |
||
| 665 | * |
||
| 666 | * @param $haystack array|string |
||
| 667 | * @param $needle string |
||
| 668 | * @param $echo bool |
||
| 669 | */ |
||
| 670 | View Code Duplication | public function selected( $haystack = false, $needle = '', $echo = true ) { |
|
| 681 | |||
| 682 | /** |
||
| 683 | * Checks to see if an item is selected. If $echo is false, it will return the $type if conditions are true. |
||
| 684 | * |
||
| 685 | * @param $haystack array|string |
||
| 686 | * @param $needle string |
||
| 687 | * @param $type string |
||
| 688 | * @param $wrap bool |
||
| 689 | * @return $html string |
||
| 690 | */ |
||
| 691 | public function itemd( $haystack = false, $needle = '', $type = '', $wrap = true ) { |
||
| 709 | |||
| 710 | /** |
||
| 711 | * Grabs any attachments for the current item |
||
| 712 | */ |
||
| 713 | public function find_attachments( $id = false ) { |
||
| 735 | |||
| 736 | // CUSTOM FIELD FUNCTIONS. |
||
| 737 | |||
| 738 | /** |
||
| 739 | * Saves the room data |
||
| 740 | */ |
||
| 741 | public function save_custom_field( $value = false, $meta_key, $id, $decrease = false, $unique = true ) { |
||
| 757 | |||
| 758 | /** |
||
| 759 | * Grabs the custom fields, and resaves an array of unique items. |
||
| 760 | */ |
||
| 761 | public function cleanup_posts() { |
||
| 775 | |||
| 776 | // TAXONOMY FUNCTIONS. |
||
| 777 | |||
| 778 | /** |
||
| 779 | * Set_taxonomy with some terms |
||
| 780 | */ |
||
| 781 | public function set_taxonomy( $taxonomy, $terms, $id ) { |
||
| 806 | |||
| 807 | /** |
||
| 808 | * Sets the terms of the current post |
||
| 809 | * |
||
| 810 | * @param boolean $id |
||
| 811 | * @param boolean $name |
||
| 812 | * @param boolean $taxonomy |
||
| 813 | * @param boolean $parent |
||
| 814 | * @return void |
||
| 815 | */ |
||
| 816 | public function set_term( $id = false, $name = false, $taxonomy = false, $parent = false ) { |
||
| 837 | |||
| 838 | /** |
||
| 839 | * set_taxonomy with some terms |
||
| 840 | */ |
||
| 841 | public function taxonomy_checkboxes( $taxonomy = false, $selected = array() ) { |
||
| 864 | |||
| 865 | // MAP FUNCTIONS |
||
| 866 | |||
| 867 | /** |
||
| 868 | * Saves the longitude and lattitude, as well as sets the map marker. |
||
| 869 | */ |
||
| 870 | public function set_map_data( $data, $id, $zoom = '10' ) { |
||
| 924 | |||
| 925 | // IMAGE FUNCTIONS |
||
| 926 | |||
| 927 | /** |
||
| 928 | * Creates the main gallery data |
||
| 929 | */ |
||
| 930 | public function set_featured_image( $data, $id ) { |
||
| 944 | |||
| 945 | /** |
||
| 946 | * Sets a banner image |
||
| 947 | */ |
||
| 948 | public function set_banner_image( $data, $id, $content = array( 'none' ) ) { |
||
| 979 | |||
| 980 | /** |
||
| 981 | * Creates the main gallery data |
||
| 982 | */ |
||
| 983 | public function create_main_gallery( $data, $id ) { |
||
| 1032 | |||
| 1033 | /** |
||
| 1034 | * search_form |
||
| 1035 | */ |
||
| 1036 | public function get_scaling_url( $args = array() ) { |
||
| 1065 | |||
| 1066 | /** |
||
| 1067 | * Attaches 1 image |
||
| 1068 | */ |
||
| 1069 | public function attach_image( $v = false, $parent_id, $image_sizes = false, $banner = false ) { |
||
| 1070 | if ( false !== $v ) { |
||
| 1071 | $temp_fragment = explode( '/', $v['url_fragment'] ); |
||
| 1072 | $url_filename = $temp_fragment[ count( $temp_fragment ) -1 ]; |
||
| 1073 | $url_filename = str_replace( array( '.jpg', '.png', '.jpeg' ), '', $url_filename ); |
||
| 1074 | $url_filename = trim( $url_filename ); |
||
| 1075 | $title = $url_filename; |
||
| 1076 | $url_filename = str_replace( ' ', '_', $url_filename ); |
||
| 1077 | |||
| 1078 | if ( ! isset( $this->options['image_replacing'] ) && in_array( $url_filename, $this->found_attachments ) ) { |
||
| 1079 | return array_search( $url_filename, $this->found_attachments ); |
||
| 1080 | } |
||
| 1081 | |||
| 1082 | $postdata = array(); |
||
| 1083 | |||
| 1084 | if ( empty( $v['label'] ) ) { |
||
| 1085 | $v['label'] = ''; |
||
| 1086 | } |
||
| 1087 | |||
| 1088 | View Code Duplication | if ( ! empty( $v['description'] ) ) { |
|
| 1089 | $desc = wp_strip_all_tags( $v['description'] ); |
||
| 1090 | $posdata = array( |
||
| 1091 | 'post_excerpt' => $desc, |
||
| 1092 | ); |
||
| 1093 | } |
||
| 1094 | |||
| 1095 | View Code Duplication | if ( ! empty( $v['section'] ) ) { |
|
| 1096 | $desc = wp_strip_all_tags( $v['section'] ); |
||
| 1097 | $posdata = array( |
||
| 1098 | 'post_excerpt' => $desc, |
||
| 1099 | ); |
||
| 1100 | } |
||
| 1101 | |||
| 1102 | $attach_id = null; |
||
| 1103 | // Resizor - add option to setting if required. |
||
| 1104 | $fragment = str_replace( ' ', '%20', $v['url_fragment'] ); |
||
| 1105 | $url = $this->get_scaling_url( $image_sizes ) . $fragment; |
||
| 1106 | $attach_id = $this->attach_external_image2( $url, $parent_id, '', $v['label'], $postdata ); |
||
| 1107 | if ( ! empty( $attach_id ) ) { |
||
| 1108 | $this->found_attachments[ $attach_id ] = $url_filename; |
||
| 1109 | add_post_meta( $attach_id, 'lsx_wetu_id', $v['url_fragment'] , true ); |
||
| 1110 | return $attach_id; |
||
| 1111 | } |
||
| 1112 | } |
||
| 1113 | return false; |
||
| 1114 | } |
||
| 1115 | |||
| 1116 | public function attach_external_image2( $url = null, $post_id = null, $thumb = null, $filename = null, $post_data = array() ) { |
||
| 1117 | if ( ! $url || ! $post_id ) { return new WP_Error( 'missing', 'Need a valid URL and post ID...' ); } |
||
| 1118 | $att_id = false; |
||
| 1119 | |||
| 1120 | require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
||
| 1121 | require_once( ABSPATH . 'wp-admin/includes/media.php' ); |
||
| 1122 | require_once( ABSPATH . 'wp-admin/includes/image.php' ); |
||
| 1123 | // Download file to temp location, returns full server path to temp file. |
||
| 1124 | |||
| 1125 | $tmp = tempnam( '/tmp', 'FOO' ); |
||
| 1126 | $image = wp_remote_get( $url ); |
||
| 1127 | if ( ! empty( $image ) && isset( $image['response'] ) && isset( $image['response']['code'] ) && 200 === $image['response']['code'] ) { |
||
| 1128 | file_put_contents( $tmp, $image['body'] ); |
||
| 1129 | chmod( $tmp,'777' ); |
||
| 1130 | |||
| 1131 | preg_match( '/[^\?]+\.(tif|TIFF|jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG|pdf|PDF|bmp|BMP)/', $url, $matches ); // fix file filename for query strings |
||
| 1132 | $url_filename = basename( $matches[0] ); |
||
| 1133 | $url_filename = str_replace( '%20','_',$url_filename ); |
||
| 1134 | // extract filename from url for title |
||
| 1135 | $url_type = wp_check_filetype( $url_filename ); // determine file type (ext and mime/type) |
||
| 1136 | |||
| 1137 | // override filename if given, reconstruct server path. |
||
| 1138 | if ( ! empty( $filename ) && ' ' != $filename ) { |
||
| 1139 | $filename = sanitize_file_name( $filename ); |
||
| 1140 | $tmppath = pathinfo( $tmp ); |
||
| 1141 | |||
| 1142 | $extension = ''; |
||
| 1143 | if ( isset( $tmppath['extension'] ) ) { |
||
| 1144 | $extension = $tmppath['extension']; |
||
| 1145 | } |
||
| 1146 | |||
| 1147 | $new = $tmppath['dirname'] . '/' . $filename . '.' . $extension; |
||
| 1148 | rename( $tmp, $new ); // renames temp file on server |
||
| 1149 | $tmp = $new; // push new filename (in path) to be used in file array later |
||
| 1150 | } |
||
| 1151 | |||
| 1152 | // assemble file data (should be built like $_FILES since wp_handle_sideload() will be using). |
||
| 1153 | $file_array['tmp_name'] = $tmp; // full server path to temp file |
||
| 1154 | |||
| 1155 | View Code Duplication | if ( ! empty( $filename ) && ' ' != $filename ) { |
|
| 1156 | $file_array['name'] = $filename . '.' . $url_type['ext']; // user given filename for title, add original URL extension |
||
| 1157 | } else { |
||
| 1158 | $file_array['name'] = $url_filename; // just use original URL filename |
||
| 1159 | } |
||
| 1160 | |||
| 1161 | // set additional wp_posts columns. |
||
| 1162 | View Code Duplication | if ( empty( $post_data['post_title'] ) ) { |
|
| 1163 | |||
| 1164 | $url_filename = str_replace( '%20',' ', $url_filename ); |
||
| 1165 | |||
| 1166 | $post_data['post_title'] = basename( $url_filename, '.' . $url_type['ext'] ); // just use the original filename (no extension) |
||
| 1167 | } |
||
| 1168 | |||
| 1169 | // make sure gets tied to parent. |
||
| 1170 | if ( empty( $post_data['post_parent'] ) ) { |
||
| 1171 | $post_data['post_parent'] = $post_id; |
||
| 1172 | } |
||
| 1173 | |||
| 1174 | // do the validation and storage stuff. |
||
| 1175 | $att_id = media_handle_sideload( $file_array, $post_id, null, $post_data ); // $post_data can override the items saved to wp_posts table, like post_mime_type, guid, post_parent, post_title, post_content, post_status |
||
| 1176 | |||
| 1177 | // If error storing permanently, unlink. |
||
| 1178 | if ( is_wp_error( $att_id ) ) { |
||
| 1179 | unlink( $file_array['tmp_name'] ); |
||
| 1180 | return false; |
||
| 1181 | } |
||
| 1182 | } |
||
| 1183 | return $att_id; |
||
| 1184 | } |
||
| 1185 | |||
| 1186 | // AJAX FUNCTIONS |
||
| 1187 | /** |
||
| 1188 | * Run through the accommodation grabbed from the DB. |
||
| 1189 | */ |
||
| 1190 | public function process_ajax_search() { |
||
| 1191 | $this->current_importer->process_ajax_search(); |
||
| 1192 | die(); |
||
| 1193 | } |
||
| 1194 | |||
| 1195 | /** |
||
| 1196 | * Connect to wetu |
||
| 1197 | */ |
||
| 1198 | public function process_ajax_import() { |
||
| 1199 | $this->current_importer->process_ajax_import(); |
||
| 1200 | die(); |
||
| 1201 | } |
||
| 1202 | |||
| 1203 | /** |
||
| 1204 | * Formats the row for the completed list. |
||
| 1205 | */ |
||
| 1206 | public function format_completed_row( $response ) { |
||
| 1207 | echo wp_kses_post( '<li class="post-' . $response . '"><span class="dashicons dashicons-yes"></span> <a target="_blank" href="' . get_permalink( $response ) . '">' . get_the_title( $response ) . '</a></li>' ); |
||
| 1208 | } |
||
| 1209 | |||
| 1210 | /** |
||
| 1211 | * Formats the error. |
||
| 1212 | */ |
||
| 1213 | public function format_error( $response ) { |
||
| 1214 | echo wp_kses_post( '<li class="post-error"><span class="dashicons dashicons-no"></span>' . $response . '</li>' ); |
||
| 1215 | } |
||
| 1216 | |||
| 1217 | /** |
||
| 1218 | * Does a multine search |
||
| 1219 | */ |
||
| 1220 | public function multineedle_stripos( $haystack, $needles, $offset = 0 ) { |
||
| 1221 | $found = false; |
||
| 1222 | $needle_count = count( $needles ); |
||
| 1223 | |||
| 1224 | foreach ( $needles as $needle ) { |
||
| 1225 | if ( false !== stripos( $haystack, $needle, $offset ) ) { |
||
| 1226 | $found[] = true; |
||
| 1227 | } |
||
| 1228 | } |
||
| 1229 | |||
| 1230 | if ( false !== $found && count( $found ) === $needle_count ) { |
||
| 1231 | return true; |
||
| 1232 | } else { |
||
| 1233 | return false; |
||
| 1234 | } |
||
| 1235 | } |
||
| 1236 | |||
| 1237 | /** |
||
| 1238 | * Grab all the current accommodation posts via the lsx_wetu_id field. |
||
| 1239 | */ |
||
| 1240 | View Code Duplication | public function find_current_accommodation( $post_type = 'accommodation' ) { |
|
| 1241 | global $wpdb; |
||
| 1242 | $return = array(); |
||
| 1243 | |||
| 1244 | // @codingStandardsIgnoreStart |
||
| 1245 | $current_accommodation = $wpdb->get_results(" |
||
| 1246 | SELECT key1.post_id,key1.meta_value |
||
| 1247 | FROM {$wpdb->postmeta} key1 |
||
| 1248 | |||
| 1249 | INNER JOIN {$wpdb->posts} key2 |
||
| 1250 | ON key1.post_id = key2.ID |
||
| 1251 | |||
| 1252 | WHERE key1.meta_key = 'lsx_wetu_id' |
||
| 1253 | AND key2.post_type = '{$post_type}' |
||
| 1254 | |||
| 1255 | LIMIT 0,5000 |
||
| 1256 | "); |
||
| 1257 | // @codingStandardsIgnoreEnd |
||
| 1258 | |||
| 1259 | if ( null !== $current_accommodation && ! empty( $current_accommodation ) ) { |
||
| 1260 | foreach ( $current_accommodation as $accom ) { |
||
| 1261 | $return[ $accom->meta_value ] = $accom; |
||
| 1262 | } |
||
| 1263 | } |
||
| 1264 | |||
| 1265 | return $return; |
||
| 1266 | } |
||
| 1267 | |||
| 1268 | /** |
||
| 1269 | * Set the Video date |
||
| 1270 | */ |
||
| 1271 | public function set_video_data( $data, $id ) { |
||
| 1272 | if ( ! empty( $data[0]['content']['youtube_videos'] ) && is_array( $data[0]['content']['youtube_videos'] ) ) { |
||
| 1273 | $videos = false; |
||
| 1274 | |||
| 1275 | foreach ( $data[0]['content']['youtube_videos'] as $video ) { |
||
| 1276 | $temp_video = array(); |
||
| 1277 | |||
| 1278 | if ( isset( $video['label'] ) ) { |
||
| 1279 | $temp_video['title'] = $video['label']; |
||
| 1280 | } |
||
| 1281 | if ( isset( $video['description'] ) ) { |
||
| 1282 | $temp_video['description'] = strip_tags( $video['description'] ); |
||
| 1283 | } |
||
| 1284 | if ( isset( $video['url'] ) ) { |
||
| 1285 | $temp_video['url'] = $video['url']; |
||
| 1286 | } |
||
| 1287 | |||
| 1288 | $temp_video['thumbnail'] = ''; |
||
| 1289 | $videos[] = $temp_video; |
||
| 1290 | } |
||
| 1291 | |||
| 1292 | if ( false !== $id && '0' !== $id ) { |
||
| 1293 | delete_post_meta( $id, 'videos' ); |
||
| 1294 | } |
||
| 1295 | |||
| 1296 | foreach ( $videos as $video ) { |
||
| 1297 | add_post_meta( $id,'videos',$video,false ); |
||
| 1298 | } |
||
| 1299 | } |
||
| 1300 | } |
||
| 1301 | |||
| 1302 | public function shuffle_assoc( &$array ) { |
||
| 1303 | $new = array(); |
||
| 1304 | $keys = array_keys( $array ); |
||
| 1305 | |||
| 1306 | shuffle( $keys ); |
||
| 1307 | |||
| 1308 | foreach ( $keys as $key ) { |
||
| 1309 | $new[ $key ] = $array[ $key ]; |
||
| 1310 | } |
||
| 1311 | |||
| 1312 | $array = $new; |
||
| 1313 | |||
| 1314 | return true; |
||
| 1315 | } |
||
| 1316 | |||
| 1317 | /** |
||
| 1318 | * Save the list of Tours into an option |
||
| 1319 | */ |
||
| 1320 | public function update_options() { |
||
| 1321 | $own = ''; |
||
| 1322 | $options = array(); |
||
| 1323 | delete_option( 'lsx_ti_tours_api_options' ); |
||
| 1324 | |||
| 1325 | if ( isset( $_GET['own'] ) ) { |
||
| 1326 | $this->current_importer->url_qs .= '&own=true'; |
||
| 1327 | $options[] = 'own'; |
||
| 1328 | } |
||
| 1329 | |||
| 1330 | if ( isset( $_GET['type'] ) && 'allitineraries' !== $_GET['type'] ) { |
||
| 1331 | $this->current_importer->url_qs .= '&type=' . $_GET['type']; |
||
| 1332 | $options[] = $_GET['type']; |
||
| 1333 | } else { |
||
| 1334 | $options[] = 'sample'; |
||
| 1335 | $this->current_importer->url_qs .= '&type=sample'; |
||
| 1336 | } |
||
| 1337 | |||
| 1338 | $url = str_replace( 'Pins', 'Itinerary', $this->current_importer->url . '/V8/List?' . $this->current_importer->url_qs ); |
||
| 1339 | $url .= '&results=2000'; |
||
| 1340 | add_option( 'lsx_ti_tours_api_options', $options ); |
||
| 1341 | $data = wp_remote_get( $url ); |
||
| 1342 | $tours = json_decode( wp_remote_retrieve_body( $data ), true ); |
||
| 1343 | |||
| 1344 | if ( isset( $tours['error'] ) ) { |
||
| 1345 | return $tours['error']; |
||
| 1346 | } elseif ( isset( $tours['itineraries'] ) && ! empty( $tours['itineraries'] ) ) { |
||
| 1347 | set_transient( 'lsx_ti_tours', $tours['itineraries'], 60 * 60 * 4 ); |
||
| 1348 | return true; |
||
| 1349 | } |
||
| 1350 | } |
||
| 1351 | |||
| 1352 | /** |
||
| 1353 | * Gets the post_id from the key |
||
| 1354 | * |
||
| 1355 | * @param boolean $wetu_id |
||
| 1356 | * @return string |
||
| 1357 | */ |
||
| 1358 | public function get_post_id_by_key_value( $wetu_id = false ) { |
||
| 1369 | /** |
||
| 1370 | * Set the team memberon each item. |
||
| 1371 | */ |
||
| 1372 | public function set_team_member( $id, $team_members ) { |
||
| 1373 | delete_post_meta( $id, 'team_to_' . $this->tab_slug ); |
||
| 1374 | |||
| 1375 | foreach ( $team_members as $team ) { |
||
| 1376 | add_post_meta( $id, 'team_to_' . $this->tab_slug, $team ); |
||
| 1377 | } |
||
| 1378 | } |
||
| 1379 | } |
||
| 1380 | |||
| 1381 | $lsx_wetu_importer = new LSX_WETU_Importer(); |
||
| 1382 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..