Sensei_Shortcode_Loader::possibly_add_body_class()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 27
Code Lines 10

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 27
rs 8.439
cc 5
eloc 10
nc 4
nop 1
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 19 and the first side effect is on line 2.

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
if ( ! defined( 'ABSPATH' ) ) exit; // security check
3
/**
4
 * Sensei Shortcode Loader Class
5
 *
6
 * This class handles the api for all Sensei shortcodes. It does not
7
 * execute on the shortcodes directly but relies on a class that responds
8
 * to each shortcode. Whe WordPress calls do_shortcode for a shortcode registered
9
 * in this function, the functions load_shortcode will be called and it will
10
 * instantiate the correct shortcode handling class as it was registered.
11
 *
12
 *
13
 * @package Content
14
 * @subpackage Shortcode
15
 * @author Automattic
16
 *
17
 * @since 1.9.0
18
 */
19
class Sensei_Shortcode_Loader{
20
21
    /**
22
     * @var array {
23
     *  type string $shortcode
24
     *  type Sensei_Shortcode
25
     * } all the shortcodes and which class to instantiate when they are called from
26
     * WordPress's do_shortcode() function.
27
     *
28
     */
29
    protected $shortcode_classes;
30
31
    /**
32
     * Run all the functions that needs to be hooked into WordPress
33
     *
34
     * @since 1.9.0
35
     */
36
    public function __construct(){
37
38
        // create a list of shortcodes and the class that handles them
39
        $this->setup_shortcode_class_map();
40
41
        // setup all the shortcodes and load the listener into WP
42
        $this->initialize_shortcodes();
43
44
        // add sensei body class for shortcodes
45
        add_filter( 'body_class', array( $this, 'possibly_add_body_class' ));
46
47
        // array( $this, 'add_body_class')
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
48
49
    }
50
51
    /**
52
     * Array of shortcode classes that should be instantiated when WordPress loads
53
     * a Sensei specific shortcode.
54
     * This list contains:
55
     * $shortcode => $class_name
56
     *
57
     * $shortcode is the actual shortcode the user will add to the editor
58
     * $class_name is the name of the class that will be instantiated to handle
59
     * the rendering of the shortcode.
60
     *
61
     * NOTE: When adding a new shortcode here be sure to load your shortcodes class
62
     * in class-sensei-autoloader class_file_map function
63
     */
64
    public function setup_shortcode_class_map(){
65
66
        $this->shortcode_classes = array(
67
            'sensei_featured_courses'    => 'Sensei_Shortcode_Featured_Courses',
68
            'sensei_user_courses'        => 'Sensei_Shortcode_User_Courses',
69
            'sensei_courses'             => 'Sensei_Shortcode_Courses',
70
            'sensei_teachers'            => 'Sensei_Shortcode_Teachers',
71
            'sensei_user_messages'       => 'Sensei_Shortcode_User_Messages',
72
            'sensei_course_page'         => 'Sensei_Shortcode_Course_Page',
73
            'sensei_lesson_page'         => 'Sensei_Shortcode_Lesson_Page',
74
            'sensei_course_categories'   => 'Sensei_Shortcode_Course_Categories',
75
            'sensei_unpurchased_courses' => 'Sensei_Shortcode_Unpurchased_Courses',
76
        );
77
78
        // legacy shortcode handling:
79
        Sensei_Legacy_Shortcodes::init();
80
81
    }
82
83
    /**
84
     * Add all shortcodes here
85
     *
86
     * This function adds shortcodes to WP that links to other functionality.
87
     * @since 1.9.0
88
     */
89
    public function initialize_shortcodes(){
90
91
        // shortcodes should only respond to front end calls
92
        if( is_admin() || defined( 'DOING_AJAX' ) ){
93
            return;
94
        }
95
96
        /**
97
         * Tell WP to run this classes load_shortcode function for all the
98
         * shortcodes registered here in.
99
         *
100
         * With this method we only load shortcode classes when we need them.
101
         */
102
        foreach( $this->shortcode_classes as $shortcode => $class ){
103
104
            // all Sensei shortcodes are rendered by this loader class
105
            // it acts as an interface between wp and the shortcodes registered
106
            // above
107
            add_shortcode( $shortcode, array( $this,'render_shortcode' ) );
108
109
        }
110
111
    }
112
113
    /**
114
     * Respond to WordPress do_shortcode calls
115
     * for shortcodes registered in the initialize_shortcodes function.
116
     *
117
     * @since 1.9.0
118
     *
119
     * @param $attributes
120
     * @param $content
121
     * @param $code the shortcode that is being requested
122
     *
123
     * @return string
124
     */
125
    public function render_shortcode( $attributes='', $content='', $code ){
126
127
        // only respond if the shortcode that we've added shortcode
128
        // classes for.
129
        if( ! isset( $this->shortcode_classes[ $code ] ) ){
130
            return '';
131
        }
132
133
        // create an instances of the current requested shortcode
134
        $shortcode_handling_class = $this->shortcode_classes[ $code ];
135
        $shortcode = new $shortcode_handling_class( $attributes, $content, $code );
136
137
        // we expect the sensei class instantiated to implement the Sensei_Shortcode interface
138
        if( ! in_array( 'Sensei_Shortcode_Interface', class_implements( $shortcode) ) ){
139
140
            $message = "The rendering class for your shortcode: $code, must implement the Sensei_Shortcode interface";
141
            _doing_it_wrong('Sensei_Shortcode_Loader::render_shortcode',$message, '1.9.0' );
142
143
        }
144
145
        return $shortcode->render();
146
147
    }
148
149
    /**
150
     * Add the Sensei body class if
151
     * the current page has a Sensei shortcode.
152
     *
153
     * Note: legacy shortcodes not supported here.
154
     *
155
     * @since 1.9.0
156
     *
157
     * @param array $classes
158
     * @return array
159
     */
160
    public function possibly_add_body_class ( $classes ) {
161
162
        global $post;
163
164
        $has_sensei_shortcode = false;
165
166
        if ( is_a( $post, 'WP_Post' ) ) {
167
168
            // check all registered Sensei shortcodes (not legacy shortcodes)
169
            foreach ( $this->shortcode_classes as $shortcode => $class ){
170
171
                if ( has_shortcode( $post->post_content, $shortcode ) ) {
172
173
                    $has_sensei_shortcode = true;
174
                }
175
176
            }
177
        }
178
179
        if( $has_sensei_shortcode ) {
180
            $classes[] = 'sensei' ;
181
        }
182
183
184
        return $classes;
185
186
    }
187
188
} // end class Sensei_Shortcodes
189
new Sensei_Shortcode_Loader();
190