| Total Complexity | 62 |
| Total Lines | 769 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like WP_Super_Duper_Shortcode 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 WP_Super_Duper_Shortcode, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class WP_Super_Duper_Shortcode { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var WP_Super_Duper |
||
| 22 | */ |
||
| 23 | protected $sd; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Class constructor. |
||
| 27 | * |
||
| 28 | * @param WP_Super_Duper $super_duper |
||
| 29 | */ |
||
| 30 | public function __construct( $super_duper ) { |
||
| 31 | |||
| 32 | $this->sd = $super_duper; |
||
| 33 | |||
| 34 | // Registers the shortcode. |
||
| 35 | add_shortcode( $this->sd->base_id, array( $this, 'shortcode_output' ) ); |
||
| 36 | |||
| 37 | // This makes the insert button work for cornerstone. |
||
| 38 | add_action( 'wp_print_footer_scripts', array( $this, 'maybe_cornerstone_builder' ) ); |
||
| 39 | |||
| 40 | // Fusion Builder (avada) support |
||
| 41 | if ( function_exists( 'fusion_builder_map' ) ) { |
||
| 42 | add_action( 'init', array( $this, 'register_fusion_element' ) ); |
||
| 43 | } |
||
| 44 | |||
| 45 | // Add shortcode insert button once |
||
| 46 | add_action( 'media_buttons', array( $this, 'shortcode_insert_button' ) ); |
||
| 47 | add_action( 'wp_ajax_super_duper_get_widget_settings', array( $this, 'get_widget_settings' ) ); |
||
| 48 | |||
| 49 | // generatepress theme sections compatibility |
||
| 50 | if ( function_exists( 'generate_sections_sections_metabox' ) ) { |
||
| 51 | add_action( 'generate_sections_metabox', array( $this, 'shortcode_insert_button_script' ) ); |
||
| 52 | } |
||
| 53 | |||
| 54 | if ( $this->sd->is_preview() ) { |
||
| 55 | add_action( 'wp_footer', array( $this, 'shortcode_insert_button_script' ) ); |
||
| 56 | // this makes the insert button work for elementor |
||
| 57 | add_action( 'elementor/editor/after_enqueue_scripts', array( |
||
| 58 | $this, |
||
| 59 | 'shortcode_insert_button_script' |
||
| 60 | ) ); // for elementor |
||
| 61 | } |
||
| 62 | |||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Insert shortcode builder button to classic editor (not inside Gutenberg, not needed). |
||
| 67 | * |
||
| 68 | * @since 1.0.0 |
||
| 69 | * |
||
| 70 | * @param string $editor_id Optional. Shortcode editor id. Default null. |
||
| 71 | * @param string $insert_shortcode_function Optional. Insert shortcode function. Default null. |
||
| 72 | */ |
||
| 73 | public static function shortcode_insert_button( $editor_id = '', $insert_shortcode_function = '' ) { |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Output the shortcode. |
||
| 106 | * |
||
| 107 | * @param array $args |
||
| 108 | * @param string $content |
||
| 109 | * |
||
| 110 | * @return string |
||
| 111 | */ |
||
| 112 | public function shortcode_output( $args = array(), $content = '' ) { |
||
| 113 | $args = $this->sd->argument_values( $args ); |
||
| 114 | |||
| 115 | // Clean booleans. |
||
| 116 | $args = $this->sd->string_to_bool( $args ); |
||
| 117 | |||
| 118 | // if we have a enclosed shortcode we add it to the special `html` argument |
||
| 119 | if ( ! empty( $content ) ) { |
||
| 120 | $args['html'] = $content; |
||
| 121 | } |
||
| 122 | |||
| 123 | $class = isset( $this->sd->options['widget_ops']['classname'] ) ? esc_attr( $this->sd->options['widget_ops']['classname'] ) : ''; |
||
| 124 | $class .= " sdel-" . $this->sd->get_instance_hash(); |
||
| 125 | |||
| 126 | $class = apply_filters( 'wp_super_duper_div_classname', $class, $args, $this->sd, $this ); |
||
| 127 | $class = apply_filters( 'wp_super_duper_div_classname_' . $this->sd->base_id, $class, $args, $this->sd, $this ); |
||
| 128 | |||
| 129 | $attrs = apply_filters( 'wp_super_duper_div_attrs', '', $args, $this->sd, $this ); |
||
| 130 | $attrs = apply_filters( 'wp_super_duper_div_attrs_' . $this->sd->base_id, $attrs, $args, $this->sd, $this ); |
||
| 131 | |||
| 132 | $shortcode_args = array(); |
||
| 133 | $output = ''; |
||
| 134 | $no_wrap = ! empty( $this->sd->options['no_wrap'] ) || ! empty( $args['no_wrap'] ); |
||
| 135 | |||
| 136 | $main_content = $this->sd->output( $args, $shortcode_args, $content ); |
||
|
|
|||
| 137 | |||
| 138 | if ( $main_content && ! $no_wrap ) { |
||
| 139 | // wrap the shortcode in a div with the same class as the widget |
||
| 140 | $output .= '<div class="' . esc_attr( $class ) . '" ' . $attrs . '>'; |
||
| 141 | if ( ! empty( $args['title'] ) ) { |
||
| 142 | // if its a shortcode and there is a title try to grab the title wrappers |
||
| 143 | $shortcode_args = array( 'before_title' => '', 'after_title' => '' ); |
||
| 144 | if ( empty( $instance ) ) { |
||
| 145 | global $wp_registered_sidebars; |
||
| 146 | if ( ! empty( $wp_registered_sidebars ) ) { |
||
| 147 | foreach ( $wp_registered_sidebars as $sidebar ) { |
||
| 148 | if ( ! empty( $sidebar['before_title'] ) ) { |
||
| 149 | $shortcode_args['before_title'] = $sidebar['before_title']; |
||
| 150 | $shortcode_args['after_title'] = $sidebar['after_title']; |
||
| 151 | break; |
||
| 152 | } |
||
| 153 | } |
||
| 154 | } |
||
| 155 | } |
||
| 156 | $output .= $this->sd->output_title( $shortcode_args, $args ); |
||
| 157 | } |
||
| 158 | $output .= $main_content; |
||
| 159 | $output .= '</div>'; |
||
| 160 | } elseif ( $main_content && $no_wrap ) { |
||
| 161 | $output .= $main_content; |
||
| 162 | } |
||
| 163 | |||
| 164 | // if preview, show a placeholder if empty |
||
| 165 | if ( $this->sd->is_preview() && $output == '' ) { |
||
| 166 | $output = $this->sd->preview_placeholder_text( "{{" . $this->sd->base_id . "}}" ); |
||
| 167 | } |
||
| 168 | |||
| 169 | return apply_filters( 'wp_super_duper_widget_output', $output, $args, $shortcode_args, $this ); |
||
| 170 | } |
||
| 171 | |||
| 172 | public function register_fusion_element() { |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | protected function get_fusion_params() { |
||
| 195 | $params = array(); |
||
| 196 | $arguments = $this->sd->get_arguments(); |
||
| 197 | |||
| 198 | if ( ! empty( $arguments ) ) { |
||
| 199 | foreach ( $arguments as $key => $val ) { |
||
| 200 | $param = array(); |
||
| 201 | // type |
||
| 202 | $param['type'] = str_replace( |
||
| 203 | array( |
||
| 204 | "text", |
||
| 205 | "number", |
||
| 206 | "email", |
||
| 207 | "color", |
||
| 208 | "checkbox" |
||
| 209 | ), |
||
| 210 | array( |
||
| 211 | "textfield", |
||
| 212 | "textfield", |
||
| 213 | "textfield", |
||
| 214 | "colorpicker", |
||
| 215 | "select", |
||
| 216 | ), |
||
| 217 | $val['type'] ); |
||
| 218 | |||
| 219 | // multiselect |
||
| 220 | if ( $val['type'] == 'multiselect' || ( ( $param['type'] == 'select' || $val['type'] == 'select' ) && ! empty( $val['multiple'] ) ) ) { |
||
| 221 | $param['type'] = 'multiple_select'; |
||
| 222 | $param['multiple'] = true; |
||
| 223 | } |
||
| 224 | |||
| 225 | // heading |
||
| 226 | $param['heading'] = $val['title']; |
||
| 227 | |||
| 228 | // description |
||
| 229 | $param['description'] = isset( $val['desc'] ) ? $val['desc'] : ''; |
||
| 230 | |||
| 231 | // param_name |
||
| 232 | $param['param_name'] = $key; |
||
| 233 | |||
| 234 | // Default |
||
| 235 | $param['default'] = isset( $val['default'] ) ? $val['default'] : ''; |
||
| 236 | |||
| 237 | // Group |
||
| 238 | if ( isset( $val['group'] ) ) { |
||
| 239 | $param['group'] = $val['group']; |
||
| 240 | } |
||
| 241 | |||
| 242 | // value |
||
| 243 | if ( $val['type'] == 'checkbox' ) { |
||
| 244 | if ( isset( $val['default'] ) && $val['default'] == '0' ) { |
||
| 245 | unset( $param['default'] ); |
||
| 246 | } |
||
| 247 | $param['value'] = array( '' => __( "No" ), '1' => __( "Yes" ) ); |
||
| 248 | } elseif ( $param['type'] == 'select' || $param['type'] == 'multiple_select' ) { |
||
| 249 | $param['value'] = isset( $val['options'] ) ? $val['options'] : array(); |
||
| 250 | } else { |
||
| 251 | $param['value'] = isset( $val['default'] ) ? $val['default'] : ''; |
||
| 252 | } |
||
| 253 | |||
| 254 | // setup the param |
||
| 255 | $params[] = $param; |
||
| 256 | |||
| 257 | } |
||
| 258 | } |
||
| 259 | |||
| 260 | return $params; |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Maybe insert the shortcode inserter button in the footer if we are in the cornerstone builder |
||
| 265 | */ |
||
| 266 | public function maybe_cornerstone_builder() { |
||
| 267 | if ( did_action( 'cornerstone_before_boot_app' ) ) { |
||
| 268 | self::shortcode_insert_button_script(); |
||
| 269 | } |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Output the JS and CSS for the shortcode insert button. |
||
| 274 | * |
||
| 275 | * @since 1.0.6 |
||
| 276 | * |
||
| 277 | * @param string $editor_id |
||
| 278 | * @param string $insert_shortcode_function |
||
| 279 | */ |
||
| 280 | public static function shortcode_insert_button_script( $editor_id = '', $insert_shortcode_function = '' ) { |
||
| 730 | } |
||
| 731 | } |
||
| 732 | </script> |
||
| 733 | <?php |
||
| 734 | } |
||
| 735 | |||
| 736 | /** |
||
| 737 | * Gets the shortcode insert button html. |
||
| 738 | * |
||
| 739 | * @param string $id |
||
| 740 | * @param string $search_for_id |
||
| 741 | * |
||
| 742 | * @return mixed |
||
| 743 | */ |
||
| 744 | public static function shortcode_button( $id = '', $search_for_id = '' ) { |
||
| 745 | ob_start(); |
||
| 746 | ?> |
||
| 747 | <span class="sd-lable-shortcode-inserter"> |
||
| 748 | <a onclick="sd_ajax_get_picker(<?php echo $id; |
||
| 749 | if ( $search_for_id ) { |
||
| 750 | echo "," . $search_for_id; |
||
| 751 | } ?>);" href="#TB_inline?width=100%&height=550&inlineId=super-duper-content-ajaxed" |
||
| 752 | class="thickbox button super-duper-content-open" title="Add Shortcode"> |
||
| 753 | <span style="vertical-align: middle;line-height: 18px;font-size: 20px;" |
||
| 754 | class="dashicons dashicons-screenoptions"></span> |
||
| 755 | </a> |
||
| 756 | <div id="super-duper-content-ajaxed" style="display:none;"> |
||
| 757 | <span>Loading</span> |
||
| 758 | </div> |
||
| 759 | </span> |
||
| 760 | |||
| 761 | <?php |
||
| 762 | $html = ob_get_clean(); |
||
| 763 | |||
| 764 | // remove line breaks so we can use it in js |
||
| 765 | return preg_replace( "/\r|\n/", "", trim( $html ) ); |
||
| 766 | } |
||
| 767 | |||
| 768 | /** |
||
| 769 | * Get widget settings. |
||
| 770 | * |
||
| 771 | * @since 1.0.0 |
||
| 772 | */ |
||
| 773 | public function get_widget_settings() { |
||
| 787 | |||
| 788 | } |
||
| 789 | |||
| 793 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.