Completed
Push — master ( 45ba98...c3b0cd )
by Stephanie
04:13
created

FrmPointers::settings_pointer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 12
rs 9.4286
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
3
/**
4
 * This class handles the pointers used in the introduction tour.
5
 */
6
class FrmPointers {
7
8
	/**
9
	 * @var object Instance of this class
10
	 */
11
	public static $instance;
12
13
	/**
14
	 * @var array Holds the buttons to be put out
15
	 */
16
	private $button_array;
17
18
	/**
19
	 * @var array Holds the admin pages we have pointers for and the callback that generates the pointers content
20
	 */
21
	private $admin_pages = array(
22
		'' => 'forms_pointer',
23
		'entries'   => 'entries_pointer',
24
		'styles'    => 'styles_pointer',
25
		'import'    => 'import_pointer',
26
		'settings'  => 'settings_pointer',
27
		'addons'    => 'addons_pointer',
28
	);
29
30
	/**
31
	 * Class constructor.
32
	 */
33
	private function __construct() {
34
		if ( current_user_can( 'manage_options' ) ) {
35
36
			if ( ! get_user_meta( get_current_user_id(), 'frm_ignore_tour' ) ) {
1 ignored issue
show
introduced by
get_user_meta() usage is highly discouraged, check VIP documentation on "Working with wp_users"
Loading history...
37
				wp_enqueue_style( 'wp-pointer' );
38
				wp_enqueue_script( 'jquery-ui' );
39
				wp_enqueue_script( 'wp-pointer' );
40
				add_action( 'admin_print_footer_scripts', array( $this, 'intro_tour' ) );
41
			}
42
		}
43
	}
44
45
	/**
46
	 * Get the singleton instance of this class
47
	 *
48
	 * @return object
49
	 */
50
	public static function get_instance() {
51
		if ( ! ( self::$instance instanceof self ) ) {
52
			self::$instance = new self();
53
		}
54
55
		return self::$instance;
56
	}
57
58
	/**
59
	 * Load the introduction tour
60
	 */
61
	public function intro_tour() {
62
		global $pagenow;
63
64
		$page = preg_replace( '/^(formidable[-]?)/', '', filter_input( INPUT_GET, 'page' ) );
65
66
		if ( 'admin.php' === $pagenow && array_key_exists( $page, $this->admin_pages ) ) {
67
			$this->do_page_pointer( $page );
0 ignored issues
show
Bug introduced by
It seems like $page defined by preg_replace('/^(formida...put(INPUT_GET, 'page')) on line 64 can also be of type array<integer,string>; however, FrmPointers::do_page_pointer() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
68
		} else {
69
			$this->start_tour_pointer();
70
		}
71
	}
72
73
	/**
74
	 * Prints the pointer script
75
	 *
76
	 * @param string $selector The CSS selector the pointer is attached to.
77
	 * @param array  $options  The options for the pointer.
78
	 */
79
	public function print_scripts( $selector, $options ) {
80
		// Button1 is the close button, which always exists.
81
		$default_button = array(
82
			'text'     => false,
83
			'function' => '',
84
		);
85
		$button_array_defaults = array(
86
			'button2' => $default_button,
87
			'button3' => $default_button,
88
		);
89
		$this->button_array = wp_parse_args( $this->button_array, $button_array_defaults );
90
		?>
91
		<script type="text/javascript">
92
			//<![CDATA[
93
			(function ($) {
94
				// Don't show the tour on screens with an effective width smaller than 1024px or an effective height smaller than 768px.
95
				if (jQuery(window).width() < 1024 || jQuery(window).availWidth < 1024) {
96
					return;
97
				}
98
99
				var frm_pointer_options = <?php echo json_encode( $options ); ?>, setup;
100
101
				frm_pointer_options = $.extend(frm_pointer_options, {
102
					buttons: function (event, t) {
103
						var button = jQuery('<a href="<?php echo $this->get_ignore_url(); ?>" id="pointer-close" style="margin:0 5px;" class="button-secondary">' + '<?php _e( 'Close', 'formidable' ) ?>' + '</a>');
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
104
						button.bind('click.pointer', function () {
105
							t.element.pointer('close');
106
						});
107
						return button;
108
					},
109
					close: function () {
110
					}
111
				});
112
113
				setup = function () {
114
					$('<?php echo $selector; ?>').pointer(frm_pointer_options).pointer('open');
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$selector'
Loading history...
115
					var lastOpenedPointer = jQuery( '.wp-pointer').slice( -1 );
116
					<?php
117
					$this->button2();
118
					$this->button3();
119
					?>
120
				};
121
122
				if (frm_pointer_options.position && frm_pointer_options.position.defer_loading)
123
					$(window).bind('load.wp-pointers', setup);
124
				else
125
					$(document).ready(setup);
126
			})(jQuery);
127
			//]]>
128
		</script>
129
	<?php
130
	}
131
132
	/**
133
	 * Render button 2, if needed
134
	 */
135 View Code Duplication
	private function button2() {
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...
136
		if ( $this->button_array['button2']['text'] ) {
137
			?>
138
			lastOpenedPointer.find( '#pointer-close' ).after('<a id="pointer-primary" class="button-primary">' +
139
				'<?php echo $this->button_array['button2']['text']; ?>' + '</a>');
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
140
			lastOpenedPointer.find('#pointer-primary').click(function () {
141
			<?php echo $this->button_array['button2']['function']; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
142
			});
143
		<?php
144
		}
145
	}
146
147
	/**
148
	 * Render button 3, if needed. This is the previous button in most cases
149
	 */
150 View Code Duplication
	private function button3() {
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...
151
		if ( $this->button_array['button3']['text'] ) {
152
			?>
153
			lastOpenedPointer.find('#pointer-primary').after('<a id="pointer-ternary" style="float: left;" class="button-secondary">' +
154
				'<?php echo $this->button_array['button3']['text']; ?>' + '</a>');
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
155
			lastOpenedPointer.find('#pointer-ternary').click(function () {
156
			<?php echo $this->button_array['button3']['function']; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$this'
Loading history...
157
			});
158
		<?php }
159
	}
160
161
	/**
162
	 * Show a pointer that starts the tour
163
	 */
164
	private function start_tour_pointer() {
165
		$selector = 'li.toplevel_page_formidable';
166
		$content  = '<h3>' . __( 'Congratulations!', 'formidable' ) . '</h3>'
167
		            .'<p>' . sprintf( __( 'You&#8217;ve just installed new forms! Click &#8220;Start Tour&#8221; to view a quick introduction of this plugin&#8217;s core functionality.' ), 'formidable' ) . '</p>';
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'sprintf'
Loading history...
168
		$opt_arr  = array(
169
			'content'  => $content,
170
			'position' => array( 'edge' => 'top', 'align' => 'center' ),
171
		);
172
173
		$this->button_array['button2']['text']     = __( 'Start Tour', 'formidable' );
174
		$this->button_array['button2']['function'] = sprintf( 'document.location="%s";', admin_url( 'admin.php?page=formidable' ) );
175
176
		$this->print_scripts( $selector, $opt_arr );
177
	}
178
179
	/**
180
	 * Shows a pointer on the proper pages
181
	 *
182
	 * @param string $page Admin page key.
183
	 */
184
	private function do_page_pointer( $page ) {
185
		$selector = 'h2';
186
187
		$pointer = call_user_func( array( $this, $this->admin_pages[ $page ] ) );
188
189
		$opt_arr = array(
190
			'content'      => $pointer['content'],
191
			'position'     => array(
192
				'edge'  => 'top',
193
				'align' => ( is_rtl() ) ? 'left' : 'right',
194
			),
195
			'pointerWidth' => 450,
196
		);
197 View Code Duplication
		if ( isset( $pointer['next_page'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
198
			if ( ! empty( $pointer['next_page'] ) ) {
199
				$pointer['next_page'] = '-' . $pointer['next_page'];
200
			}
201
			$this->button_array['button2'] = array(
202
				'text'     => __( 'Next', 'formidable' ),
203
				'function' => 'window.location="' . esc_url_raw( admin_url( 'admin.php?page=formidable' . $pointer['next_page'] ) ) . '";',
204
			);
205
		}
206 View Code Duplication
		if ( isset( $pointer['prev_page'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
207
			if ( ! empty( $pointer['prev_page'] ) ) {
208
				$pointer['prev_page'] = '-' . $pointer['prev_page'];
209
			}
210
			$this->button_array['button3'] = array(
211
				'text'     => __( 'Previous', 'formidable' ),
212
				'function' => 'window.location="' . esc_url_raw( admin_url( 'admin.php?page=formidable-' . $pointer['prev_page'] ) ) . '";',
213
			);
214
		}
215
		$this->print_scripts( $selector, $opt_arr );
216
	}
217
218
	/**
219
	 * Returns the content of the Forms listing page pointer
220
	 *
221
	 * @return array
222
	 */
223
	private function forms_pointer() {
224
		global $current_user;
225
226
		return array(
227
			'content'   => '<h3>' . __( 'Forms', 'formidable' ) . '</h3>'
228
			               . '<p>' . __( 'All your forms will be listed on this page. Create your first form by clicking on the "Add New" button.', 'formidable' ) . '</p>'
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw '__'
Loading history...
229
			               . '<p><strong>' . __( 'Subscribe to our Newsletter', 'formidable' ) . '</strong><br/>'
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw '__'
Loading history...
230
			               . sprintf( __( 'If you would like to hear about new features and updates for %1$s, subscribe to our newsletter:', 'formidable' ), 'Formidable' ) . '</p>'
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'sprintf'
Loading history...
231
			               . '<form target="_blank" action="//formidablepro.us1.list-manage.com/subscribe/post?u=a4a913790ffb892daacc6f271&amp;id=7e7df15967" method="post" selector="newsletter-form" accept-charset="' . esc_attr( get_bloginfo( 'charset' ) ) . '">'
232
			               . '<p>'
233
			               . '<input style="margin: 5px; color:#666" name="EMAIL" value="' . esc_attr( $current_user->user_email ) . '" selector="newsletter-email" placeholder="' . esc_attr__( 'Email', 'formidable' ) . '"/>'
234
						   . '<input type="hidden" name="group[4505]" value="4" />'
235
			               . '<button type="submit" class="button-primary">' . esc_html__( 'Subscribe', 'formidable' ) . '</button>'
236
			               . '</p>'
237
			               . '</form>',
238
			'next_page' => 'entries',
239
		);
240
	}
241
242
	/**
243
	 * Returns the content of the Entries listing page pointer
244
	 *
245
	 * @return array
246
	 */
247 View Code Duplication
	private function entries_pointer() {
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...
248
		return array(
249
			'content'   => '<h3>' . __( 'Entries', 'formidable' ) . '</h3>'
250
			               . '<p>' . __( 'Each time one of your forms is submitted, an entry is created. You will find every form submission listed here so you will always have a backup if an email fails.', 'formidable' ) . '</p>',
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw '__'
Loading history...
251
			'prev_page' => '',
252
			'next_page' => 'styles',
253
		);
254
	}
255
256
	/**
257
	 * Returns the content of the Styles page pointer
258
	 *
259
	 * @return array
260
	 */
261 View Code Duplication
	private function styles_pointer() {
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...
262
		return array(
263
			'content'   => '<h3>' . __( 'Styles', 'formidable' ) . '</h3>'
264
			               . '<p>' . __( 'Want to make changes to the way your forms look? Make all the changes you would like right here, and watch the sample form change before your eyes.', 'formidable' ) . '</p>',
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw '__'
Loading history...
265
			'prev_page' => 'entries',
266
			'next_page' => 'import',
267
		);
268
	}
269
270
	/**
271
	 * Returns the content of the Import/Export page pointer
272
	 *
273
	 * @return array
274
	 */
275 View Code Duplication
	private function import_pointer() {
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...
276
		return array(
277
			'content'   => '<h3>' . __( 'Import/Export', 'formidable' ) . '</h3>'
278
			               . '<p>' . __( 'Import and export forms and styles when copying from one site to another or sharing with someone else. Your entries can be exported to a CSV as well. The Premium version also includes the option to import entries to your site from a CSV.', 'formidable' ) . '</p>',
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw '__'
Loading history...
279
			'prev_page' => 'styles',
280
			'next_page' => 'settings',
281
		);
282
	}
283
284
	/**
285
	 * Returns the content of the advanced page pointer
286
	 *
287
	 * @return array
288
	 */
289
	private function settings_pointer() {
290
		return array(
291
			'content'   => '<h3>' . __( 'Global Settings', 'formidable' ) . '</h3>'
292
				. '<p><strong>' . __( 'General', 'formidable' ) . '</strong><br/>'
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw '__'
Loading history...
293
				. __( 'Turn stylesheets and scripts off, set which user roles have access to change and create forms, setup your reCaptcha, and set default messages for new forms and fields.', 'formidable' )
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw '__'
Loading history...
294
				. '<p><strong>' . __( 'Plugin Licenses', 'formidable' ) . '</strong><br/>'
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw '__'
Loading history...
295
				. sprintf( __( 'Once you&#8217;ve purchased %1$s or any addons, you&#8217;ll have to enter a license key to get access to all of their powerful features. A Plugin Licenses tab will appear here for you to enter your license key.', 'formidable' ), 'Formidable Pro' )
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'sprintf'
Loading history...
296
           	    . '</p>',
297
			'prev_page' => 'import',
298
			'next_page' => 'addons',
299
		);
300
	}
301
302
	/**
303
	 * Returns the content of the extensions and licenses page pointer
304
	 *
305
	 * @return array
306
	 */
307
	private function addons_pointer() {
308
		return array(
309
			'content'   => '<h3>' . __( 'Addons', 'formidable' ) . '</h3>'
310
			               . '<p>' . sprintf( __( 'The powerful functions of %1$s can be extended with %2$spremium plugins%3$s. You can read all about the Formidable Premium Plugins %2$shere%3$s.', 'formidable' ), 'Formidable', '<a target="_blank" href="' . esc_url( FrmAppHelper::make_affiliate_url( 'https://formidablepro.com/' ) ) . '">', '</a>' )
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'sprintf'
Loading history...
311
						   . '</p>'
312
			               . '<p><strong>' . __( 'Like this plugin?', 'formidable' ) . '</strong><br/>' . sprintf( __( 'So, we&#8217;ve come to the end of the tour. If you like the plugin, please %srate it 5 stars on WordPress.org%s!', 'formidable' ), '<a target="_blank" href="https://wordpress.org/plugins/formidable/">', '</a>' ) . '</p>'
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw '__'
Loading history...
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'sprintf'
Loading history...
313
			               . '<p>' . sprintf( __( 'Thank you for using our plugin and good luck with your forms!<br/><br/>Best,<br/>Team Formidable - %1$sformidablepro.com%2$s', 'formidable' ), '<a target="_blank" href="' . esc_url( FrmAppHelper::make_affiliate_url( 'https://formidablepro.com/' ) ) . '">', '</a>' ) . '</p>',
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'sprintf'
Loading history...
314
			'prev_page' => 'settings',
315
		);
316
	}
317
318
	/**
319
	 * Extending the current page URL with two params to be able to ignore the tour.
320
	 *
321
	 * @return mixed
322
	 */
323
	private function get_ignore_url() {
324
		$arr_params = array(
325
			'frm_restart_tour' => false,
326
			'frm_ignore_tour'  => '1',
327
			'nonce'            => wp_create_nonce( 'frm-ignore-tour' ),
328
		);
329
330
		return esc_url( add_query_arg( $arr_params ) );
331
	}
332
}
333