| Total Complexity | 44 |
| Total Lines | 270 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like MAG_CMB2_Field_Post_Search_Ajax 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 MAG_CMB2_Field_Post_Search_Ajax, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class MAG_CMB2_Field_Post_Search_Ajax { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Current version number |
||
| 15 | */ |
||
| 16 | const VERSION = '1.0.0'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * The url which is used to load local resources |
||
| 20 | * |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | protected static $url = ''; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Holds class instance |
||
| 27 | * |
||
| 28 | * @since 1.0.0 |
||
| 29 | * |
||
| 30 | * @var object \MAG_CMB2_Field_Post_Search_Ajax |
||
| 31 | */ |
||
| 32 | protected static $instance = null; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Initialize the plugin by hooking into CMB2 |
||
| 36 | */ |
||
| 37 | public function __construct() { |
||
| 38 | add_action( 'cmb2_render_post_search_ajax', array( $this, 'render' ), 10, 5 ); |
||
| 39 | add_action( 'cmb2_sanitize_post_search_ajax', array( $this, 'sanitize' ), 10, 4 ); |
||
| 40 | add_action( 'wp_ajax_cmb_post_search_ajax_get_results', array( $this, 'cmb_post_search_ajax_get_results' ) ); |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Return an instance of this class. |
||
| 45 | * |
||
| 46 | * @since 1.0.0 |
||
| 47 | * |
||
| 48 | * @return object \MAG_CMB2_Field_Post_Search_Ajax A single instance of this class. |
||
| 49 | */ |
||
| 50 | public static function get_instance() { |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Render field |
||
| 60 | */ |
||
| 61 | public function render( $field, $value, $object_id, $object_type, $field_type ) { |
||
| 62 | $this->setup_admin_scripts(); |
||
| 63 | $field_name = $field->_name(); |
||
| 64 | |||
| 65 | if ( $field->args( 'limit' ) > 1 ) { |
||
| 66 | |||
| 67 | echo '<ul class="cmb-post-search-ajax-results" id="' . $field_name . '_results">'; |
||
| 68 | if ( isset( $value ) && ! empty( $value ) ) { |
||
| 69 | if ( ! is_array( $value ) ) { |
||
| 70 | $value = array( $value ); |
||
| 71 | } |
||
| 72 | $value = array_unique( $value ); |
||
| 73 | foreach ( $value as $val ) { |
||
| 74 | $handle = ( $field->args( 'sortable' ) ) ? '<span class="hndl"></span>' : ''; |
||
| 75 | $li_css = ''; |
||
| 76 | if ( $field->args( 'object_type' ) == 'user' ) { |
||
| 77 | $guid = get_edit_user_link( $val ); |
||
| 78 | $user = get_userdata( $val ); |
||
| 79 | $title = $user->display_name; |
||
| 80 | } else { |
||
| 81 | $guid = get_edit_post_link( $val ); |
||
| 82 | $title = get_the_title( $val ); |
||
| 83 | if ( 'trash' === get_post_status( $val ) ) { |
||
| 84 | $li_css = 'display:none;'; |
||
| 85 | } |
||
| 86 | } |
||
| 87 | echo '<li style="' . $li_css . '">' . $handle . '<input type="hidden" name="' . $field_name . '_results[]" value="' . $val . '"><a href="' . $guid . '" target="_blank" class="edit-link">' . $title . '</a><a class="remover"><span class="dashicons dashicons-no"></span><span class="dashicons dashicons-dismiss"></span></a></li>'; |
||
| 88 | } |
||
| 89 | } |
||
| 90 | echo '</ul>'; |
||
| 91 | $field_value = ''; |
||
| 92 | } else { |
||
| 93 | if ( is_array( $value ) ) { |
||
| 94 | $value = $value[0]; |
||
| 95 | } |
||
| 96 | if ( $field->args( 'object_type' ) == 'user' ) { |
||
| 97 | $field_value = ( $value ? get_userdata( $value )->display_name : '' ); |
||
| 98 | } else { |
||
| 99 | $field_value = ( $value ? get_the_title( $value ) : '' ); |
||
| 100 | } |
||
| 101 | echo $field_type->input( |
||
| 102 | array( |
||
| 103 | 'type' => 'hidden', |
||
| 104 | 'name' => $field_name . '_results', |
||
| 105 | 'value' => $value, |
||
| 106 | 'desc' => false, |
||
| 107 | ) |
||
| 108 | ); |
||
| 109 | if ( isset( $field->group ) ) { |
||
| 110 | $store_name = str_replace( '][', '_', $field_name ); |
||
| 111 | $store_name = str_replace( ']', '', $store_name ); |
||
| 112 | $store_name = str_replace( '[', '_', $store_name ); |
||
| 113 | |||
| 114 | echo $field_type->input( |
||
| 115 | array( |
||
| 116 | 'type' => 'hidden', |
||
| 117 | 'id' => $field_name . '_store', |
||
| 118 | 'name' => $store_name . '_store', |
||
| 119 | 'class' => 'cmb-post-search-ajax-store', |
||
| 120 | 'value' => $value, |
||
| 121 | 'desc' => false, |
||
| 122 | ) |
||
| 123 | ); |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | echo $field_type->input( |
||
| 128 | array( |
||
| 129 | 'type' => 'text', |
||
| 130 | 'name' => $field_name, |
||
| 131 | 'id' => $field_name, |
||
| 132 | 'class' => 'cmb-post-search-ajax', |
||
| 133 | 'value' => $field_value, |
||
| 134 | 'desc' => false, |
||
| 135 | 'data-limit' => $field->args( 'limit' ) ? $field->args( 'limit' ) : '1', |
||
| 136 | 'data-sortable' => $field->args( 'sortable' ) ? $field->args( 'sortable' ) : '0', |
||
| 137 | 'data-object' => $field->args( 'object_type' ) ? $field->args( 'object_type' ) : 'post', |
||
| 138 | 'data-queryargs'=> $field->args( 'query_args' ) ? htmlspecialchars( json_encode( $field->args( 'query_args' ) ), ENT_QUOTES, 'UTF-8' ) : '' |
||
| 139 | ) |
||
| 140 | ); |
||
| 141 | |||
| 142 | echo '<img src="' . admin_url( 'images/spinner.gif' ) . '" class="cmb-post-search-ajax-spinner" />'; |
||
| 143 | |||
| 144 | $field_type->_desc( true, true ); |
||
| 145 | |||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Optionally save the latitude/longitude values into two custom fields |
||
| 150 | */ |
||
| 151 | public function sanitize( $override_value, $value, $object_id, $field_args ) { |
||
| 152 | $fid = ''; |
||
| 153 | if ( isset( $field_args['id'] ) ) { |
||
| 154 | $fid = $field_args['id']; |
||
| 155 | } |
||
| 156 | |||
| 157 | // IF the field is in a repeatable group, then get the info from the post data. |
||
| 158 | if ( isset( $field_args['render_row_cb'][0]->group ) && ! empty( $field_args['render_row_cb'][0]->group ) ) { |
||
| 159 | $new_index = ''; |
||
| 160 | |||
| 161 | $data_to_save = $field_args['render_row_cb'][0]->group->args['render_row_cb'][0]->data_to_save; |
||
| 162 | $oid = $field_args['_name']; |
||
| 163 | $iid = $field_args['_id']; |
||
| 164 | $oid = explode( '[', $oid ); |
||
| 165 | if ( is_array( $oid ) ) { |
||
|
|
|||
| 166 | $oid = $oid[0]; |
||
| 167 | } |
||
| 168 | |||
| 169 | if ( isset( $data_to_save[ $oid ] ) && ! empty( $data_to_save[ $oid ] ) ) { |
||
| 170 | foreach( $data_to_save[ $oid ] as $index => $svalues ) { |
||
| 171 | if ( isset( $svalues[ $iid ] ) && $value === $svalues[ $iid ] ) { |
||
| 172 | $new_index = $index; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | if ( '' !== $new_index ) { |
||
| 178 | $new_index = $oid . '_' . $new_index . '_' . $iid . '_store'; |
||
| 179 | |||
| 180 | if ( ! empty( $data_to_save[ $new_index ] ) ) { |
||
| 181 | $value = $data_to_save[ $new_index ]; |
||
| 182 | } |
||
| 183 | } else { |
||
| 184 | $value = false; |
||
| 185 | } |
||
| 186 | } else if ( ! empty( $field_args['render_row_cb'][0]->data_to_save[ $fid . '_results' ] ) ) { |
||
| 187 | $value = $field_args['render_row_cb'][0]->data_to_save[ $fid . '_results' ]; |
||
| 188 | } else { |
||
| 189 | $value = false; |
||
| 190 | } |
||
| 191 | |||
| 192 | return $value; |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Defines the url which is used to load local resources. Based on, and uses, |
||
| 197 | * the CMB2_Utils class from the CMB2 library. |
||
| 198 | */ |
||
| 199 | public static function url( $path = '' ) { |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Enqueue scripts and styles |
||
| 224 | */ |
||
| 225 | public function setup_admin_scripts() { |
||
| 235 | |||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Ajax request : get results |
||
| 240 | */ |
||
| 241 | public function cmb_post_search_ajax_get_results() { |
||
| 281 | } |
||
| 282 | } |
||
| 283 | } |
||
| 285 |