Completed
Push — staging ( 99a729...9693fc )
by
unknown
19:21
created

Yikes_Inc_Easy_Mailchimp_Extender_Public   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 226
Duplicated Lines 3.1 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 7
loc 226
ccs 0
cts 83
cp 0
rs 9.6
c 0
b 0
f 0
wmc 35
lcom 1
cbo 2

8 Methods

Rating   Name   Duplication   Size   Complexity  
B yikes_custom_already_subscribed_response() 0 26 7
A yikes_custom_already_subscribed_text() 0 20 5
A __construct() 0 31 1
A yikes_custom_frontend_content_filter() 0 8 1
A load_checkbox_integration_classes() 0 15 6
A load_error_logging_class() 7 7 2
B yikes_process_non_ajax_forms() 0 22 11
A yikes_retrieve_form_settings() 0 10 2

How to fix   Duplicated Code   

Duplicated Code

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
2
/**
3
 * The public-facing functionality of the plugin.
4
 *
5
 * @link       https://www.yikesplugins.com/
6
 * @since      6.0.0
7
 *
8
 * @package    Yikes_Inc_Easy_Mailchimp_Extender
9
 * @subpackage Yikes_Inc_Easy_Mailchimp_Extender/public
10
 *
11
 * The public-facing functionality of the plugin.
12
 *
13
 * @package    Yikes_Inc_Easy_Mailchimp_Extender
14
 * @subpackage Yikes_Inc_Easy_Mailchimp_Extender/public
15
 * @author     YIKES Inc. <[email protected]>
16
 */
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() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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;
0 ignored issues
show
Unused Code introduced by
$error_logging is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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 ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $form_id of type integer|false is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
135
			$form_settings = self::yikes_retrieve_form_settings( $form_id );
0 ignored issues
show
Deprecated Code introduced by
The method Yikes_Inc_Easy_Mailchimp...etrieve_form_settings() has been deprecated.

This method has been deprecated.

Loading history...
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 ) {
190
		// if no form id found, abort
191
		if ( ! $form_id ) {
192
			return;
193
		}
194
		// retreive our form settings
195
		$form_settings = $form_settings = Yikes_Inc_Easy_Mailchimp_Extender_Public::yikes_retrieve_form_settings( $form_id );
0 ignored issues
show
Deprecated Code introduced by
The method Yikes_Inc_Easy_Mailchimp...etrieve_form_settings() has been deprecated.

This method has been deprecated.

Loading history...
196
		// if none, abort
197
		if ( ! $form_settings ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $form_settings of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
198
			return;
199
		}
200
		// trim trailing period
201
		if ( isset( $form_settings['error_messages']['update-link'] ) && ! empty( $form_settings['error_messages']['update-link'] ) ) {
202
			$response_text = $form_settings['error_messages']['update-link'];
203
			// extract the link text
204
			preg_match( '/\[link].*?\[\/link\]/', $response_text, $link_text );
205
			if ( $link_text && ! empty( $link_text ) ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $link_text of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
206
				// Extract the custom link text ([link]*[/link])
207
				$custom_link_text = str_replace( '[/link]', '', str_replace( '[link]', '', str_replace( 'click to send yourself an update link', $link_text[0], $link ) ) );
208
				// Replace the link text, with our custom link text
209
				$response_text = str_replace( $link_text, $custom_link_text, $response_text );
210
			}
211
		}
212
		// Return our new string
213
		return $response_text;
214
	}
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 ) {
223
		// if no form id found, abort
224
		if ( ! $form_id ) {
225
			return;
226
		}
227
228
		// retreive our form settings
229
		$form_settings = $form_settings = self::yikes_retrieve_form_settings( $form_id );
0 ignored issues
show
Deprecated Code introduced by
The method Yikes_Inc_Easy_Mailchimp...etrieve_form_settings() has been deprecated.

This method has been deprecated.

Loading history...
230
		// if none, abort
231
		if ( ! $form_settings ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $form_settings of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
232
			return;
233
		}
234
235
		// trim trailing period
236
		if ( isset( $form_settings['error_messages']['already-subscribed'] ) && ! empty( $form_settings['error_messages']['already-subscribed'] ) ) {
237
			$response_text = str_replace( '[email]', $email, $form_settings['error_messages']['already-subscribed'] );
238
		}
239
		// Return our new string
240
		return $response_text;
241
	}
242
}
243