Completed
Branch BETA-4.9-message-activity (793322)
by
unknown
33:08 queued 14:47
created

PostShortcodeTracking   B

Complexity

Total Complexity 52

Size/Duplication

Total Lines 405
Duplicated Lines 1.73 %

Coupling/Cohesion

Components 1
Dependencies 4
Metric Value
wmc 52
lcom 1
cbo 4
dl 7
loc 405
rs 7.9487

10 Methods

Rating   Name   Duplication   Size   Complexity  
B set_hooks_admin() 0 32 1
A set_post_shortcodes_for_posts_page() 0 10 3
A set_post_shortcode_for_posts_page() 0 21 4
C unset_post_shortcodes_on_delete() 0 35 8
B unset_posts_page_shortcode_for_post() 0 26 5
C update_post_shortcodes() 0 63 12
A reset_page_for_posts_on_initial_set() 0 3 1
A reset_page_for_posts_on_delete() 0 5 2
C parse_post_content_on_save() 0 73 13
A reset_page_for_posts_on_change() 7 9 3

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like PostShortcodeTracking 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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 PostShortcodeTracking, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace EventEspresso\core\admin;
3
4
if ( ! defined( 'EVENT_ESPRESSO_VERSION' ) ) {
5
	exit( 'No direct script access allowed' );
6
}
7
8
9
10
/**
11
 * Class PostShortcodeTracking
12
 *
13
 * Description
14
 *
15
 * @package       Event Espresso
16
 * @subpackage    core
17
 * @author        Brent Christensen
18
 * @since         $VID:$
19
 *
20
 */
