Completed
Push — master ( d07b76...196a0a )
by Zack
18:54 queued 11:24
created

Shortcode   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 122
ccs 30
cts 30
cp 1
rs 10
c 1
b 0
f 0
wmc 8
lcom 1
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A callback() 0 3 1
A remove() 0 5 1
A add() 0 13 3
B parse() 0 39 3
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 13 and the first side effect is on line 5.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
namespace GV;
3
4
/** If this file is called directly, abort. */
5
if ( ! defined( 'GRAVITYVIEW_DIR' ) )
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
6
	die();
7
8
/**
9
 * The base \GV\Shortcode class.
10
 *
11
 * Contains some unitility methods, base class for all GV Shortcodes.
12
 */
13
class Shortcode {
14
	/*
15
	 * @var array All GravityView-registered and loaded shortcodes can be found here.
16
	 */
17
	private static $shortcodes;
18
19
	/**
20
	 * @var array The parsed attributes of this shortcode.
21
	 */
22
	public $atts;
23
24
	/**
25
	 * @var string The parsed name of this shortcode.
26
	 */
27
	public $name;
28
29
	/**
30
	 * @var string The parsed content between tags of this shortcode.
31
	 */
32
	public $content;
33
34
	/**
35
	 * The WordPress Shortcode API callback for this shortcode.
36
	 *
37
	 * @param array $atts The callback shortcode attributes.
38
	 * @param string|null $content The wrapped content. Default: null.
39
	 *
40
	 * @throws \BadMethodCallException in base class.
41
	 *
42
	 * @return string The result of the shortcode logic.
43
	 */
44 1
	public static function callback( $atts, $content = null ) {
0 ignored issues
show
Unused Code introduced by
The parameter $atts is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $content is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
45 1
		throw new \BadMethodCallException( 'Callback not implemented in base \GV\Shortcode class.' );
46
	}
47
48
	/**
49
	 * Register this shortcode class with the WordPress Shortcode API.
50
	 *
51
	 * @throws \ErrorException if shortcode with this name has already been registered elsewhere.
52
	 * @internal
53
54
	 * @return \GV\Shortcode The only internally registered instance of this shortcode.
55
	 */
56 1
	public static function add() {
57 1
		$shortcode = new static();
58 1
		if ( shortcode_exists( $shortcode->name ) ) {
59 1
			if ( empty( self::$shortcodes[$shortcode->name] ) ) {
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
60 1
				throw new \ErrorException( sprintf( 'Shortcode [%s] has already been registered elsewhere.', $shortcode->name ) );
61
			}
62
		} else {
63 1
			add_shortcode( $shortcode->name, array( get_class( $shortcode ), 'callback' ) );
64 1
			self::$shortcodes[$shortcode->name] = $shortcode;
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
65
		}
66
67 1
		return self::$shortcodes[$shortcode->name];
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
68
	}
69
70
	/**
71
	 * Unregister this shortcode.
72
	 *
73
	 * @internal
74
	 *
75
	 * @return void
76
	 */
77 1
	public static function remove() {
78 1
		$shortcode = new static();
79 1
		unset( self::$shortcodes[$shortcode->name] );
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
80 1
		remove_shortcode( $shortcode->name );
81 1
	}
82
83
	/**
84
	 * Parse a string of content and figure out which ones there are.
85
	 *
86
	 * Only registered shortcodes (via add_shortcode) will show up.
87
	 * Returned order is not guaranteed.
88
	 *
89
	 * @param string $content Some post content to search through.
90
	 *
91
	 * @internal
92
	 *
93
	 * @return \GV\Shortcode[] An array of \GV\Shortcode (and subclass) instances.
94
	 */
95 1
	public static function parse( $content ) {
96 1
		$shortcodes = array();
97
98
		/**
99
		 * The matches contains:
100
		 *
101
		 * 1 - An extra [ to allow for escaping shortcodes with double [[]]
102
		 * 2 - The shortcode name
103
		 * 3 - The shortcode argument list
104
		 * 4 - The self closing /
105
		 * 5 - The content of a shortcode when it wraps some content.
106
		 * 6 - An extra ] to allow for escaping shortcodes with double [[]]
107
		 */
108 1
		preg_match_all( '/' . get_shortcode_regex() . '/', $content, $matches, PREG_SET_ORDER );
109
110 1
		foreach ( $matches as $shortcode ) {
111 1
			$shortcode_name = $shortcode[2];
112
113 1
			$shortcode_atts = shortcode_parse_atts( $shortcode[3] );
114 1
			$shortcode_content = $shortcode[5];
115
116
			/** This is a registered GravityView shortcode. */
117 1
			if ( !empty( self::$shortcodes[$shortcode_name] ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
118 1
				$shortcode = clone( self::$shortcodes[$shortcode_name] );
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
119
			} else {
120
				/** This is some generic shortcode. */
121 1
				$shortcode = new self;
122 1
				$shortcode->name = $shortcode_name;
123
			}
124
125 1
			$shortcode->atts = $shortcode_atts;
126 1
			$shortcode->content = $shortcode_content;
127
128
			/** Merge inner shortcodes. */
129 1
			$shortcodes = array_merge( $shortcodes, array( $shortcode ), self::parse( $shortcode_content ) );
130
		}
131
132 1
		return $shortcodes;
133
	}
134
}
135