Passed
Push — master ( 54f7d9...dbb97a )
by Joseph
20s
created

Metabox::addMetabox()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 15
rs 9.7666
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
            'Choose the accessibility of this page from the options above.',
120
            'subway'
121
        );
122
      
123
        $private_setting_label = __( 'Members Only', 'subway'  );
0 ignored issues
show
Unused Code introduced by
$private_setting_label 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...
124
125
        $is_post_public          = self::isPostPublic($post->ID);
0 ignored issues
show
Unused Code introduced by
$is_post_public 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...
126
        $is_post_private         = self::isPostPrivate($post->ID);
127
128
        // Make sure the form request comes from WordPress
129
        wp_nonce_field(  basename(__FILE__),  'subway_post_visibility_nonce' );
130
131
        // Disable the options (radio) when site is selected as public
132
        ?>
133
        
134
        <?php if ( ! Options::isPublicSite() ):  ?>
135
        <?php // Site is private. Give them some Beer! ?>
136
            <p>
137
                <label class="subway-visibility-settings-checkbox-label" for="subway-visibility-public">
138
                    <input type="radio" class="subway-visibility-settings-radio" id="subway-visibility-public" name="subway-visibility-settings" value="public" <?php echo checked( false, $is_post_private, false); ?>>
139
                    <?php esc_html_e( 'Public','subway') ?>
140
                </label>
141
            </p>
142
            <p>
143
                <label class="subway-visibility-settings-checkbox-label" for="subway-visibility-private">
144
                    <input type="radio" class="subway-visibility-settings-radio" id="subway-visibility-private" name="subway-visibility-settings"
145
                    value="private" <?php echo checked( true, $is_post_private, false); ?>>
146
                    <?php esc_html_e( 'Members Only','subway') ?>
147
                 </label>
148
            </p>
149
            <p class="howto"><?php echo esc_html($howto); ?></p>
150
        <?php else: ?>
151
            <?php // Site is public! Explain to them ?>
152
            <p><em>
153
                <?php esc_html_e('You have chosen to make your site public inside Settings > Subway. Make your site private so that you can select visibility options.', 'subway'); ?>
154
            </em>
155
            </p>
156
        <?php endif; ?>
157
        <?php
158
    }
159
160
    /**
161
     * This method verify if nonce is valid then updates a post_meta.
162
     *
163
     * @param integer $post_id Contains ID of the current post.
164
     *
165
     * @since  2.0.9
166
     * @access public
167
     * @return boolean false Returns false if nonce is not valid.
168
     */
169
    public function saveVisibilityMetabox( $post_id = '' )
170
    {
171
172
        $public_posts     = Options::getPublicPostsIdentifiers();
173
174
        $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...
175
176
        $visibility_field = 'subway-visibility-settings';
177
178
        $visibility_nonce = filter_input(
179
            INPUT_POST,
180
            'subway_post_visibility_nonce',
181
            FILTER_SANITIZE_STRING
182
        );
183
184
        $post_visibility = filter_input(
185
            INPUT_POST,
186
            $visibility_field,
187
            FILTER_SANITIZE_STRING
188
        );
189
190
        $is_valid_visibility_nonce = self::isNonceValid(
191
            $visibility_nonce
192
        );
193
194
        // verify taxonomies meta box nonce
195
        if (false === $is_valid_visibility_nonce ) {
196
            return;
197
        }
198
199
        if (! empty($post_visibility) ) {
200
            if (! empty($post_id) ) {
201
                if ('public' === $post_visibility ) {
202
                    if (! in_array($post_id, $public_posts) ) {
203
                        array_push($public_posts, $post_id);
204
                    }
205
                }
206
                if ('private' === $post_visibility ) {
207
                    unset($public_posts[ array_search($post_id, $public_posts) ]);
208
                }
209
            }
210
        }
211
212
        if (! empty($post_id) ) {
213
            $posts_implode = implode(", ", $public_posts);
214
215
            if ('inherit' !== get_post_status($post_id) ) {
216
217
                if (true === $is_valid_visibility_nonce ) {
218
                    update_option('subway_public_post', $posts_implode);
219
                    update_post_meta(
220
                        $post_id,
221
                        self::VISIBILITY_METAKEY,
222
                        $post_visibility
223
                    );
224
                }
225
226
            }
227
        }
228
    }
229
230
    /**
231
     * This method runs the methods that handles the update for a post_meta.
232
     *
233
     * @param integer $post_id Contains ID of the current post.
234
     *
235
     * @since  2.0.9
236
     * @access public
237
     * @return boolean false Returns false if nonce is not valid.
238
     */
239
    public function saveMetaboxValues( $post_id )
240
    {
241
        $this->saveVisibilityMetabox($post_id);
242
        return;
243
    }
244
245
    /**
246
     * Initialize metabox arguments.
247
     *
248
     * @param array  $args   The arguments for the get_post_types().
249
     * @param string $output Your desired output for the data.
250
     *
251
     * @since  2.0.9
252
     * @access public
253
     * @return void
254
     */
255
    public static function getPostTypes( $args = '', $output = '' )
256
    {
257
        if (empty($args) ) {
258
            $args = array(
259
            'public'   => true,
260
            );
261
            $output = 'names';
262
        }
263
264
        $post_types = get_post_types($args, $output);
265
266
        return $post_types;
267
    }
268
269
    /**
270
     * This method verify if nonce is valid.
271
     *
272
     * @param mixed $nonce the name of a metabox nonce.
273
     *
274
     * @since  2.0.9
275
     * @access public
276
     * @return boolean true Returns true if nonce is valid.
277
     */
278
    public function isNonceValid( $nonce )
279
    {
280
        if (!isset($nonce) || !wp_verify_nonce($nonce, basename(__FILE__)) ) {
281
            return;
282
        }
283
284
        return true;
285
    }
286
287
    /**
288
     * Checks if a post is set to private.
289
     *
290
     * @param integer $post_id Contains ID of the current post.
291
     *
292
     * @since  2.0.9
293
     * @access public
294
     * @return boolean true Returns true if post is private. Otherwise false.
295
     */
296
    public static function isPostPrivate( $post_id )
297
    {
298
        $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...
299
300
        if ( ! empty( $post_id ) ) {
301
            $meta_value = get_post_meta($post_id, self::VISIBILITY_METAKEY, true);
302
303
            // New page or old pages that don't have Subway'\ Visibility Options
304
            if ( empty ( $meta_value ) ) {
305
                // Get the value from the general settings (Settings > Subway)
306
                $is_site_public = Options::isPublicSite();
307
                if ( ! $is_site_public ) {
308
                    // It's private.
309
                    return true;
310
                }
311
            }
312
            if ('private' === $meta_value ) {
313
                return true;
314
            }
315
        }
316
317
        return false;
318
    }
319
320
    /**
321
     * Checks if a post is set to public.
322
     *
323
     * @param integer $post_id Contains ID of the current post.
324
     *
325
     * @since  2.0.9
326
     * @access public
327
     * @return boolean true Returns true if post is public. Otherwise false.
328
     */
329
    public static function isPostPublic( $post_id )
330
    {
331
        $public_post = Options::getPublicPostsIdentifiers();
332
333
        if (! empty($post_id) ) {
334
            if (! in_array($post_id, $public_post, true) ) {
335
                return true;
336
            }
337
        }
338
339
        return false;
340
    }
341
342
}
343