GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — feature/attachment-taxonomies ( 9fe273...54ac97 )
by Brad
06:02 queued 02:51
created

FooGallery_Pro_Bulk_Copy::ajax_bulk_copy_start()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
/**
3
 * FooGallery Pro Bulk Copy Class
4
 */
5
if ( ! class_exists( 'FooGallery_Pro_Bulk_Copy' ) ) {
6
7
    class FooGallery_Pro_Bulk_Copy
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
    {
9
        function __construct()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
10
        {
11
            //add the bulk copy metabox
12
            add_action( 'add_meta_boxes_' . FOOGALLERY_CPT_GALLERY, array( $this, 'add_bulk_copy_meta_box_to_gallery' ) );
13
14
            // Ajax call for starting the bulk copy
15
            add_action( 'wp_ajax_foogallery_bulk_copy_start', array( $this, 'ajax_bulk_copy_start' ) );
16
17
            // Ajax call for running the bulk copy
18
            add_action( 'wp_ajax_foogallery_bulk_copy_run', array( $this, 'ajax_bulk_copy_run' ) );
19
        }
20
21
        public function ajax_bulk_copy_run()
0 ignored issues
show
Coding Style introduced by
ajax_bulk_copy_run uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
22
        {
23
            if (check_admin_referer('foogallery_bulk_copy_run', 'foogallery_bulk_copy_run_nonce')) {
24
                $data = $_POST['foogallery_bulk_copy'];
25
                $foogallery_id = $data['foogallery'];
26
                $errors = $settings = $galleries = array();
27
                $destination = false;
28
                if ( !array_key_exists( 'settings', $data ) ) {
29
                    $errors['settings'] = __('You need to decide which settings you want to copy across.', 'foogallery');
30
                } else {
31
                    $settings = $data['settings'];
32
                }
33
34
                if ( !isset( $data['destination'] ) ) {
35
                    $errors['destination'] = __('You need to choose the destination you want to copy to.', 'foogallery');
36
                } else {
37
                    $destination = $data['destination'];
38
                }
39
40
                if ( 'custom' === $destination ) {
41
                    if (!isset($data['gallery'])) {
42
                        $errors['gallery'] = __('You need to select which galleries you want to copy to.', 'foogallery');
43
                    } else {
44
                        $galleries = $data['gallery'];
45
                    }
46
                }
47
48
                if ( count($errors) ) {
49
                    $this->output_bulk_copy_form( $foogallery_id, $settings, $destination, $galleries, $errors );
50
                } else {
51
                    //if we get here, then we can perform a bulk copy!
52
                    $result = $this->run_bulk_copy( $foogallery_id, $settings, $destination, $galleries );
53
                    ?>
54
                    <div class="foogallery_bulk_copy_result"><?php echo $result; ?></div>
55
                    <button class="button button-primary button-large" id="foogallery_bulk_copy_start"><?php _e( 'Start Another Bulk Copy', 'foogallery' ); ?></button>
56
                    <?php wp_nonce_field( 'foogallery_bulk_copy_start', 'foogallery_bulk_copy_start_nonce', false ); ?>
57
                    <span class="foogallery_bulk_copy_spinner spinner"></span><?php
58
                }
59
            }
60
61
            die();
62
        }
63
64
        private function run_bulk_copy($foogallery_id, $settings, $destination = false, $galleries = array()) {
65
66
            if ( false === $settings ) {
67
                return __('No settings were selected to be copied!', 'foogallery');
68
            }
69
70
            $args = array(
71
                'post_type'     => FOOGALLERY_CPT_GALLERY,
72
                'post_status'	=> array( 'publish', 'draft' ),
73
                'cache_results' => false,
74
                'nopaging'      => true,
75
            );
76
77
            if ( 'all' === $destination ) {
78
                $exclude[] = intval($foogallery_id);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$exclude was never initialized. Although not strictly required by PHP, it is generally a good practice to add $exclude = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
79
                $args['post__not_in'] = $exclude;
80
            } else {
81
                $args['post__in'] = $galleries;
82
            }
83
84
            $gallery_posts = get_posts( $args );
85
86
            $source_gallery_template = get_post_meta( $foogallery_id, FOOGALLERY_META_TEMPLATE, true );
87
            $source_settings = get_post_meta( $foogallery_id, FOOGALLERY_META_SETTINGS, true );
88
            $source_sorting = get_post_meta( $foogallery_id, FOOGALLERY_META_SORT, true );
89
            $source_retina = get_post_meta( $foogallery_id, FOOGALLERY_META_RETINA, true );
90
            $source_custom_css = get_post_meta( $foogallery_id, FOOGALLERY_META_CUSTOM_CSS, true );
91
92
            $count = 0;
93
            foreach ( $gallery_posts as $post ) {
94
95
                //override the post meta for the gallery
96
                if ( array_key_exists(FOOGALLERY_META_SETTINGS, $settings) ) {
97
                    update_post_meta( $post->ID, FOOGALLERY_META_TEMPLATE, $source_gallery_template );
98
                    update_post_meta( $post->ID, FOOGALLERY_META_SETTINGS, $source_settings );
99
                }
100
101
                if ( array_key_exists(FOOGALLERY_META_SORT, $settings) ) {
102
                    update_post_meta( $post->ID, FOOGALLERY_META_SORT, $source_sorting );
103
                }
104
105
                if ( array_key_exists(FOOGALLERY_META_RETINA, $settings) ) {
106
                    update_post_meta( $post->ID, FOOGALLERY_META_RETINA, $source_retina );
107
                }
108
109
                if ( array_key_exists(FOOGALLERY_META_CUSTOM_CSS, $settings) ) {
110
                    update_post_meta( $post->ID, FOOGALLERY_META_CUSTOM_CSS, $source_custom_css );
111
                }
112
113
                $count++;
114
            }
115
116
            return sprintf( __( 'Successfully copied settings to %d galleries.', 'foogallery' ), $count );
117
        }
118
119
        public function output_bulk_copy_form($foogallery_id, $settings = false, $destination = false, $galleries = array(), $errors = array()) {
120
            //check if we need to set some defaults
121
            if ( false === $settings ) {
122
                $settings[FOOGALLERY_META_SETTINGS] = FOOGALLERY_META_SETTINGS;
123
                $settings[FOOGALLERY_META_RETINA] = FOOGALLERY_META_RETINA;
124
                $settings[FOOGALLERY_META_SORT] = FOOGALLERY_META_SORT;
125
                $settings[FOOGALLERY_META_CUSTOM_CSS] = FOOGALLERY_META_CUSTOM_CSS;
126
            }
127
            $exclude[] = intval($foogallery_id);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$exclude was never initialized. Although not strictly required by PHP, it is generally a good practice to add $exclude = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
128
            ?>
129
            <div class="foogallery-bulk-copy-settings">
130
                <table class="foogallery-metabox-settings">
131
                    <tr class="foogallery_template_field">
132
                        <th>
133
                            <label for="FooGallerySettings_default_lightbox"><?php _e('Which Settings?', 'foogallery'); ?></label>
134
                            <span data-balloon-length="large" data-balloon-pos="right" data-balloon="Choose which settings you wish to bulk copy to other galleries."><i class="dashicons dashicons-editor-help"></i></span>
135
                        </th>
136
                        <td>
137
                            <?php if ( array_key_exists( 'settings', $errors ) ) {?>
138
                                <div class="foogallery_bulk_copy_error"><?php echo $errors['settings']; ?></div>
139
                            <?php } ?>
140
                            <div class="foogallery_metabox_field-radio">
141
                                <input <?php echo (array_key_exists(FOOGALLERY_META_SETTINGS, $settings) ? 'checked="checked"' : ''); ?> type="checkbox" name="foogallery_bulk_copy[settings][<?php echo FOOGALLERY_META_SETTINGS; ?>]" id="FooGalleryBulkCopy_Settings_Template" value="<?php echo FOOGALLERY_META_SETTINGS; ?>">
142
                                <label for="FooGalleryBulkCopy_Settings_Template"><?php _e('Gallery Template & Settings', 'foogallery'); ?></label><br>
143
144
                                <input <?php echo (array_key_exists(FOOGALLERY_META_RETINA, $settings) ? 'checked="checked"' : ''); ?> type="checkbox" name="foogallery_bulk_copy[settings][<?php echo FOOGALLERY_META_RETINA; ?>]" id="FooGalleryBulkCopy_Settings_Retina" value="<?php echo FOOGALLERY_META_RETINA; ?>">
145
                                <label for="FooGalleryBulkCopy_Settings_Retina"><?php _e('Retina Settings', 'foogallery'); ?></label><br>
146
147
                                <input <?php echo (array_key_exists(FOOGALLERY_META_SORT, $settings) ? 'checked="checked"' : ''); ?> type="checkbox" name="foogallery_bulk_copy[settings][<?php echo FOOGALLERY_META_SORT; ?>]" id="FooGalleryBulkCopy_Settings_Sorting" value="<?php echo FOOGALLERY_META_SORT; ?>">
148
                                <label for="FooGalleryBulkCopy_Settings_Sorting"><?php _e('Sorting Settings', 'foogallery'); ?></label><br>
149
150
                                <input <?php echo (array_key_exists(FOOGALLERY_META_CUSTOM_CSS, $settings) ? 'checked="checked"' : ''); ?> type="checkbox" name="foogallery_bulk_copy[settings][<?php echo FOOGALLERY_META_CUSTOM_CSS; ?>]" id="FooGalleryBulkCopy_Settings_CustomCSS" value="<?php echo FOOGALLERY_META_CUSTOM_CSS; ?>">
151
                                <label for="FooGalleryBulkCopy_Settings_CustomCSS"><?php _e('Custom CSS', 'foogallery'); ?></label>
152
                            </div>
153
                        </td>
154
                    </tr>
155
                    <tr class="foogallery_template_field">
156
                        <th>
157
                            <label for="FooGallerySettings_default_lightbox"><?php _e('Destination', 'foogallery'); ?></label>
158
                            <span data-balloon-length="large" data-balloon-pos="right" data-balloon="Choose which galleries you want to copy the settings to."><i class="dashicons dashicons-editor-help"></i></span>
159
                        </th>
160
                        <td>
161
                            <?php if ( array_key_exists( 'destination', $errors ) ) {?>
162
                                <div class="foogallery_bulk_copy_error"><?php echo $errors['destination']; ?></div>
163
                            <?php } ?>
164
                            <div class="foogallery_metabox_field-radio">
165
                                <input <?php echo ('all' === $destination ? 'checked="checked"' : ''); ?> type="radio" name="foogallery_bulk_copy[destination]" id="FooGalleryBulkCopy_Destination_All" value="all">
166
                                <label for="FooGalleryBulkCopy_Destination_All"><?php _e('All Galleries', 'foogallery'); ?></label><br>
167
168
                                <input <?php echo ('custom' === $destination ? 'checked="checked"' : ''); ?> type="radio" name="foogallery_bulk_copy[destination]" id="FooGalleryBulkCopy_Destination_Custom" value="custom">
169
                                <label for="FooGalleryBulkCopy_Destination_Custom"><?php _e('Custom Selection', 'foogallery'); ?></label><br>
170
                            </div>
171
                        </td>
172
                    </tr>
173
                    <tr class="foogallery_template_field">
174
                        <th>
175
                            <label for="FooGallerySettings_default_lightbox"><?php _e('Select the galleries', 'foogallery'); ?></label>
176
                        </th>
177
                        <td>
178
                            <?php if ( array_key_exists( 'gallery', $errors ) ) {?><div class="foogallery_bulk_copy_error"><?php echo $errors['gallery']; ?></div><?php } ?>
179
                            <div class="foogallery_metabox_field-radio bulk_copy_custom">
180
                                <?php foreach ( foogallery_get_all_galleries($exclude) as $foogallery ) {
0 ignored issues
show
Documentation introduced by
$exclude is of type array<integer,integer>, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
181
                                    $checked = array_key_exists( $foogallery->ID, $galleries ) ? 'checked="checked"' : '';
182
                                    ?><div class="foogallery_bulk_copy_custom_selection">
183
                                    <input <?php echo $checked; ?> type="checkbox" name="foogallery_bulk_copy[gallery][<?php echo $foogallery->ID; ?>]" id="FooGalleryBulkCopy_Custom_<?php echo $foogallery->ID; ?>" value="<?php echo $foogallery->ID; ?>">
184
                                    <label for="FooGalleryBulkCopy_Custom_<?php echo $foogallery->ID; ?>"><?php echo $foogallery->name . ' [' . $foogallery->ID . ']'; ?></label>
185
                                    </div>
186
                                <?php } ?>
187
                            </div>
188
                        </td>
189
                    </tr>
190
                </table>
191
                <p>
192
                    <input type="hidden" name="foogallery_bulk_copy[foogallery]" value="<?php echo $foogallery_id; ?>" />
193
                    <button class="button button-primary button-large" id="foogallery_bulk_copy_run"><?php _e( 'Run Bulk Copy', 'foogallery' ); ?></button>
194
                    <span class="foogallery_bulk_copy_spinner spinner"></span>
195
                    <?php if ( count ($errors) > 0 ) { ?>
196
                        <br />
197
                        <div class="foogallery_bulk_copy_error"><?php _e('The bulk copy could not be run, due to errors. Please see above and correct the errors before continuing.', 'foogallery'); ?></div>
198
                    <?php } ?>
199
                    <?php wp_nonce_field( 'foogallery_bulk_copy_run', 'foogallery_bulk_copy_run_nonce', false ); ?>
200
                </p>
201
            </div>
202
            <?php
203
        }
204
205
        public function ajax_bulk_copy_start()
0 ignored issues
show
Coding Style introduced by
ajax_bulk_copy_start uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
206
        {
207
            if (check_admin_referer('foogallery_bulk_copy_start', 'foogallery_bulk_copy_start_nonce')) {
208
                $foogallery_id = $_POST['foogallery'];
209
                $this->output_bulk_copy_form( $foogallery_id );
210
            }
211
212
            die();
213
        }
214
215
        /**
216
         * Add a metabox to the gallery for bulk copying
217
         * @param $post
218
         */
219
        function add_bulk_copy_meta_box_to_gallery($post) {
0 ignored issues
show
Unused Code introduced by
The parameter $post 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...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
220
            add_meta_box(
221
                'foogallery_bulk_copy',
222
                __( 'Bulk Copy', 'foogallery' ),
223
                array( $this, 'render_gallery_bulk_copy_metabox' ),
224
                FOOGALLERY_CPT_GALLERY,
225
                'normal',
226
                'low'
227
            );
228
        }
229
230
        /**
231
         * Render the bulk copy metabox on the gallery edit page
232
         * @param $post
233
         */
234
        function render_gallery_bulk_copy_metabox( $post ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
235
            ?>
236
            <style type="text/css">
237
                .foogallery_bulk_copy_custom_selection {
238
                    display: inline-block;
239
                    width: 40%;
240
                    margin-bottom:5px;
241
                }
242
243
                .foogallery_bulk_copy_custom_selection label {
244
                    vertical-align: top;
245
                }
246
247
                .foogallery_bulk_copy_error {
248
                    position: relative;
249
                    line-height: 16px;
250
                    padding: 6px 5px;
251
                    font-size: 14px;
252
                    text-align: left;
253
                    margin-bottom: 5px;
254
                    background-color: #FFe4e4;
255
                    border-left: 4px solid #be392f;
256
                    -webkit-box-shadow: 0 1px 1px 0 rgba(0, 0, 0, .1);
257
                    box-shadow: 0 1px 1px 0 rgba(0, 0, 0, .1);
258
                }
259
260
                .foogallery_bulk_copy_spinner {
261
                    float: none !important;
262
                }
263
264
                .foogallery_bulk_copy_result {
265
                    position: relative;
266
                    line-height: 16px;
267
                    padding: 6px 5px;
268
                    font-size: 14px;
269
                    text-align: left;
270
                    margin-top: 5px;
271
                    margin-bottom: 5px;
272
                    background-color: #e4ffe4;
273
                    border-left: 4px solid #1bbe33;
274
                    -webkit-box-shadow: 0 1px 1px 0 rgba(0, 0, 0, .1);
275
                    box-shadow: 0 1px 1px 0 rgba(0, 0, 0, .1);
276
                }
277
            </style>
278
            <script type="text/javascript">
279
                jQuery(function ($) {
280
                    $('#foogallery_bulk_copy_container').on('click', '#foogallery_bulk_copy_start', function(e) {
281
                        e.preventDefault();
282
283
                        $('.foogallery_bulk_copy_spinner').addClass('is-active');
284
                        var data = 'action=foogallery_bulk_copy_start' +
285
                            '&foogallery=<?php echo $post->ID; ?>' +
286
                            '&foogallery_bulk_copy_start_nonce=' + $('#foogallery_bulk_copy_start_nonce').val() +
287
                            '&_wp_http_referer=' + encodeURIComponent($('input[name="_wp_http_referer"]').val());
288
289
                        $.ajax({
290
                            type: "POST",
291
                            url: ajaxurl,
292
                            data: data,
293
                            success: function(data) {
294
                                $('#foogallery_bulk_copy_container').html(data);
295
                                $('.foogallery_bulk_copy_spinner').removeClass('is-active');
296
                            }
297
                        });
298
                    });
299
300
                    $('#foogallery_bulk_copy_container').on('click', '#foogallery_bulk_copy_run', function(e) {
301
                        e.preventDefault();
302
303
                        $('.foogallery_bulk_copy_spinner').addClass('is-active');
304
                        var data = $('.foogallery-bulk-copy-settings').find('select, textarea, input').serialize() +
305
                            '&action=foogallery_bulk_copy_run' +
306
                            '&foogallery_bulk_copy_run_nonce=' + $('#foogallery_bulk_copy_run_nonce').val() +
307
                            '&_wp_http_referer=' + encodeURIComponent($('input[name="_wp_http_referer"]').val());
308
309
                        $.ajax({
310
                            type: "POST",
311
                            url: ajaxurl,
312
                            data: data,
313
                            success: function(data) {
314
                                $('#foogallery_bulk_copy_container').html(data);
315
                                $('.foogallery_bulk_copy_spinner').removeClass('is-active');
316
                            }
317
                        });
318
                    });
319
                });
320
            </script>
321
            <div>
322
                <p class="foogallery-help"><?php _e('You can bulk copy the settings from this gallery to other galleries in a few easy steps. To get started, click the button below. Please be sure to save your gallery before you start the copy, as only the saved settings stored in the database will be copied across.', 'foogallery'); ?></p>
323
            </div>
324
            <br/>
325
            <div id="foogallery_bulk_copy_container">
326
                <button class="button button-primary button-large" id="foogallery_bulk_copy_start"><?php _e( 'Start Bulk Copy', 'foogallery' ); ?></button>
327
                <?php wp_nonce_field( 'foogallery_bulk_copy_start', 'foogallery_bulk_copy_start_nonce', false ); ?>
328
                <span class="foogallery_bulk_copy_spinner spinner"></span>
329
            </div>
330
            <?php
331
        }
332
    }
333
}