Passed
Pull Request — master (#40)
by
unknown
02:23
created

Metabox::addMetabox()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 10
nc 2
nop 0
dl 0
loc 15
rs 9.4285
c 1
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the Subway WordPress Plugin Package.
4
 * This file contains the class which handles the metabox of the plugin.
5
 *
6
 * (c) Jasper Jardin <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * PHP Version 5.4
12
 *
13
 * @category Subway\Metabox
14
 * @package  Subway
15
 * @author   Jasper J. <[email protected]>
16
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
17
 * @version  GIT:github.com/codehaiku/subway
18
 * @link     github.com/codehaiku/subway The Plugin Repository
19
 */
20
21
namespace Subway;
22
23
if (! defined('ABSPATH') ) {
24
    return;
25
}
26
27
/**
28
 * Subway Metabox methods.
29
 *
30
 * @category Subway\Metabox
31
 * @package  Subway
32
 * @author   Jasper J. <[email protected]>
33
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
34
 * @link     github.com/codehaiku/subway The Plugin Repository
35
 * @since    2.0.9
36
 */
37
final class Metabox
38
{
39
40
    /**
41
     * Subway visibility meta value,
42
     *
43
     * @since 2.0.9
44
     * @const string VISIBILITY_METAKEY
45
     */
46
    const VISIBILITY_METAKEY = 'subway_visibility_meta_key';
47
48
    /**
49
     * Registers and update metabox with its intended method below.
50
     *
51
     * @since  2.0.9
52
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
53
     */
54
    public function __construct()
55
    {
56
        add_action(
57
            'add_meta_boxes',
58
            array(
59
            $this,
60
            'addMetabox'
61
            )
62
        );
63
        add_action(
64
            'save_post',
65
            array(
66
            $this,
67
            'saveMetaboxValues'
68
            )
69
        );
70
    }
71
72
    /**
73
     * Initialize metabox
74
     *
75
     * @since  2.0.9
76
     * @access public
77
     * @return void
78
     */
79
    public static function initMetabox()
80
    {
81
        new Metabox();
82
    }
83
84
    /**
85
     * Initialize metabox
86
     *
87
     * @since  2.0.9
88
     * @access public
89
     * @return void
90
     */
91
    public function addMetabox()
92
    {
93
        $post_types = $this->getPostTypes();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $post_types is correct as $this->getPostTypes() (which targets Subway\Metabox::getPostTypes()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
94
95
        foreach ( $post_types as $post_type => $value ) {
0 ignored issues
show
Bug introduced by
The expression $post_types of type null is not traversable.
Loading history...
96
            add_meta_box(
97
                'subway_visibility_metabox',
98
                esc_html__('Subway: Visibility Option', 'subway'),
99
                array( $this, 'visibilityMetabox' ),
100
                $post_type,
101
                'side',
102
                'low'
103
            );
104
        }
105
    }
106
107
    /**
108
     * This method displays the Subway Visibility checkbox.
109
     *
110
     * @param object $post Contains data from the current post.
111
     *
112
     * @since  2.0.9
113
     * @access public
114
     * @return void
115
     */
116
    public function visibilityMetabox( $post )
117
    {
118
        $howto = __(
119
            'Select a radio button to make this post/page public or private',
120
            'subway'
121
        );
122
        $public_label = '<strong>'. __('public', 'subway') .'</strong>';
123
        $public_setting_label = __(
124
            'Select to make this post ',
125
            'subway'
126
        ) . $public_label;
127
        $private_label = '<strong>'. __('private', 'subway') .'</strong>';
128
        $private_setting_label = __(
129
            'Select to make this post ',
130
            'subway'
131
        ) . $private_label;
132
        $is_post_public          = self::isPostPublic($post->ID);
133
        $is_post_private         = self::isPostPrivate($post->ID);
134
        $public_value            = '';
135
        $private_value           = '';
136
137
        if ($is_post_public ) {
138
            $public_value = 'public';
139
        }
140
        if ($is_post_private ) {
141
            $private_value = 'private';
142
        }
143
144
        // Make sure the form request comes from WordPress
145
        wp_nonce_field(
146
            basename(__FILE__),
147
            'subway_post_visibility_nonce'
148
        );
149
        ?>
150
151
        <label class="screen-reader-text" for="subway-visibility">
152
        <?php echo esc_html($setting_label); ?>
0 ignored issues
show
Bug introduced by
The variable $setting_label does not exist. Did you mean $public_setting_label?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
153
        </label>
154
155
        <label class="subway-visibility-settings-checkbox-label"
156
            for="subway-visibility-public">
157
158
            <input type="radio" class="subway-visibility-settings-radio"
159
            id="subway-visibility-public"
160
            name="subway-visibility-settings" value="public"
161
            <?php echo checked('public', esc_attr($public_value), false); ?>>
162
163
            <?php
164
                echo wp_kses(
165
                    $public_setting_label,
166
                    array(
167
                    'strong' => array()
168
                    )
169
                );
170
            ?>
171
        </label><br/>
172
173
        <label class="subway-visibility-settings-checkbox-label"
174
            for="subway-visibility-private">
175
176
            <input type="radio" class="subway-visibility-settings-radio"
177
            id="subway-visibility-private" name="subway-visibility-settings"
178
            value="private"
179
        <?php echo checked('private', esc_attr($private_value), false); ?>>
180
181
            <?php
182
            echo wp_kses(
183
                $private_setting_label,
184
                array( 'strong' => array() )
185
            );
186
            ?>
187
        </label>
188
189
        <p class="howto"><?php echo esc_html($howto); ?></p>
190
191
        <?php
192
    }
193
194
    /**
195
     * This method verify if nonce is valid then updates a post_meta.
196
     *
197
     * @param integer $post_id Contains ID of the current post.
198
     *
199
     * @since  2.0.9
200
     * @access public
201
     * @return boolean false Returns false if nonce is not valid.
202
     */
203
    public function saveVisibilityMetabox( $post_id = '' )
204
    {
205
206
        $public_posts     = Options::getPublicPostsIdentifiers();
207
208
        $posts_implode    = '';
0 ignored issues
show
Unused Code introduced by
$posts_implode is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
209
210
        $visibility_field = 'subway-visibility-settings';
211
212
        $visibility_nonce = filter_input(
213
            INPUT_POST,
214
            'subway_post_visibility_nonce',
215
            FILTER_SANITIZE_STRING
216
        );
217
218
        $post_visibility = filter_input(
219
            INPUT_POST,
220
            $visibility_field,
221
            FILTER_SANITIZE_STRING
222
        );
223
224
        $is_valid_visibility_nonce = self::isNonceValid(
225
            $visibility_nonce
226
        );
227
228
        // verify taxonomies meta box nonce
229
        if (false === $is_valid_visibility_nonce ) {
230
            return;
231
        }
232
233
        if (! empty($post_visibility) ) {
234
            if (! empty($post_id) ) {
235
                if ('public' === $post_visibility ) {
236
                    if (! in_array($post_id, $public_posts) ) {
237
                        array_push($public_posts, $post_id);
238
                    }
239
                }
240
                if ('private' === $post_visibility ) {
241
                    unset($public_posts[ array_search($post_id, $public_posts) ]);
242
                }
243
            }
244
        }
245
246
        if (! empty($post_id) ) {
247
            $posts_implode = implode(", ", $public_posts);
248
249
            if ('inherit' !== get_post_status($post_id) ) {
250
251
                if (true === $is_valid_visibility_nonce ) {
252
                    update_option('subway_public_post', $posts_implode);
253
                    update_post_meta(
254
                        $post_id,
255
                        self::VISIBILITY_METAKEY,
256
                        $post_visibility
257
                    );
258
                }
259
260
            }
261
        }
262
    }
263
264
    /**
265
     * This method runs the methods that handles the update for a post_meta.
266
     *
267
     * @param integer $post_id Contains ID of the current post.
268
     *
269
     * @since  2.0.9
270
     * @access public
271
     * @return boolean false Returns false if nonce is not valid.
272
     */
273
    public function saveMetaboxValues( $post_id )
274
    {
275
        $this->saveVisibilityMetabox($post_id);
276
        return;
277
    }
278
279
    /**
280
     * Initialize metabox arguments.
281
     *
282
     * @param array  $args   The arguments for the get_post_types().
283
     * @param string $output Your desired output for the data.
284
     *
285
     * @since  2.0.9
286
     * @access public
287
     * @return void
288
     */
289
    public static function getPostTypes( $args = '', $output = '' )
290
    {
291
        if (empty($args) ) {
292
            $args = array(
293
            'public'   => true,
294
            );
295
            $output = 'names';
296
        }
297
298
        $post_types = get_post_types($args, $output);
299
300
        return $post_types;
301
    }
302
303
    /**
304
     * This method verify if nonce is valid.
305
     *
306
     * @param mixed $nonce the name of a metabox nonce.
307
     *
308
     * @since  2.0.9
309
     * @access public
310
     * @return boolean true Returns true if nonce is valid.
311
     */
312
    public function isNonceValid( $nonce )
313
    {
314
        if (!isset($nonce) || !wp_verify_nonce($nonce, basename(__FILE__)) ) {
315
            return;
316
        }
317
318
        return true;
319
    }
320
321
    /**
322
     * Checks if a post is set to private.
323
     *
324
     * @param integer $post_id Contains ID of the current post.
325
     *
326
     * @since  2.0.9
327
     * @access public
328
     * @return boolean true Returns true if post is private. Otherwise false.
329
     */
330
    public static function isPostPrivate( $post_id )
331
    {
332
        $meta_value = '';
0 ignored issues
show
Unused Code introduced by
$meta_value is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
333
334
        if (! empty($post_id) ) {
335
            $meta_value = get_post_meta($post_id, self::VISIBILITY_METAKEY, true);
336
            if ('private' === $meta_value ) {
337
                return true;
338
            }
339
        }
340
341
        return false;
342
    }
343
344
    /**
345
     * Checks if a post is set to public.
346
     *
347
     * @param integer $post_id Contains ID of the current post.
348
     *
349
     * @since  2.0.9
350
     * @access public
351
     * @return boolean true Returns true if post is public. Otherwise false.
352
     */
353
    public static function isPostPublic( $post_id )
354
    {
355
        $public_post = Options::getPublicPostsIdentifiers();
356
357
        if (! empty($post_id) ) {
358
            if (! in_array($post_id, $public_post, true) ) {
359
                return true;
360
            }
361
        }
362
363
        return false;
364
    }
365
366
}
367