21
class PostShortcodeTracking {
22
23
	/**
24
	 * set_hooks_admin
25
	 *
26
	 * @access    public
27
	 */
28
	public static function set_hooks_admin() {
29
		add_action(
30
			'save_post',
31
			array( 'EventEspresso\core\admin\PostShortcodeTracking', 'parse_post_content_on_save' ),
32
			100,
33
			2
34
		);
35
		add_action(
36
			'delete_post',
37
			array( 'EventEspresso\core\admin\PostShortcodeTracking', 'unset_post_shortcodes_on_delete' ),
38
			100,
39
			1
40
		);
41
		add_action(
42
			'add_option_page_for_posts',
43
			array( 'EventEspresso\core\admin\PostShortcodeTracking', 'reset_page_for_posts_on_initial_set' ),
44
			100,
45
			2
46
		);
47
		add_action(
48
			'update_option',
49
			array( 'EventEspresso\core\admin\PostShortcodeTracking', 'reset_page_for_posts_on_change' ),
50
			100,
51
			3
52
		);
53
		add_action(
54
			'delete_option',
55
			array( 'EventEspresso\core\admin\PostShortcodeTracking', 'reset_page_for_posts_on_delete' ),
56
			100,
57
			1
58
		);
59
	}
60
61
62
63
	/**
64
	 *    parse_post_content_on_save
65
	 *
66
	 *    any time a post is saved, we need to check for any EE shortcodes that may be embedded in the content,
67
	 *    and then track what posts those shortcodes are on, so that we can initialize shortcodes well before the_content() runs.
68
	 *    this allows us to do things like enqueue scripts for shortcodes ONLY on the pages the shortcodes are actually used on
69
	 *
70
	 * @access    public
71
	 * @param int     $post_ID
72
	 * @param \WP_Post $post
73
	 * @return    void
74
	 */
75
	public static function parse_post_content_on_save( $post_ID, $post ) {
76
		// if the post is trashed, then let's remove our post shortcode tracking
77
		if ( $post instanceof \WP_Post && $post->post_status == 'trash' ) {
0 ignored issues
show
Bug introduced by
The class WP_Post does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
78
			PostShortcodeTracking::unset_post_shortcodes_on_delete( $post_ID );
79
			return;
80
		}
81
		// default post types
82
		$post_types = array( 'post' => 0, 'page' => 1 );
83
		// add CPTs
84
		$CPTs = \EE_Register_CPTs::get_CPTs();
85
		$post_types = array_merge( $post_types, $CPTs );
86
		// for default or CPT posts...
87
		if ( isset( $post_types[ $post->post_type ] ) ) {
88
			// post on frontpage ?
89
			$page_for_posts = \EE_Config::get_page_for_posts();
90
			if ( $post->post_name == $page_for_posts ) {
91
				PostShortcodeTracking::set_post_shortcodes_for_posts_page( $page_for_posts );
92
				return;
93
			}
94
			// array of shortcodes indexed by post name
95
			\EE_Registry::CFG()->core->post_shortcodes = isset( \EE_Registry::CFG()->core->post_shortcodes )
96
				? \EE_Registry::CFG()->core->post_shortcodes
97
				: array();
98
			// whether to proceed with update
99
			$update_post_shortcodes = false;
100
			// empty both arrays
101
			\EE_Registry::CFG()->core->post_shortcodes[ $post->post_name ] = array();
102
			// check that posts page is already being tracked
103
			if ( ! isset( \EE_Registry::CFG()->core->post_shortcodes[ $page_for_posts ] ) ) {
104
				// if not, then ensure that it is properly added
105
				\EE_Registry::CFG()->core->post_shortcodes[ $page_for_posts ] = array();
106
			}
107
			// loop thru shortcodes
108
			foreach ( \EE_Registry::instance()->shortcodes as $EES_Shortcode => $shortcode_dir ) {
109
				// convert to UPPERCASE to get actual shortcode
110
				$EES_Shortcode = strtoupper( $EES_Shortcode );
111
				// is the shortcode in the post_content ?
112
				if ( strpos( $post->post_content, $EES_Shortcode ) !== false ) {
113
					// map shortcode to post names and post IDs
114
					\EE_Registry::CFG()->core->post_shortcodes[ $post->post_name ][ $EES_Shortcode ] = $post_ID;
115
					// and add this shortcode to the tracking for the blog page
116
					PostShortcodeTracking::set_post_shortcode_for_posts_page( $page_for_posts, $EES_Shortcode, $post_ID );
117
					$update_post_shortcodes = true;
118
				} else {
119
					// shortcode is not present in post content, so check if we were tracking it previously
120
					// stop tracking if shortcode is not used in this specific post
121
					if ( isset( \EE_Registry::CFG()->core->post_shortcodes[ $post->post_name ][ $EES_Shortcode ] ) ) {
122
						unset( \EE_Registry::CFG()->core->post_shortcodes[ $post->post_name ][ $EES_Shortcode ] );
123
						$update_post_shortcodes = true;
124
					}
125
					// make sure that something is set for the shortcode posts (even though we may remove this)
126
					$shortcode_posts = isset(
127
						\EE_Registry::CFG()->core->post_shortcodes[ $page_for_posts ][ $EES_Shortcode ]
128
					)
129
						? \EE_Registry::CFG()->core->post_shortcodes[ $page_for_posts ][ $EES_Shortcode ]
130
						: array();
131
					// and stop tracking for this shortcode on the blog page if it is not used
132
					$update_post_shortcodes = PostShortcodeTracking::unset_posts_page_shortcode_for_post(
133
						$post_ID,
134
						$EES_Shortcode,
135
						$shortcode_posts,
136
						$page_for_posts,
137
						$update_post_shortcodes
138
					)
139
						? true
140
						: $update_post_shortcodes;
141
				}
142
			}
143
			if ( $update_post_shortcodes ) {
144
				PostShortcodeTracking::update_post_shortcodes( $page_for_posts );
145
			}
146
		}
147
	}
148
149
150
151
	/**
152
	 * set_post_shortcodes_for_posts_page (plz note: shortcodes is plural)
153
	 *
154
	 * called when updating the WordPress Posts Page,
155
	 * and adds shortcode tracking for the Posts Page, for all shortcodes currently tracked on individual posts
156
	 *
157
	 * @access protected
158
	 * @param  string $page_for_posts
159
	 * @return void
160
	 */
161
	protected static function set_post_shortcodes_for_posts_page( $page_for_posts ) {
162
		\EE_Registry::CFG()->core->post_shortcodes[ $page_for_posts ] = array();
163
		// loop thru shortcodes
164
		foreach ( \EE_Registry::CFG()->core->post_shortcodes as $post_name => $post_shortcodes ) {
165
			foreach ( $post_shortcodes as $EES_Shortcode => $post_ID ) {
166
				PostShortcodeTracking::set_post_shortcode_for_posts_page( $page_for_posts, $EES_Shortcode, $post_ID );
167
			}
168
		}
169
		PostShortcodeTracking::update_post_shortcodes( $page_for_posts );
170
	}
171
172
173
174
	/**
175
	 * set_post_shortcode_for_posts_page (plz note: shortcode is singular)
176
	 *
177
	 * adds Posts Page shortcode tracking for the supplied shortcode for an individual post
178
	 *
179
	 * @access protected
180
	 * @param  string $page_for_posts
181
	 * @param         $EES_Shortcode
182
	 * @param         $post_ID
183
	 */
184
	protected static function set_post_shortcode_for_posts_page( $page_for_posts, $EES_Shortcode, $post_ID ) {
185
		// critical page shortcodes that we do NOT want added to the Posts page (blog)
186
		$critical_shortcodes = \EE_Registry::CFG()->core->get_critical_pages_shortcodes_array();
187
		// if the shortcode is NOT one of the critical page shortcodes like ESPRESSO_TXN_PAGE
188
		if ( in_array( $EES_Shortcode, $critical_shortcodes ) ) {
189
			return;
190
		}
191
		// add shortcode to "Posts page" tracking
192
		if ( isset( \EE_Registry::CFG()->core->post_shortcodes[ $page_for_posts ][ $EES_Shortcode ] ) ) {
193
			// make sure tracking is in form of an array
194
			if ( ! is_array( \EE_Registry::CFG()->core->post_shortcodes[ $page_for_posts ][ $EES_Shortcode ] ) ) {
195
				\EE_Registry::CFG()->core->post_shortcodes[ $page_for_posts ][ $EES_Shortcode ] = array(
196
					\EE_Registry::CFG()->core->post_shortcodes[ $page_for_posts ][ $EES_Shortcode ] => true
197
				);
198
			}
199
			\EE_Registry::CFG()->core->post_shortcodes[ $page_for_posts ][ $EES_Shortcode ] =
200
				\EE_Registry::CFG()->core->post_shortcodes[ $page_for_posts ][ $EES_Shortcode ] + array( $post_ID => true );
201
		} else {
202
			\EE_Registry::CFG()->core->post_shortcodes[ $page_for_posts ][ $EES_Shortcode ] = array( $post_ID => true );
203
		}
204
	}
205
206
207
208
	/**
209
	 * unset_post_shortcodes_on_delete
210
	 *
211
	 * @access protected
212
	 * @param  int $ID
213
	 * @return void
214
	 */
215
	protected static function unset_post_shortcodes_on_delete( $ID ) {
216
		$update_post_shortcodes = false;
217
		// post on frontpage ?
218
		$page_for_posts = \EE_Config::get_page_for_posts();
219
		// looking for any references to this post
220
		foreach ( \EE_Registry::CFG()->core->post_shortcodes as $post_name => $post_shortcodes ) {
221
			// is this the "Posts Page" (blog) ?
222
			if ( $post_name == $page_for_posts ) {
223
				// loop thru shortcodes registered for the posts page
224
				foreach ( $post_shortcodes as $shortcode_class => $shortcode_posts ) {
225
					$update_post_shortcodes = PostShortcodeTracking::unset_posts_page_shortcode_for_post(
226
						$ID,
227
						$shortcode_class,
228
						$shortcode_posts,
229
						$page_for_posts,
230
						$update_post_shortcodes
231
					)
232
						? true
233
						: $update_post_shortcodes;
234
				}
235
			} else {
236
				// loop thru shortcodes registered for each page
237
				foreach ( $post_shortcodes as $shortcode_class => $post_ID ) {
238
					// if this is page is being deleted, then don't track any post shortcodes for it
239
					if ( $post_ID == $ID ) {
240
						unset( \EE_Registry::CFG()->core->post_shortcodes[ $post_name ] );
241
						$update_post_shortcodes = true;
242
					}
243
				}
244
			}
245
		}
246
		if ( $update_post_shortcodes ) {
247
			PostShortcodeTracking::update_post_shortcodes( $page_for_posts );
248
		}
249
	}
250
251
252
253
	/**
254
	 * unset_post_shortcodes_on_delete
255
	 *
256
	 * @access protected
257
	 * @param  int $ID
258
	 * @param      $shortcode_class
259
	 * @param      $shortcode_posts
260
	 * @param      $page_for_posts
261
	 * @param bool $update_post_shortcodes
262
	 * @return bool
263
	 */
264
	protected static function unset_posts_page_shortcode_for_post(
265
		$ID,
266
		$shortcode_class,
267
		$shortcode_posts,
268
		$page_for_posts,
269
		$update_post_shortcodes = false
270
	) {
271
		// make sure that an array of post IDs is being tracked for each  shortcode
272
		if ( ! is_array( $shortcode_posts ) ) {
273
			\EE_Registry::CFG()->core->post_shortcodes[ $page_for_posts ][ $shortcode_class ] = array(
274
				$shortcode_posts => true
275
			);
276
			$update_post_shortcodes = true;
277
		}
278
		// now if the ID of the post being deleted is in the $shortcode_posts array
279
		if ( is_array( $shortcode_posts ) && isset( $shortcode_posts[ $ID ] ) ) {
280
			unset( \EE_Registry::CFG()->core->post_shortcodes[ $page_for_posts ][ $shortcode_class ][ $ID ] );
281
			$update_post_shortcodes = true;
282
		}
283
		// if nothing is registered for that shortcode anymore, then delete the shortcode altogether
284
		if ( empty( \EE_Registry::CFG()->core->post_shortcodes[ $page_for_posts ][ $shortcode_class ] ) ) {
285
			unset( \EE_Registry::CFG()->core->post_shortcodes[ $page_for_posts ][ $shortcode_class ] );
286
			$update_post_shortcodes = true;
287
		}
288
		return $update_post_shortcodes;
289
	}
290
291
292
293
	/**
294
	 *    update_post_shortcodes
295
	 *
296
	 * @access    public
297
	 * @param $page_for_posts
298
	 * @return    void
299
	 */
300
	public static function update_post_shortcodes( $page_for_posts = '' ) {
301
		// make sure page_for_posts is set
302
		$page_for_posts = ! empty( $page_for_posts )
303
			? $page_for_posts
304
			: \EE_Config::get_page_for_posts();
305
		// allow others to mess stuff up :D
306
		do_action(
307
			'AHEE__\EventEspresso\core\admin\PostShortcodeTracking__update_post_shortcodes',
308
			\EE_Config::instance()->core->post_shortcodes,
309
			$page_for_posts
310
		);
311
		// keep old hookpoint for now, will deprecate later
312
		do_action(
313
			'AHEE__EE_Config__update_post_shortcodes',
314
			\EE_Config::instance()->core->post_shortcodes,
315
			$page_for_posts
316
		);
317
		// verify that post_shortcodes is set
318
		\EE_Config::instance()->core->post_shortcodes = isset( \EE_Config::instance()->core->post_shortcodes )
319
		                                                && is_array( \EE_Config::instance()->core->post_shortcodes )
320
			? \EE_Config::instance()->core->post_shortcodes
321
			: array();
322
		// cycle thru post_shortcodes
323
		foreach ( \EE_Config::instance()->core->post_shortcodes as $post_name => $shortcodes ) {
324
			// are there any shortcodes to track ?
325
			if ( ! empty( $shortcodes ) ) {
326
				// loop thru list of tracked shortcodes
327
				foreach ( $shortcodes as $shortcode => $post_id ) {
328
					// if shortcode is for a critical page,
329
					// BUT this is NOT the corresponding critical page for that shortcode
330
					if ( $post_name == $page_for_posts ) {
331
						continue;
332
					}
333
					// skip the posts page, because we want all shortcodes registered for it
334
					if ( $post_name == $page_for_posts ) {
335
						continue;
336
					}
337
					// make sure post still exists
338
					$post = get_post( $post_id );
339
					if ( $post ) {
340
						// check that the post name matches what we have saved
341
						if ( $post->post_name == $post_name ) {
342
							// if so, then break before hitting the unset below
343
							continue;
344
						}
345
					}
346
					// we don't like missing posts around here >:(
347
					unset( \EE_Config::instance()->core->post_shortcodes[ $post_name ] );
348
				}
349
			} else {
350
				// you got no shortcodes to keep track of !
351
				unset( \EE_Config::instance()->core->post_shortcodes[ $post_name ] );
352
			}
353
		}
354
		// critical page shortcodes that we do NOT want added to the Posts page (blog)
355
		$critical_shortcodes = \EE_Config::instance()->core->get_critical_pages_shortcodes_array();
356
		$critical_shortcodes = array_flip( $critical_shortcodes );
357
		foreach ( $critical_shortcodes as $critical_shortcode ) {
358
			unset( \EE_Config::instance()->core->post_shortcodes[ $page_for_posts ][ $critical_shortcode ] );
359
		}
360
		//only show errors
361
		\EE_Config::instance()->update_espresso_config();
362
	}
363
364
365
366
	/**
367
	 * reset_page_for_posts_on_initial_set
368
	 *
369
	 * if an admin is on the WP Reading Settings page and sets the option for "Posts page",
370
	 * when it had previously been unset,
371
	 * then we need to attribute any actively used shortcodes to the new blog page
372
	 *
373
	 * @access public
374
	 * @param  string $option
375
	 * @param  string $value
376
	 * @return void
377
	 */
378
	public static function reset_page_for_posts_on_initial_set( $option, $value ) {
379
		PostShortcodeTracking::reset_page_for_posts_on_change( $option, '', $value );
380
	}
381
382
383
384
	/**
385
	 *    reset_page_for_posts_on_change
386
	 *
387
	 *    if an admin is on the WP Reading Settings page and changes the option for "Posts page",
388
	 * then we need to attribute any actively used shortcodes for the previous blog page to the new blog page
389
	 *
390
	 * @access public
391
	 * @param  string $option
392
	 * @param  string $old_value
393
	 * @param  string $value
394
	 * @return void
395
	 */
396
	public static function reset_page_for_posts_on_change( $option, $old_value = '', $value = '' ) {
397 View Code Duplication
		if ( $option == 'page_for_posts' ) {
398
			global $wpdb;
399
			$table = $wpdb->posts;
400
			$SQL = "SELECT post_name from $table WHERE post_type='posts' OR post_type='page' AND post_status='publish' AND ID=%d";
401
			$new_page_for_posts = $value ? $wpdb->get_var( $wpdb->prepare( $SQL, $value ) ) : 'posts';
402
			PostShortcodeTracking::set_post_shortcodes_for_posts_page( $new_page_for_posts );
403
		}
404
	}
405
406
407
408
	/**
409
	 * reset_page_for_posts_on_delete
410
	 *
411
	 * if an admin deletes a page designated as the WP "Posts page",
412
	 * then we need to attribute any actively used shortcodes for that blog page to a generic 'posts' page
413
	 *
414
	 * @access public
415
	 * @param  string $option
416
	 * @return void
417
	 */
418
	public static function reset_page_for_posts_on_delete( $option ) {
419
		if ( $option == 'page_for_posts' ) {
420
			PostShortcodeTracking::set_post_shortcodes_for_posts_page( 'posts' );
421
		}
422
	}
423
424
425
}
426
// End of file PostShortcodeTracking.php
427
// Location: /PostShortcodeTracking.php