| Total Complexity | 64 |
| Total Lines | 271 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like LSX_Projects_SCPO_Engine 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 LSX_Projects_SCPO_Engine, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class LSX_Projects_SCPO_Engine { |
||
| 15 | |||
| 16 | function __construct() { |
||
| 17 | if ( ! get_option( 'lsx_projects_scporder_install' ) ) { |
||
| 18 | $this->lsx_projects_scporder_install(); |
||
| 19 | } |
||
| 20 | |||
| 21 | add_action( 'admin_init', array( $this, 'refresh' ) ); |
||
| 22 | add_action( 'admin_init', array( $this, 'load_script_css' ) ); |
||
| 23 | |||
| 24 | add_action( 'wp_ajax_update-menu-order', array( $this, 'update_menu_order' ) ); |
||
| 25 | |||
| 26 | add_action( 'pre_get_posts', array( $this, 'lsx_projects_scporder_pre_get_posts' ) ); |
||
| 27 | |||
| 28 | add_filter( 'get_previous_post_where', array( $this, 'lsx_projects_scporder_previous_post_where' ) ); |
||
| 29 | add_filter( 'get_previous_post_sort', array( $this, 'lsx_projects_scporder_previous_post_sort' ) ); |
||
| 30 | add_filter( 'get_next_post_where', array( $this, 'lsx_projects_scporder_next_post_where' ) ); |
||
| 31 | add_filter( 'get_next_post_sort', array( $this, 'lsx_projects_scporder_next_post_sort' ) ); |
||
| 32 | } |
||
| 33 | |||
| 34 | function lsx_projects_scporder_install() { |
||
| 35 | update_option( 'lsx_projects_scporder_install', 1 ); |
||
| 36 | } |
||
| 37 | |||
| 38 | function _check_load_script_css() { |
||
| 39 | $active = false; |
||
| 40 | $objects = $this->get_lsx_projects_scporder_options_objects(); |
||
| 41 | |||
| 42 | if ( empty( $objects ) ) { |
||
| 43 | return false; |
||
| 44 | } |
||
| 45 | |||
| 46 | if ( isset( $_GET['orderby'] ) || strstr( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ), 'action=edit' ) || strstr( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ), 'wp-admin/post-new.php' ) ) { |
||
| 47 | return false; |
||
| 48 | } |
||
| 49 | |||
| 50 | if ( ! empty( $objects ) ) { |
||
| 51 | if ( isset( $_GET['post_type'] ) && ! isset( $_GET['taxonomy'] ) && array_key_exists( sanitize_text_field( wp_unslash( $_GET['post_type'] ) ), $objects ) ) { // if page or custom post types |
||
| 52 | $active = true; |
||
| 53 | } |
||
| 54 | |||
| 55 | if ( ! isset( $_GET['post_type'] ) && strstr( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ), 'wp-admin/edit.php' ) && array_key_exists( 'post', $objects ) ) { // if post |
||
| 56 | $active = true; |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | return $active; |
||
| 61 | } |
||
| 62 | |||
| 63 | function load_script_css() { |
||
| 64 | if ( $this->_check_load_script_css() ) { |
||
| 65 | wp_enqueue_script( 'scporderjs', LSX_PROJECTS_URL . 'assets/js/scporder.min.js', array( 'jquery', 'jquery-ui-sortable' ), null, true ); |
||
| 66 | |||
| 67 | $scporderjs_params = array( |
||
| 68 | 'ajax_url' => admin_url( 'admin-ajax.php' ), |
||
| 69 | 'ajax_nonce' => wp_create_nonce( 'scporder' ), |
||
| 70 | ); |
||
| 71 | |||
| 72 | wp_localize_script( 'scporderjs', 'scporderjs_params', $scporderjs_params ); |
||
| 73 | |||
| 74 | wp_enqueue_style( 'scporder', LSX_PROJECTS_URL . 'assets/css/scporder.css', array(), null ); |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | function refresh() { |
||
| 79 | global $wpdb; |
||
| 80 | |||
| 81 | $objects = $this->get_lsx_projects_scporder_options_objects(); |
||
| 82 | |||
| 83 | if ( ! empty( $objects ) ) { |
||
| 84 | foreach ( $objects as $object => $object_data ) { |
||
| 85 | $result = $wpdb->get_results($wpdb->prepare(" |
||
| 86 | SELECT count(*) as cnt, max(menu_order) as max, min(menu_order) as min |
||
| 87 | FROM $wpdb->posts |
||
| 88 | WHERE post_type = '%s' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future') |
||
| 89 | ", $object)); |
||
| 90 | |||
| 91 | if ( 0 == $result[0]->cnt || $result[0]->cnt == $result[0]->max ) { |
||
| 92 | continue; |
||
| 93 | } |
||
| 94 | |||
| 95 | $results = $wpdb->get_results( $wpdb->prepare(" |
||
| 96 | SELECT ID |
||
| 97 | FROM $wpdb->posts |
||
| 98 | WHERE post_type = '%s' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future') |
||
| 99 | ORDER BY menu_order ASC |
||
| 100 | ", $object ) ); |
||
| 101 | |||
| 102 | foreach ( $results as $key => $result ) { |
||
| 103 | $wpdb->update( |
||
| 104 | $wpdb->posts, |
||
| 105 | array( |
||
| 106 | 'menu_order' => $key + 1, |
||
| 107 | ), |
||
| 108 | array( |
||
| 109 | 'ID' => $result->ID, |
||
| 110 | ) |
||
| 111 | ); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | function update_menu_order() { |
||
| 118 | check_ajax_referer( 'scporder', 'security' ); |
||
| 119 | |||
| 120 | global $wpdb; |
||
| 121 | |||
| 122 | parse_str( sanitize_text_field( wp_unslash( $_POST['order'] ) ), $data ); |
||
| 123 | |||
| 124 | if ( ! is_array( $data ) ) { |
||
| 125 | return false; |
||
| 126 | } |
||
| 127 | |||
| 128 | $id_arr = array(); |
||
| 129 | |||
| 130 | foreach ( $data as $key => $values ) { |
||
| 131 | foreach ( $values as $position => $id ) { |
||
| 132 | $id_arr[] = $id; |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | $menu_order_arr = array(); |
||
| 137 | |||
| 138 | foreach ( $id_arr as $key => $id ) { |
||
| 139 | $results = $wpdb->get_results( "SELECT menu_order FROM $wpdb->posts WHERE ID = " . intval( $id ) ); |
||
| 140 | |||
| 141 | foreach ( $results as $result ) { |
||
| 142 | $menu_order_arr[] = $result->menu_order; |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | sort( $menu_order_arr ); |
||
| 147 | |||
| 148 | foreach ( $data as $key => $values ) { |
||
| 149 | foreach ( $values as $position => $id ) { |
||
| 150 | $wpdb->update( |
||
| 151 | $wpdb->posts, |
||
| 152 | array( |
||
| 153 | 'menu_order' => $menu_order_arr[ $position ], |
||
| 154 | ), |
||
| 155 | array( |
||
| 156 | 'ID' => intval( $id ), |
||
| 157 | ) |
||
| 158 | ); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | function lsx_projects_scporder_previous_post_where( $where ) { |
||
| 178 | } |
||
| 179 | |||
| 180 | function lsx_projects_scporder_previous_post_sort( $orderby ) { |
||
| 181 | global $post; |
||
| 182 | |||
| 183 | $objects = $this->get_lsx_projects_scporder_options_objects(); |
||
| 184 | |||
| 185 | if ( empty( $objects ) ) { |
||
| 186 | return $orderby; |
||
| 187 | } |
||
| 188 | |||
| 189 | if ( isset( $post->post_type ) && array_key_exists( $post->post_type, $objects ) ) { |
||
| 190 | $orderby = 'ORDER BY p.menu_order ASC LIMIT 1'; |
||
| 191 | } |
||
| 192 | |||
| 193 | return $orderby; |
||
| 194 | } |
||
| 195 | |||
| 196 | function lsx_projects_scporder_next_post_where( $where ) { |
||
| 197 | global $post; |
||
| 198 | |||
| 199 | $objects = $this->get_lsx_projects_scporder_options_objects(); |
||
| 200 | |||
| 201 | if ( empty( $objects ) ) { |
||
| 202 | return $where; |
||
| 203 | } |
||
| 204 | |||
| 205 | if ( isset( $post->post_type ) && array_key_exists( $post->post_type, $objects ) ) { |
||
| 206 | $current_menu_order = $post->menu_order; |
||
| 207 | $where = "WHERE p.menu_order < '" . $current_menu_order . "' AND p.post_type = '" . $post->post_type . "' AND p.post_status = 'publish'"; |
||
| 208 | } |
||
| 209 | |||
| 210 | return $where; |
||
| 211 | } |
||
| 212 | |||
| 213 | function lsx_projects_scporder_next_post_sort( $orderby ) { |
||
| 214 | global $post; |
||
| 215 | |||
| 216 | $objects = $this->get_lsx_projects_scporder_options_objects(); |
||
| 217 | |||
| 218 | if ( empty( $objects ) ) { |
||
| 219 | return $orderby; |
||
| 220 | } |
||
| 221 | |||
| 222 | if ( isset( $post->post_type ) && array_key_exists( $post->post_type, $objects ) ) { |
||
| 223 | $orderby = 'ORDER BY p.menu_order DESC LIMIT 1'; |
||
| 224 | } |
||
| 225 | |||
| 226 | return $orderby; |
||
| 227 | } |
||
| 228 | |||
| 229 | function lsx_projects_scporder_pre_get_posts( $wp_query ) { |
||
| 277 | } |
||
| 278 | } |
||
| 279 | } |
||
| 280 | } |
||
| 281 | |||
| 282 | function get_lsx_projects_scporder_options_objects() { |
||
| 285 | ); |
||
| 286 | } |
||
| 287 | |||
| 288 | } |
||
| 289 | |||
| 316 |