| Total Complexity | 50 |
| Total Lines | 284 |
| Duplicated Lines | 0 % |
| Changes | 14 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Cron 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.
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 Cron, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Cron { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Holds class instance |
||
| 21 | * |
||
| 22 | * @since 1.0.0 |
||
| 23 | * |
||
| 24 | * @var object|Module_Template |
||
|
|
|||
| 25 | */ |
||
| 26 | protected static $instance = null; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Initialize the plugin by setting localization, filters, and administration functions. |
||
| 30 | * |
||
| 31 | * @since 1.0.0 |
||
| 32 | * |
||
| 33 | * @access private |
||
| 34 | */ |
||
| 35 | public function __construct() { |
||
| 36 | add_filter( 'cron_schedules', array( $this, 'register_schedule' ), 10, 1 ); |
||
| 37 | add_action( 'lsx_wetu_importer_settings_before', array( $this, 'watch_for_trigger' ), 200 ); |
||
| 38 | add_action( 'lsx_wetu_accommodation_images_cron', array( $this, 'process' ), 10, 1 ); |
||
| 39 | add_action( 'lsx_wetu_accommodation_images_sync', array( $this, 'cron_callback' ), 10, 1 ); |
||
| 40 | add_filter( 'cmb_meta_boxes', array( $this, 'metaboxes' ) ); |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Return an instance of this class. |
||
| 45 | * |
||
| 46 | * @since 1.0.0 |
||
| 47 | * |
||
| 48 | * @return object Cron() A single instance of this class. |
||
| 49 | */ |
||
| 50 | public static function get_instance() { |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Define the metabox and field configurations. |
||
| 60 | * |
||
| 61 | * @param array $meta_boxes |
||
| 62 | * @return array |
||
| 63 | */ |
||
| 64 | public function metaboxes( array $meta_boxes ) { |
||
| 65 | // Allowed post types. |
||
| 66 | $allowed_post_types = array( 'accommodation' ); |
||
| 67 | |||
| 68 | $fields = array(); |
||
| 69 | |||
| 70 | $fields[] = array( |
||
| 71 | 'id' => 'wetu_skip_banner', |
||
| 72 | 'name' => esc_html__( 'Skip Banner Image', 'lsx-banners' ), |
||
| 73 | 'type' => 'checkbox', |
||
| 74 | ); |
||
| 75 | |||
| 76 | $fields[] = array( |
||
| 77 | 'id' => 'wetu_skip_featured', |
||
| 78 | 'name' => esc_html__( 'Skip Featured Image', 'lsx-banners' ), |
||
| 79 | 'type' => 'checkbox', |
||
| 80 | ); |
||
| 81 | |||
| 82 | $meta_boxes[] = array( |
||
| 83 | 'title' => esc_html__( 'WETU Settings', 'lsx-banners' ), |
||
| 84 | 'pages' => $allowed_post_types, |
||
| 85 | 'fields' => $fields, |
||
| 86 | 'context' => 'side', |
||
| 87 | 'priority' => 'low', |
||
| 88 | ); |
||
| 89 | |||
| 90 | return $meta_boxes; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Registers a 5 min schedule for us to use. |
||
| 95 | * |
||
| 96 | * @param array $schedules |
||
| 97 | * @return array |
||
| 98 | */ |
||
| 99 | public function register_schedule( $schedules ) { |
||
| 100 | $schedules['wetu-5-minutes'] = array( |
||
| 101 | 'interval' => 5 * MINUTE_IN_SECONDS, |
||
| 102 | 'display' => __( 'Every 5 minutes', 'lsx-wetu-importer' ), |
||
| 103 | ); |
||
| 104 | return $schedules; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Watches for changes in the button triggers. |
||
| 109 | * |
||
| 110 | * @return void |
||
| 111 | */ |
||
| 112 | public function watch_for_trigger() { |
||
| 135 | } |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Remove our cron from the shedule. |
||
| 141 | * |
||
| 142 | * @return void |
||
| 143 | */ |
||
| 144 | public function deactivate( $task = 'lsx_wetu_accommodation_images_cron' ) { |
||
| 145 | wp_clear_scheduled_hook( $task, array( $task ) ); |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * This function will schedule the cron event. |
||
| 150 | * |
||
| 151 | * @param string $task |
||
| 152 | * @param string $schedule |
||
| 153 | * @param string $time |
||
| 154 | * @return void |
||
| 155 | */ |
||
| 156 | public function schedule( $task = 'lsx_wetu_accommodation_images_cron', $schedule = 'weekly', $time = 'Sunday 10pm' ) { |
||
| 157 | $args = array( $task ); |
||
| 158 | if ( '' === $time ) { |
||
| 159 | $time = time(); |
||
| 160 | } |
||
| 161 | |||
| 162 | if ( isset( $_GET['accommodation_images_cron_featured'] ) && '' !== $_GET['accommodation_images_cron_featured'] ) { |
||
| 163 | $args[] = 'featured_image'; |
||
| 164 | } |
||
| 165 | wp_schedule_event( $time, $schedule, $task, $args ); |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * This is the function that will be triggered by the cron event. |
||
| 170 | * |
||
| 171 | * @return void |
||
| 172 | */ |
||
| 173 | public function process( $task = '' ) { |
||
| 174 | switch ( $task ) { |
||
| 175 | case 'lsx_wetu_accommodation_images_cron': |
||
| 176 | $this->register_accommodation_images_sync(); |
||
| 177 | break; |
||
| 178 | |||
| 179 | default: |
||
| 180 | break; |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * This is the function that will be triggered by the cron event. |
||
| 186 | * |
||
| 187 | * @return void |
||
| 188 | */ |
||
| 189 | public function register_accommodation_images_sync() { |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * This is the function that will be triggered by the cron event. |
||
| 199 | * |
||
| 200 | * @return void |
||
| 201 | */ |
||
| 202 | public function cron_callback( $task = '', $featured_image = '' ) { |
||
| 203 | $has_accommodation = get_option( $task ); |
||
| 204 | if ( false !== $has_accommodation && ! empty( $has_accommodation ) ) { |
||
| 205 | $next_time = array_slice( $has_accommodation, 3 ); |
||
| 206 | $this_time = array_slice( $has_accommodation, 0, 2 ); |
||
| 207 | |||
| 208 | $api_key = $this->get_api_key(); |
||
| 209 | $url = 'https://wetu.com/API/Pins/' . $api_key . '/Get?all=include&ids='; |
||
| 210 | |||
| 211 | // Run through the current items. |
||
| 212 | foreach ( $this_time as $accommodation ) { |
||
| 213 | $wetu_id = get_post_meta( $accommodation, 'lsx_wetu_id', true ); |
||
| 214 | $last_date = get_post_meta( $accommodation, 'lsx_wetu_modified_date', true ); |
||
| 215 | |||
| 216 | // Grabbing the image sync. |
||
| 217 | $featured_image = get_post_meta( $accommodation, 'wetu_skip_featured', true ); |
||
| 218 | $banner_image = get_post_meta( $accommodation, 'wetu_skip_banner', true ); |
||
| 219 | |||
| 220 | $accommodation_info = wp_remote_get( $url . $wetu_id ); |
||
| 221 | if ( ! empty( $accommodation_info ) && isset( $accommodation_info['response'] ) && isset( $accommodation_info['response']['code'] ) && 200 === $accommodation_info['response']['code'] ) { |
||
| 222 | $adata = json_decode( $accommodation_info['body'], true ); |
||
| 223 | |||
| 224 | if ( isset( $adata[0] ) && isset( $adata[0]['last_modified'] ) && '' !== $adata[0]['last_modified'] ) { |
||
| 225 | $modified_time = strtotime( $adata[0]['last_modified'] ); |
||
| 226 | if ( $modified_time > $last_date ) { |
||
| 227 | $accommodation_importer = new \LSX_WETU_Importer_Accommodation(); |
||
| 228 | |||
| 229 | if ( false === $banner_image || '' === $banner_image ) { |
||
| 230 | $accommodation_importer->set_banner_image( $adata, $accommodation ); |
||
| 231 | } |
||
| 232 | |||
| 233 | if ( false === $featured_image || '' === $featured_image ) { |
||
| 234 | $accommodation_importer->set_featured_image( $adata, $accommodation ); |
||
| 235 | } |
||
| 236 | |||
| 237 | $accommodation_importer->create_main_gallery( $adata, $accommodation ); |
||
| 238 | update_post_meta( $accommodation, 'lsx_wetu_modified_date', $modified_time, $last_date ); |
||
| 239 | } |
||
| 240 | } |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | // Save the values for next time. |
||
| 245 | if ( ! empty( $next_time ) ) { |
||
| 246 | update_option( $task, $next_time ); |
||
| 247 | } else { |
||
| 248 | delete_option( $task ); |
||
| 249 | $this->deactivate( $task ); |
||
| 250 | } |
||
| 251 | } else { |
||
| 252 | $this->deactivate( $task ); |
||
| 253 | update_option( 'lsx_wetu_nexttime', $task ); |
||
| 254 | } |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * This will grab the accommodation ids and load them up into an option field. |
||
| 259 | * |
||
| 260 | * @param string $task |
||
| 261 | * @return void |
||
| 262 | */ |
||
| 263 | public function load_items_to_sync( $task = 'accommodation_images' ) { |
||
| 281 | } |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Gets the API key stored in the options table. |
||
| 286 | * |
||
| 287 | * @return string |
||
| 288 | */ |
||
| 289 | public function get_api_key() { |
||
| 304 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths