1 | <?php |
||
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 ) { |
|
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] ) ) { |
|
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; |
|
65 | } |
||
66 | |||
67 | 1 | return self::$shortcodes[$shortcode->name]; |
|
68 | } |
||
69 | |||
70 | /** |
||
71 | * Unregister this shortcode. |
||
72 | * |
||
73 | * @internal |
||
74 | * |
||
75 | * @return void |
||
76 | */ |
||
77 | 1 | public static function remove() { |
|
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 ) { |
|
134 | } |
||
135 |
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.