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 WETU_Importer_Tours 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 WETU_Importer_Tours, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class WETU_Importer_Tours extends WETU_Importer { |
||
| 11 | |||
| 12 | /** |
||
| 13 | * The url to list items from WETU |
||
| 14 | * |
||
| 15 | * @since 0.0.1 |
||
| 16 | * |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | public $tab_slug = 'tour'; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * The url to list items from WETU |
||
| 23 | * |
||
| 24 | * @since 0.0.1 |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | public $url = false; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The query string url to list items from WETU |
||
| 32 | * |
||
| 33 | * @since 0.0.1 |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | public $url_qs = false; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Holds a list of any current accommodation |
||
| 41 | * |
||
| 42 | * @since 0.0.1 |
||
| 43 | * |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | public $current_accommodation = false; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Holds a list of any current destinations |
||
| 50 | * |
||
| 51 | * @since 0.0.1 |
||
| 52 | * |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | public $current_destinations = false; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Holds a list of the destination and the image it needs to grab. |
||
| 59 | * |
||
| 60 | * @since 0.0.1 |
||
| 61 | * |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | public $destination_images = false; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Options |
||
| 68 | * |
||
| 69 | * @since 0.0.1 |
||
| 70 | * |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | public $options = false; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * The fields you wish to import |
||
| 77 | * |
||
| 78 | * @since 0.0.1 |
||
| 79 | * |
||
| 80 | * @var string |
||
| 81 | */ |
||
| 82 | public $tour_options = false; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Initialize the plugin by setting localization, filters, and administration functions. |
||
| 86 | * |
||
| 87 | * @since 1.0.0 |
||
| 88 | * |
||
| 89 | * @access private |
||
| 90 | */ |
||
| 91 | public function __construct() { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Sets the variables used throughout the plugin. |
||
| 97 | */ |
||
| 98 | public function set_variables() { |
||
| 99 | parent::set_variables(); |
||
| 100 | |||
| 101 | //https://wetu.com/API/Itinerary/V8/Get |
||
| 102 | if ( false !== $this->api_username && false !== $this->api_password ) { |
||
| 103 | $this->url = 'https://wetu.com/API/Itinerary/'; |
||
| 104 | $this->url_qs = 'username=' . $this->api_username . '&password=' . $this->api_password; |
||
| 105 | } elseif ( false !== $this->api_key ) { |
||
| 106 | $this->url = 'https://wetu.com/API/Itinerary/' . $this->api_key; |
||
| 107 | $this->url_qs = ''; |
||
| 108 | } |
||
| 109 | |||
| 110 | $temp_options = get_option( '_lsx-to_settings',false ); |
||
| 111 | |||
| 112 | if ( false !== $temp_options && isset( $temp_options[ $this->plugin_slug ] ) && ! empty( $temp_options[ $this->plugin_slug ] ) ) { |
||
| 113 | $this->options = $temp_options[ $this->plugin_slug ]; |
||
| 114 | } |
||
| 115 | |||
| 116 | $tour_options = get_option( 'wetu_importer_tour_settings',false ); |
||
| 117 | |||
| 118 | if ( false !== $tour_options ) { |
||
| 119 | $this->tour_options = $tour_options; |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Display the importer administration screen |
||
| 125 | */ |
||
| 126 | public function display_page() { |
||
| 127 | ?> |
||
| 128 | <div class="wrap"> |
||
| 129 | <?php $this->navigation( 'tour' ); ?> |
||
| 130 | |||
| 131 | <?php $this->update_options_form(); ?> |
||
| 132 | |||
| 133 | <?php $this->search_form(); ?> |
||
| 134 | |||
| 135 | <form method="get" action="" id="posts-filter"> |
||
| 136 | <input type="hidden" name="post_type" class="post_type" value="<?php echo esc_attr( $this->tab_slug ); ?>" /> |
||
| 137 | |||
| 138 | <p><input class="button button-primary add" type="button" value="<?php esc_html_e( 'Add to List','wetu-importer' ); ?>" /> |
||
| 139 | <input class="button button-primary clear" type="button" value="<?php esc_html_e( 'Clear','wetu-importer' ); ?>" /> |
||
| 140 | </p> |
||
| 141 | |||
| 142 | <table class="wp-list-table widefat fixed posts"> |
||
| 143 | <?php $this->table_header(); ?> |
||
| 144 | |||
| 145 | <tbody id="the-list"> |
||
| 146 | <tr class="post-0 type-tour status-none" id="post-0"> |
||
| 147 | <th class="check-column" scope="row"> |
||
| 148 | <label for="cb-select-0" class="screen-reader-text"><?php esc_html_e( 'Enter a title to search for and press enter','wetu-importer' ); ?></label> |
||
| 149 | </th> |
||
| 150 | <td class="date column-date column-ref" colspan="4"> |
||
| 151 | <strong> |
||
| 152 | <?php esc_html_e( 'Search for tours using the search form above','wetu-importer' ); ?> |
||
| 153 | </strong> |
||
| 154 | </td> |
||
| 155 | </tr> |
||
| 156 | </tbody> |
||
| 157 | |||
| 158 | <?php $this->table_footer(); ?> |
||
| 159 | |||
| 160 | </table> |
||
| 161 | |||
| 162 | <p><input class="button button-primary add" type="button" value="<?php esc_html_e( 'Add to List','wetu-importer' ); ?>" /> |
||
| 163 | <input class="button button-primary clear" type="button" value="<?php esc_html_e( 'Clear','wetu-importer' ); ?>" /> |
||
| 164 | </p> |
||
| 165 | </form> |
||
| 166 | |||
| 167 | <div style="display:none;" class="import-list-wrapper"> |
||
| 168 | <br /> |
||
| 169 | <form method="get" action="" id="import-list"> |
||
| 170 | |||
| 171 | <div class="row"> |
||
| 172 | <div class="settings-all" style="width:30%;display:block;float:left;"> |
||
| 173 | <h3><?php esc_html_e( 'What content to Sync from WETU' ); ?></h3> |
||
| 174 | <ul> |
||
| 175 | <li><input class="content select-all" <?php $this->checked( $this->tour_options,'all' ); ?> type="checkbox"name="content[]" value="all" /> <?php esc_html_e( 'Select All','wetu-importer' ); ?></li> |
||
|
|
|||
| 176 | |||
| 177 | <?php if ( isset( $this->options ) && ! isset( $this->options['disable_tour_descriptions'] ) ) { ?> |
||
| 178 | <li><input class="content" <?php $this->checked( $this->tour_options,'description' ); ?> type="checkbox" name="content[]" value="description" /> <?php esc_html_e( 'Description','wetu-importer' ); ?></li> |
||
| 179 | <?php } ?> |
||
| 180 | |||
| 181 | <li><input class="content" <?php $this->checked( $this->tour_options,'price' ); ?> type="checkbox" name="content[]" value="price" /> <?php esc_html_e( 'Price','wetu-importer' ); ?></li> |
||
| 182 | <li><input class="content" <?php $this->checked( $this->tour_options,'duration' ); ?> type="checkbox" name="content[]" value="duration" /> <?php esc_html_e( 'Duration','wetu-importer' ); ?></li> |
||
| 183 | <li><input class="content" <?php $this->checked( $this->tour_options,'group_size' ); ?> type="checkbox" name="content[]" value="group_size" /> <?php esc_html_e( 'Group Size','wetu-importer' ); ?></li> |
||
| 184 | <li><input class="content" <?php $this->checked( $this->tour_options,'category' ); ?> type="checkbox" name="content[]" value="category" /> <?php esc_html_e( 'Category','wetu-importer' ); ?></li> |
||
| 185 | <li><input class="content" <?php $this->checked( $this->tour_options,'tags' ); ?> type="checkbox" name="content[]" value="tags" /> <?php esc_html_e( 'Tags','wetu-importer' ); ?></li> |
||
| 186 | |||
| 187 | <li><input class="content" <?php $this->checked( $this->tour_options,'itineraries' ); ?> type="checkbox" name="content[]" value="itineraries" /> <?php esc_html_e( 'Itinerary Days','wetu-importer' ); ?></li> |
||
| 188 | |||
| 189 | <?php |
||
| 190 | /* |
||
| 191 | if ( class_exists( 'LSX_TO_Maps' ) ) { ?> |
||
| 192 | <li><input class="content" <?php $this->checked($this->tour_options,'map'); ?> type="checkbox" name="content[]" value="map" /> <?php esc_html_e('Map Coordinates (generates a KML file)','wetu-importer'); ?></li> |
||
| 193 | <?php } |
||
| 194 | */ |
||
| 195 | ?> |
||
| 196 | </ul> |
||
| 197 | </div> |
||
| 198 | <div class="settings-all" style="width:30%;display:block;float:left;"> |
||
| 199 | <h3><?php esc_html_e( 'Itinerary Info' ); ?></h3> |
||
| 200 | <ul> |
||
| 201 | <li><input class="content" <?php $this->checked( $this->tour_options,'itinerary_description' ); ?> type="checkbox" name="content[]" value="itinerary_description" /> <?php esc_html_e( 'Description','wetu-importer' ); ?></li> |
||
| 202 | <li><input class="content" <?php $this->checked( $this->tour_options,'itinerary_included' ); ?> type="checkbox" name="content[]" value="itinerary_included" /> <?php esc_html_e( 'Included','wetu-importer' ); ?></li> |
||
| 203 | <li><input class="content" <?php $this->checked( $this->tour_options,'itinerary_excluded' ); ?> type="checkbox" name="content[]" value="itinerary_excluded" /> <?php esc_html_e( 'Excluded','wetu-importer' ); ?></li> |
||
| 204 | </ul> |
||
| 205 | |||
| 206 | <h4><?php esc_html_e( 'Additional Content' ); ?></h4> |
||
| 207 | <ul> |
||
| 208 | <li><input class="content" <?php $this->checked( $this->tour_options,'accommodation' ); ?> type="checkbox" name="content[]" value="accommodation" /> <?php esc_html_e( 'Sync Accommodation','wetu-importer' ); ?></li> |
||
| 209 | <li><input class="content" <?php $this->checked( $this->tour_options,'destination' ); ?> type="checkbox" name="content[]" value="destination" /> <?php esc_html_e( 'Sync Destinations','wetu-importer' ); ?></li> |
||
| 210 | <li><input class="content" <?php $this->checked( $this->tour_options,'featured_image' ); ?> type="checkbox" name="content[]" value="featured_image" /> <?php esc_html_e( 'Featured Image','wetu-importer' ); ?></li> |
||
| 211 | <li><input class="content" <?php $this->checked( $this->tour_options,'banner_image' ); ?> type="checkbox" name="content[]" value="banner_image" /> <?php esc_html_e( 'Banner Image','wetu-importer' ); ?></li> |
||
| 212 | </ul> |
||
| 213 | </div> |
||
| 214 | <?php if ( class_exists( 'LSX_TO_Team' ) ) { ?> |
||
| 215 | <div style="width:30%;display:block;float:left;"> |
||
| 216 | <h3><?php esc_html_e( 'Assign a Team Member' ); ?></h3> |
||
| 217 | <?php $this->team_member_checkboxes( $this->tour_options ); ?> |
||
| 218 | </div> |
||
| 219 | <?php } ?> |
||
| 220 | |||
| 221 | <br clear="both" /> |
||
| 222 | </div> |
||
| 223 | |||
| 224 | <h3><?php esc_html_e( 'Your List' ); ?></h3> |
||
| 225 | <p><input class="button button-primary" type="submit" value="<?php esc_html_e( 'Sync','wetu-importer' ); ?>" /></p> |
||
| 226 | <table class="wp-list-table widefat fixed posts"> |
||
| 227 | <?php $this->table_header(); ?> |
||
| 228 | |||
| 229 | <tbody> |
||
| 230 | |||
| 231 | </tbody> |
||
| 232 | |||
| 233 | <?php $this->table_footer(); ?> |
||
| 234 | |||
| 235 | </table> |
||
| 236 | |||
| 237 | <p><input class="button button-primary" type="submit" value="<?php esc_html_e( 'Sync','wetu-importer' ); ?>" /></p> |
||
| 238 | </form> |
||
| 239 | </div> |
||
| 240 | |||
| 241 | <div style="display:none;" class="completed-list-wrapper"> |
||
| 242 | <h3><?php esc_html_e( 'Completed', 'wetu-importer' ); ?> - <small><?php esc_html_e( 'Import your', 'wetu-importer' ); ?> <a href="<?php echo esc_attr( admin_url( 'admin.php' ) ); ?>?page=<?php echo esc_attr( $this->plugin_slug ); ?>&tab=accommodation"><?php esc_html_e( 'accommodation' ); ?></a> <?php esc_html_e( 'next','wetu-importer' ); ?></small></h3> |
||
| 243 | <ul> |
||
| 244 | </ul> |
||
| 245 | </div> |
||
| 246 | </div> |
||
| 247 | <?php |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * search_form |
||
| 252 | */ |
||
| 253 | public function update_options_form() { |
||
| 254 | $tours = get_transient( 'lsx_ti_tours' ); |
||
| 255 | |||
| 256 | echo '<div class="wetu-status tour-wetu-status"><h3>' . esc_html__( 'Wetu Status','wetu-importer' ) . ' - '; |
||
| 257 | |||
| 258 | if ( '' === $tours || false === $tours || isset( $_GET['refresh_tours'] ) ) { |
||
| 259 | $result = $this->update_options(); |
||
| 260 | |||
| 261 | if ( true === $result ) { |
||
| 262 | echo '<span style="color:green;">' . esc_attr( 'Connected','wetu-importer' ) . '</span>'; |
||
| 263 | echo ' - <small><a href="#">' . esc_attr( 'Refresh','wetu-importer' ) . '</a></small>'; |
||
| 264 | } else { |
||
| 265 | echo '<span style="color:red;">' . wp_kses_post( $result ) . '</span>'; |
||
| 266 | } |
||
| 267 | } else { |
||
| 268 | echo '<span style="color:green;">' . esc_attr( 'Connected','wetu-importer' ) . '</span> - <small><a href="#">' . esc_attr( 'Refresh','wetu-importer' ) . '</a></small>'; |
||
| 269 | } |
||
| 270 | |||
| 271 | echo '</h3>'; |
||
| 272 | |||
| 273 | $form_options = get_option( 'lsx_ti_tours_api_options' ); |
||
| 274 | if ( false === $form_options ) { |
||
| 275 | $form_options = array( 0 ); |
||
| 276 | } |
||
| 277 | ?> |
||
| 278 | <form method="get" class="tour-refresh-form" action="<?php echo esc_attr( admin_url( 'admin.php' ) ); ?>"> |
||
| 279 | <input type="hidden" name="page" value="<?php echo esc_attr( $this->plugin_slug ); ?>" /> |
||
| 280 | <input type="hidden" name="tab" value="tour" /> |
||
| 281 | <input type="hidden" name="refresh_tours" value="true" /> |
||
| 282 | |||
| 283 | <p class="tour-search-options"> |
||
| 284 | <label for="own"><input class="content" <?php if ( in_array( 'own',$form_options ) ) { echo 'checked'; } ?> type="checkbox" name="own" value="true" /> <?php esc_html_e( 'Own Tours','wetu-importer' ); ?> </label> |
||
| 285 | </p> |
||
| 286 | |||
| 287 | <p class="tour-search-options"> |
||
| 288 | <label for="type"><input class="content" <?php if ( in_array( 'allitineraries',$form_options ) ) { echo 'checked'; } ?> type="radio" name="type[]" value="allitineraries" /> <?php esc_html_e( 'All','wetu-importer' ); ?></label> |
||
| 289 | <label for="type"><input class="content" <?php if ( in_array( 'sample',$form_options ) ) { echo 'checked'; } ?> type="radio" name="type[]" value="sample" /> <?php esc_html_e( 'Sample','wetu-importer' ); ?></label> |
||
| 290 | <label for="type"><input class="content" <?php if ( in_array( 'personal',$form_options ) ) { echo 'checked'; } ?> type="radio" name="type[]" value="personal" /> <?php esc_html_e( 'Personal','wetu-importer' ); ?></label> |
||
| 291 | </p> |
||
| 292 | |||
| 293 | <p><input class="button button-primary submit" type="submit" value="<?php echo esc_attr( 'Refresh Tours','wetu-importer' ); ?>"></p> |
||
| 294 | </form> |
||
| 295 | <br /> |
||
| 296 | <?php |
||
| 297 | echo '</div>'; |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Save the list of Tours into an option |
||
| 302 | */ |
||
| 303 | public function update_options() { |
||
| 304 | $own = ''; |
||
| 305 | $options = array(); |
||
| 306 | |||
| 307 | delete_option( 'lsx_ti_tours_api_options' ); |
||
| 308 | |||
| 309 | if ( isset( $_GET['own'] ) ) { |
||
| 310 | $this->url_qs .= '&own=true'; |
||
| 311 | $options[] = 'own'; |
||
| 312 | } |
||
| 313 | |||
| 314 | if ( isset( $_GET['type'] ) ) { |
||
| 315 | $this->url_qs .= '&type=' . implode( '',$_GET['type'] ); |
||
| 316 | $options[] = implode( '',$_GET['type'] ); |
||
| 317 | } |
||
| 318 | |||
| 319 | $this->url_qs .= '&results=2000'; |
||
| 320 | |||
| 321 | add_option( 'lsx_ti_tours_api_options',$options ); |
||
| 322 | |||
| 323 | $data = wp_remote_get( $this->url . '/V8/List?' . $this->url_qs ); |
||
| 324 | $tours = json_decode( wp_remote_retrieve_body( $data ), true ); |
||
| 325 | |||
| 326 | if ( isset( $tours['error'] ) ) { |
||
| 327 | return $tours['error']; |
||
| 328 | } elseif ( isset( $tours['itineraries'] ) && ! empty( $tours['itineraries'] ) ) { |
||
| 329 | set_transient( 'lsx_ti_tours',$tours['itineraries'],60 * 60 * 2 ); |
||
| 330 | return true; |
||
| 331 | } |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Grab all the current tour posts via the lsx_wetu_id field. |
||
| 336 | */ |
||
| 337 | View Code Duplication | public function find_current_tours() { |
|
| 338 | global $wpdb; |
||
| 339 | $return = array(); |
||
| 340 | |||
| 341 | $current_tours = $wpdb->get_results(" |
||
| 342 | SELECT key1.post_id,key1.meta_value |
||
| 343 | FROM {$wpdb->postmeta} key1 |
||
| 344 | |||
| 345 | INNER JOIN {$wpdb->posts} key2 |
||
| 346 | ON key1.post_id = key2.ID |
||
| 347 | |||
| 348 | WHERE key1.meta_key = 'lsx_wetu_id' |
||
| 349 | AND key2.post_type = 'tour' |
||
| 350 | |||
| 351 | LIMIT 0,500 |
||
| 352 | "); |
||
| 353 | |||
| 354 | if ( null !== $current_tours && ! empty( $current_tours ) ) { |
||
| 355 | foreach ( $current_tours as $tour ) { |
||
| 356 | $return[ $tour->meta_value ] = $tour; |
||
| 357 | } |
||
| 358 | } |
||
| 359 | |||
| 360 | return $return; |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Run through the accommodation grabbed from the DB. |
||
| 365 | */ |
||
| 366 | public function process_ajax_search() { |
||
| 367 | $return = false; |
||
| 368 | |||
| 369 | // @codingStandardsIgnoreLine |
||
| 370 | if ( isset( $_POST['action'] ) && $_POST['action'] === 'lsx_tour_importer' && isset( $_POST['type'] ) && $_POST['type'] === $this->tab_slug ) { |
||
| 371 | $tours = get_transient( 'lsx_ti_tours' ); |
||
| 372 | |||
| 373 | if ( false !== $tours ) { |
||
| 374 | |||
| 375 | $searched_items = false; |
||
| 376 | |||
| 377 | // @codingStandardsIgnoreLine |
||
| 378 | View Code Duplication | if ( isset( $_POST['keyword'] ) ) { |
|
| 379 | // @codingStandardsIgnoreLine |
||
| 380 | $keyphrases = $_POST['keyword']; |
||
| 381 | } else { |
||
| 382 | $keyphrases = array( 0 ); |
||
| 383 | } |
||
| 384 | |||
| 385 | if ( ! is_array( $keyphrases ) ) { |
||
| 386 | $keyphrases = array( $keyphrases ); |
||
| 387 | } |
||
| 388 | foreach ( $keyphrases as &$keyword ) { |
||
| 389 | $keyword = ltrim( rtrim( $keyword ) ); |
||
| 390 | } |
||
| 391 | |||
| 392 | $post_status = false; |
||
| 393 | if ( in_array( 'publish',$keyphrases ) ) { |
||
| 394 | $post_status = 'publish'; |
||
| 395 | } |
||
| 396 | if ( in_array( 'pending',$keyphrases ) ) { |
||
| 397 | $post_status = 'pending'; |
||
| 398 | } |
||
| 399 | if ( in_array( 'draft',$keyphrases ) ) { |
||
| 400 | $post_status = 'draft'; |
||
| 401 | } |
||
| 402 | if ( in_array( 'import',$keyphrases ) ) { |
||
| 403 | $post_status = 'import'; |
||
| 404 | } |
||
| 405 | |||
| 406 | if ( ! empty( $tours ) ) { |
||
| 407 | $current_tours = $this->find_current_tours(); |
||
| 408 | |||
| 409 | foreach ( $tours as $row_key => $row ) { |
||
| 410 | if ( isset( $row['is_disabled'] ) && true === $row['is_disabled'] ) { |
||
| 411 | continue; |
||
| 412 | } |
||
| 413 | |||
| 414 | /*if('Sample' === $row['type']){ |
||
| 415 | continue; |
||
| 416 | }*/ |
||
| 417 | |||
| 418 | //If this is a current tour, add its ID to the row. |
||
| 419 | $row['post_id'] = 0; |
||
| 420 | |||
| 421 | if ( false !== $current_tours && array_key_exists( $row['identifier'], $current_tours ) ) { |
||
| 422 | $row['post_id'] = $current_tours[ $row['identifier'] ]->post_id; |
||
| 423 | } |
||
| 424 | |||
| 425 | //If we are searching for |
||
| 426 | if ( false !== $post_status ) { |
||
| 427 | if ( 'import' === $post_status ) { |
||
| 428 | |||
| 429 | if ( 0 !== $row['post_id'] ) { |
||
| 430 | continue; |
||
| 431 | } else { |
||
| 432 | $searched_items[ sanitize_title( $row['name'] ) . '-' . $row['identifier'] ] = $this->format_row( $row ); |
||
| 433 | } |
||
| 434 | } else { |
||
| 435 | if ( 0 === $row['post_id'] ) { |
||
| 436 | continue; |
||
| 437 | } else { |
||
| 438 | $current_status = get_post_status( $row['post_id'] ); |
||
| 439 | |||
| 440 | if ( $current_status !== $post_status ) { |
||
| 441 | continue; |
||
| 442 | } |
||
| 443 | } |
||
| 444 | |||
| 445 | $searched_items[ sanitize_title( $row['name'] ) . '-' . $row['identifier'] ] = $this->format_row( $row ); |
||
| 446 | } |
||
| 447 | } else { |
||
| 448 | //Search through each keyword. |
||
| 449 | foreach ( $keyphrases as $keyphrase ) { |
||
| 450 | |||
| 451 | //Make sure the keyphrase is turned into an array |
||
| 452 | $keywords = explode( ' ',$keyphrase ); |
||
| 453 | if ( ! is_array( $keywords ) ) { |
||
| 454 | $keywords = array( $keywords ); |
||
| 455 | } |
||
| 456 | |||
| 457 | if ( $this->multineedle_stripos( ltrim( rtrim( $row['name'] ) ), $keywords ) !== false ) { |
||
| 458 | $searched_items[ sanitize_title( $row['name'] ) . '-' . $row['identifier'] ] = $this->format_row( $row ); |
||
| 459 | } else if ( $this->multineedle_stripos( ltrim( rtrim( $row['reference_number'] ) ), $keywords ) !== false ) { |
||
| 460 | $searched_items[ sanitize_title( $row['name'] ) . '-' . $row['identifier'] ] = $this->format_row( $row ); |
||
| 461 | }else if ( $this->multineedle_stripos( ltrim( rtrim( $row['identifier_key'] ) ), $keywords ) !== false ) { |
||
| 462 | $searched_items[ sanitize_title( $row['name'] ) . '-' . $row['identifier'] ] = $this->format_row( $row ); |
||
| 463 | } |
||
| 464 | |||
| 465 | //Add in the year and ref |
||
| 466 | } |
||
| 467 | } |
||
| 468 | } |
||
| 469 | } |
||
| 470 | |||
| 471 | if ( false !== $searched_items ) { |
||
| 472 | ksort( $searched_items ); |
||
| 473 | $return = implode( $searched_items ); |
||
| 474 | } |
||
| 475 | } |
||
| 476 | print_r( $return ); |
||
| 477 | die(); |
||
| 478 | } |
||
| 479 | } |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Formats the row for output on the screen. |
||
| 483 | */ |
||
| 484 | public function format_row( $row = false ) { |
||
| 485 | if ( false !== $row ) { |
||
| 486 | $status = 'import'; |
||
| 487 | |||
| 488 | if ( 0 !== $row['post_id'] ) { |
||
| 489 | $status = '<a href="' . admin_url( '/post.php?post=' . $row['post_id'] . '&action=edit' ) . '" target="_blank">' . get_post_status( $row['post_id'] ) . '</a>'; |
||
| 490 | } |
||
| 491 | |||
| 492 | $row_html = ' |
||
| 493 | <tr class="post-' . $row['post_id'] . ' type-tour" id="post-' . $row['post_id'] . '"> |
||
| 494 | <th class="check-column" scope="row"> |
||
| 495 | <label for="cb-select-' . $row['identifier'] . '" class="screen-reader-text">' . $row['name'] . '</label> |
||
| 496 | <input type="checkbox" data-identifier="' . $row['identifier'] . '" value="' . $row['post_id'] . '" name="post[]" id="cb-select-' . $row['identifier'] . '"> |
||
| 497 | </th> |
||
| 498 | <td class="post-title page-title column-title"> |
||
| 499 | <strong>' . $row['name'] . '</strong> - ' . $status . ' |
||
| 500 | </td> |
||
| 501 | <td class="date column-date"> |
||
| 502 | ' . $row['reference_number'] . ' |
||
| 503 | </td> |
||
| 504 | <td class="date column-date"> |
||
| 505 | <abbr title="' . date( 'Y/m/d',strtotime( $row['last_modified'] ) ) . '">' . date( 'Y/m/d',strtotime( $row['last_modified'] ) ) . '</abbr><br>Last Modified |
||
| 506 | </td> |
||
| 507 | <td class="ssid column-ssid"> |
||
| 508 | ' . $row['identifier'] . ' |
||
| 509 | </td> |
||
| 510 | </tr>'; |
||
| 511 | return $row_html; |
||
| 512 | } |
||
| 513 | } |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Connect to wetu |
||
| 517 | */ |
||
| 518 | public function process_ajax_import( $force = false ) { |
||
| 519 | $return = false; |
||
| 520 | |||
| 521 | // @codingStandardsIgnoreLine |
||
| 522 | if ( isset( $_POST['action'] ) && $_POST['action'] === 'lsx_import_items' && isset( $_POST['type'] ) && $_POST['type'] === $this->tab_slug && isset( $_POST['wetu_id'] ) ) { |
||
| 523 | |||
| 524 | // @codingStandardsIgnoreLine |
||
| 525 | $wetu_id = $_POST['wetu_id']; |
||
| 526 | |||
| 527 | // @codingStandardsIgnoreLine |
||
| 528 | if ( isset( $_POST['post_id'] ) ) { |
||
| 529 | // @codingStandardsIgnoreLine |
||
| 530 | $post_id = $_POST['post_id']; |
||
| 531 | } else { |
||
| 532 | $post_id = 0; |
||
| 533 | } |
||
| 534 | |||
| 535 | delete_option( 'wetu_importer_tour_settings' ); |
||
| 536 | |||
| 537 | // @codingStandardsIgnoreLine |
||
| 538 | View Code Duplication | if ( isset( $_POST['content'] ) && is_array( $_POST['content'] ) && ! empty( $_POST['content'] ) ) { |
|
| 539 | // @codingStandardsIgnoreLine |
||
| 540 | $content = $_POST['content']; |
||
| 541 | add_option( 'wetu_importer_tour_settings',$content ); |
||
| 542 | } else { |
||
| 543 | $content = false; |
||
| 544 | } |
||
| 545 | $jdata = wp_remote_get( 'https://wetu.com/API/Itinerary/V8/Get?id=' . $wetu_id ); |
||
| 546 | |||
| 547 | View Code Duplication | if ( ! empty( $jdata ) && isset( $jdata['response'] ) && isset( $jdata['response']['code'] ) && 200 === $jdata['response']['code'] ) { |
|
| 548 | $jdata = json_decode( $jdata['body'], true ); |
||
| 549 | $return = $this->import_row( $jdata, $wetu_id, $post_id, $content ); |
||
| 550 | $this->format_completed_row( $return ); |
||
| 551 | $this->save_queue(); |
||
| 552 | $this->cleanup_posts(); |
||
| 553 | $this->attach_destination_images( $content ); |
||
| 554 | $this->clean_attached_destinations( $return ); |
||
| 555 | } else { |
||
| 556 | $this->format_error( esc_html__( 'There was a problem importing your tour, please contact support.', 'wetu-importer' ) ); |
||
| 557 | } |
||
| 558 | } |
||
| 559 | } |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Amends the tours destinations instead of replace. |
||
| 563 | * |
||
| 564 | * @param $id string |
||
| 565 | * @return void |
||
| 566 | */ |
||
| 567 | public function clean_attached_destinations( $id ) { |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Connect to wetu |
||
| 579 | * |
||
| 580 | * @param $data array |
||
| 581 | * @param $wetu_id string |
||
| 582 | */ |
||
| 583 | public function import_row( $data, $wetu_id, $id = 0, $importable_content = array(), $old1 = false, $old2 = false ) { |
||
| 584 | $post_name = ''; |
||
| 673 | |||
| 674 | /** |
||
| 675 | * A loop which runs through each leg on the tour. |
||
| 676 | */ |
||
| 677 | public function process_itineraries( $data, $id, $importable_content ) { |
||
| 840 | |||
| 841 | /** |
||
| 842 | * Run through your routes and save the points as a KML file. |
||
| 843 | */ |
||
| 844 | public function set_map_data( $data, $id, $zoom = 9 ) { |
||
| 871 | |||
| 872 | // CLASS SPECIFIC FUNCTIONS. |
||
| 873 | |||
| 874 | /** |
||
| 875 | * Set the Itinerary Day |
||
| 876 | */ |
||
| 877 | public function set_itinerary_day( $day, $id ) { |
||
| 880 | |||
| 881 | /** |
||
| 882 | * Set the price |
||
| 883 | */ |
||
| 884 | public function set_price( $data, $id ) { |
||
| 901 | |||
| 902 | /** |
||
| 903 | * Set the duration |
||
| 904 | */ |
||
| 905 | public function set_duration( $data, $id ) { |
||
| 912 | |||
| 913 | /** |
||
| 914 | * Set the group size |
||
| 915 | */ |
||
| 916 | public function set_group_size( $data, $id ) { |
||
| 922 | |||
| 923 | /** |
||
| 924 | * Takes the WETU tags and sets the Travel Styles. |
||
| 925 | * |
||
| 926 | * @param string $id |
||
| 927 | * @param array $travel_styles |
||
| 928 | * @return void |
||
| 929 | */ |
||
| 930 | public function set_travel_styles( $id, $data ) { |
||
| 937 | |||
| 938 | /** |
||
| 939 | * Connects the Accommodation if its available |
||
| 940 | */ |
||
| 941 | public function set_accommodation( $day, $id ) { |
||
| 966 | |||
| 967 | /** |
||
| 968 | * Grab all the current accommodation posts via the lsx_wetu_id field. |
||
| 969 | * |
||
| 970 | * @param $post_type string |
||
| 971 | * @return boolean / array |
||
| 972 | */ |
||
| 973 | public function find_current_accommodation( $post_type = 'accommodation' ) { |
||
| 986 | |||
| 987 | /** |
||
| 988 | * Grab all the current accommodation posts via the lsx_wetu_id field. |
||
| 989 | * @return boolean / array |
||
| 990 | */ |
||
| 991 | public function find_current_destinations() { |
||
| 994 | |||
| 995 | /** |
||
| 996 | * Connects the destinations post type |
||
| 997 | * |
||
| 998 | * @param $day array |
||
| 999 | * @param $id string |
||
| 1000 | * @return boolean / string |
||
| 1001 | */ |
||
| 1002 | public function set_destination( $day, $id, $leg_counter ) { |
||
| 1088 | |||
| 1089 | /** |
||
| 1090 | * Connects the destinations post type |
||
| 1091 | * |
||
| 1092 | * @param $dest_id string |
||
| 1093 | * @param $country_id array |
||
| 1094 | * @param $id string |
||
| 1095 | * |
||
| 1096 | * @return string |
||
| 1097 | */ |
||
| 1098 | public function set_country( $country_wetu_id, $id ) { |
||
| 1149 | |||
| 1150 | /** |
||
| 1151 | * Connects the destinations post type |
||
| 1152 | * |
||
| 1153 | * @param $dest_id string |
||
| 1154 | * @param $country_id array |
||
| 1155 | * @param $id string |
||
| 1156 | * |
||
| 1157 | * @return string |
||
| 1158 | */ |
||
| 1159 | public function attach_destination_images( $importable_content = array() ) { |
||
| 1203 | |||
| 1204 | /** |
||
| 1205 | * Creates the main gallery data |
||
| 1206 | */ |
||
| 1207 | public function set_featured_image( $data, $id ) { |
||
| 1238 | |||
| 1239 | /** |
||
| 1240 | * Sets a banner image |
||
| 1241 | */ |
||
| 1242 | public function set_banner_image( $data, $id, $content = array( 'none' ) ) { |
||
| 1285 | |||
| 1286 | /** |
||
| 1287 | * Grabs all of the current used featured images on the site. |
||
| 1288 | */ |
||
| 1289 | public function check_if_image_is_used( $v ) { |
||
| 1343 | |||
| 1344 | /** |
||
| 1345 | * Que an item to be saved. |
||
| 1346 | * |
||
| 1347 | * @param $id int |
||
| 1348 | */ |
||
| 1349 | public function queue_item( $id ) { |
||
| 1356 | |||
| 1357 | /** |
||
| 1358 | * Saves the queue to the option. |
||
| 1359 | */ |
||
| 1360 | public function save_queue() { |
||
| 1376 | |||
| 1377 | /** |
||
| 1378 | * The header of the item list |
||
| 1379 | */ |
||
| 1380 | View Code Duplication | public function table_header() { |
|
| 1396 | |||
| 1397 | /** |
||
| 1398 | * The footer of the item list |
||
| 1399 | */ |
||
| 1400 | View Code Duplication | public function table_footer() { |
|
| 1416 | |||
| 1417 | } |
||
| 1418 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: