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:
1 | <?php |
||
17 | class Yikes_Inc_Easy_Mailchimp_Extender_Public { |
||
18 | /** |
||
19 | * The ID of this plugin. |
||
20 | * |
||
21 | * @since 6.0.0 |
||
22 | * @access private |
||
23 | * @var string $yikes_inc_easy_mailchimp_extender The ID of this plugin. |
||
24 | */ |
||
25 | private $yikes_inc_easy_mailchimp_extender; |
||
26 | /** |
||
27 | * The version of this plugin. |
||
28 | * |
||
29 | * @since 6.0.0 |
||
30 | * @access private |
||
31 | * @var string $version The current version of this plugin. |
||
32 | */ |
||
33 | private $version; |
||
34 | /** |
||
35 | * Initialize the class and set its properties. |
||
36 | * |
||
37 | * @since 6.0.0 |
||
38 | * @param string $yikes_inc_easy_mailchimp_extender The name of the plugin. |
||
39 | * @param string $version The version of this plugin. |
||
40 | */ |
||
41 | public function __construct( $yikes_inc_easy_mailchimp_extender, $version ) { |
||
42 | $this->yikes_inc_easy_mailchimp_extender = $yikes_inc_easy_mailchimp_extender; |
||
43 | $this->version = $version; |
||
44 | /* |
||
45 | * Include our helper functions |
||
46 | * @since 6.0.3.4 |
||
47 | */ |
||
48 | include_once( YIKES_MC_PATH . 'public/helpers.php' ); |
||
49 | |||
50 | // Include our Shortcode & Processing functions (public folder) |
||
51 | require_once( YIKES_MC_PATH . 'public/partials/shortcodes/unsubscribe/process-unsubscribe.php' ); |
||
52 | include_once( YIKES_MC_PATH . 'public/partials/shortcodes/unsubscribe/shortcode-unsubscribe.php' ); |
||
53 | include_once( YIKES_MC_PATH . 'public/partials/shortcodes/process_form_shortcode.php' ); |
||
54 | include_once( YIKES_MC_PATH . 'public/partials/shortcodes/yikes-mailchimp-subscriber-count.php' ); |
||
55 | |||
56 | // include our ajax processing class |
||
57 | new YIKES_Inc_Easy_Mailchimp_Public_Ajax(); |
||
58 | |||
59 | // Include our error logging class |
||
60 | add_action( 'init' , array( $this , 'load_error_logging_class' ) , 1 ); |
||
61 | // load our checkbox classes |
||
62 | add_action( 'init' , array( $this , 'load_checkbox_integration_classes' ) , 1 ); |
||
63 | // custom front end filter |
||
64 | add_action( 'init', array( $this, 'yikes_custom_frontend_content_filter' ) ); |
||
65 | // Process non-ajax forms in the header |
||
66 | add_action( 'init', array( $this, 'yikes_process_non_ajax_forms' ) ); |
||
67 | // Filter the user already subscribed response with a custom message |
||
68 | add_filter( 'yikes-easy-mailchimp-update-existing-subscriber-text', array( $this, 'yikes_custom_already_subscribed_response' ), 10, 3 ); |
||
69 | // Filter the user already subscribed response with a custom message |
||
70 | // add_filter( 'yikes-easy-mailchimp-user-already-subscribed-text', array( $this, 'yikes_custom_already_subscribed_text' ), 10, 3 ); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Create our own custom the_content(); filter to prevent plugins and such from hooking in where not wanted |
||
75 | * |
||
76 | * @since 6.0.3 |
||
77 | */ |
||
78 | public function yikes_custom_frontend_content_filter() { |
||
79 | add_filter( 'yikes-mailchimp-frontend-content', 'wptexturize' ); |
||
80 | add_filter( 'yikes-mailchimp-frontend-content', 'convert_smilies' ); |
||
81 | add_filter( 'yikes-mailchimp-frontend-content', 'convert_chars' ); |
||
82 | add_filter( 'yikes-mailchimp-frontend-content', 'wpautop' ); |
||
83 | add_filter( 'yikes-mailchimp-frontend-content', 'shortcode_unautop' ); |
||
84 | add_filter( 'yikes-mailchimp-frontend-content', 'prepend_attachment' ); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Load our checkbox integrations |
||
89 | * |
||
90 | * Based on what the user has specified on the options page, lets |
||
91 | * load our checkbox classes |
||
92 | * |
||
93 | * @since 6.0.0 |
||
94 | **/ |
||
95 | public function load_checkbox_integration_classes() { |
||
96 | // store our options |
||
97 | $integrations = get_option( 'optin-checkbox-init' , array() ); |
||
98 | if( ! empty( $integrations ) && is_array( $integrations ) ) { |
||
99 | // load our mail integrations class |
||
100 | require_once YIKES_MC_PATH . 'public/classes/checkbox-integrations.php'; |
||
101 | // loop over selected classes and load them up! |
||
102 | foreach( $integrations as $integration => $value ) { |
||
103 | if( isset( $value['value'] ) && $value['value'] == 'on' ) { |
||
104 | // load our class extensions |
||
105 | require_once YIKES_MC_PATH . 'public/classes/checkbox-integrations/class.'.$integration.'-checkbox.php'; |
||
106 | } |
||
107 | } |
||
108 | } |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Error logging class |
||
113 | * |
||
114 | * This is our main error logging class file, used to log errors to the error log. |
||
115 | * |
||
116 | * @since 6.0.0 |
||
117 | */ |
||
118 | View Code Duplication | public function load_error_logging_class() { |
|
|
|||
119 | if( get_option( 'yikes-mailchimp-debug-status' , '' ) == '1' ) { |
||
120 | // if error logging is enabled we should include our error logging class |
||
121 | require_once YIKES_MC_PATH . 'includes/error_log/class-yikes-inc-easy-mailchimp-error-logging.php'; |
||
122 | $error_logging = new Yikes_Inc_Easy_Mailchimp_Error_Logging; |
||
123 | } |
||
124 | } |
||
125 | |||
126 | /* |
||
127 | * On form submission, lets include our form processing file |
||
128 | * - processes non-ajax forms |
||
129 | * @since 6.0.3.4 |
||
130 | */ |
||
131 | public function yikes_process_non_ajax_forms( $form_submitted ) { |
||
132 | global $wpdb,$post; |
||
133 | $form_id = ( ! empty( $_POST['yikes-mailchimp-submitted-form'] ) ) ? (int) $_POST['yikes-mailchimp-submitted-form'] : false; // store form id |
||
134 | if( $form_id ) { |
||
135 | $form_settings = self::yikes_retrieve_form_settings( $form_id ); |
||
136 | if( isset( $_POST ) && !empty( $_POST ) && isset( $form_id ) && $form_settings['submission_settings']['ajax'] == 0 ) { |
||
137 | if( $_POST['yikes-mailchimp-submitted-form'] == $form_id ) { // ensure we only process the form that was submitted |
||
138 | |||
139 | // Lets include our form processing file |
||
140 | include_once( YIKES_MC_PATH . 'public/partials/shortcodes/process/process_form_submission.php' ); |
||
141 | if( $form_settings['submission_settings']['redirect_on_submission'] == '1' ) { |
||
142 | if( $form_submitted == 1 ) { |
||
143 | // decode our settings |
||
144 | $redirect_page = ( 'custom_url' != $form_settings['submission_settings']['redirect_page'] ) ? get_permalink( (int) $form_settings['submission_settings']['redirect_page'] ) : $form_settings['submission_settings']['custom_redirect_url']; |
||
145 | wp_redirect( apply_filters( 'yikes-mailchimp-redirect-url', esc_url( $redirect_page ), $form_id, $post ) ); |
||
146 | exit; |
||
147 | } |
||
148 | } |
||
149 | } |
||
150 | } |
||
151 | } |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Get the given form data. |
||
156 | * |
||
157 | * This is a wrapper for the form interface get_form() method. It is recommended to use |
||
158 | * that method directly instead of this function. |
||
159 | * |
||
160 | * @author Jeremy Pry |
||
161 | * @deprecated |
||
162 | * |
||
163 | * @since 6.2.0 Use the new form interface. |
||
164 | * @since 6.0.3.4 |
||
165 | * |
||
166 | * @param int $form_id The form ID to retrieve. |
||
167 | * |
||
168 | * @return array |
||
169 | */ |
||
170 | public static function yikes_retrieve_form_settings( $form_id ) { |
||
171 | // if no form id, abort |
||
172 | if( ! $form_id ) { |
||
173 | return array(); |
||
174 | } |
||
175 | |||
176 | $interface = yikes_easy_mailchimp_extender_get_form_interface(); |
||
177 | |||
178 | return $interface->get_form( $form_id ); |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Filter the unsubscribed response, allowing users to customize it |
||
183 | * Users can wrap text to create a custom update link, by wrapping text in [link]xxx[/link]. |
||
184 | * @param string $response_text The default response. |
||
185 | * @param int $form_id The form ID to retreive options from. |
||
186 | * @param string $link The update profile link, when clicked this sends the user an email. |
||
187 | * @return string The final output for the update existing subscriber. |
||
188 | */ |
||
189 | public function yikes_custom_already_subscribed_response( $response_text, $form_id, $link ) { |
||
215 | |||
216 | /** |
||
217 | * Alter the beginning of the user already subscribed string |
||
218 | * Allowing users to use the email in the response, by adding [email] to the text |
||
219 | * |
||
220 | * @since 6.1 |
||
221 | */ |
||
222 | public function yikes_custom_already_subscribed_text( $response_text, $form_id, $email ) { |
||
242 | } |
||
243 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.