| @@ -1,7 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | + | |
| 2 | 3 | namespace Photonic_Plugin\Admin; |
| 3 | 4 | |
| 5 | +if (!current_user_can('edit_posts')) { | |
| 6 | + wp_die(esc_html__('You are not authorized to use this capability.', 'photonic')); | |
| 7 | +} | |
| 8 | + | |
| 4 | 9 | use WP_List_Table; |
| 5 | 10 | |
| 6 | 11 | /** |
| 7 | 12 | * Generates a table showing the usage of the <code>gallery</code> shortcode for Photonic. |
| @@ -10,23 +15,25 @@ | ||
| 10 | 15 | * |
| 11 | 16 | * @since 2.10 |
| 12 | 17 | */ |
| 13 | 18 | |
| 14 | -if(!class_exists('WP_List_Table')){ | |
| 15 | - require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' ); | |
| 19 | +if (!class_exists('WP_List_Table')) { | |
| 20 | + require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; | |
| 16 | 21 | } |
| 17 | 22 | |
| 18 | 23 | class Shortcode_Usage extends WP_List_Table { |
| 19 | 24 | public $items = []; |
| 20 | - public $tag; | |
| 21 | - var $per_page = 100; | |
| 25 | + public string $tag; | |
| 26 | + private int $per_page = 100; | |
| 22 | 27 | |
| 23 | - public function __construct($args = []) { | |
| 24 | - parent::__construct([ | |
| 25 | - 'singular' => 'post', | |
| 26 | - 'plural' => 'posts', | |
| 27 | - 'ajax' => false, | |
| 28 | - ]); | |
| 28 | + public function __construct($args = []) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter | |
| 29 | + parent::__construct( | |
| 30 | + [ | |
| 31 | + 'singular' => 'post', | |
| 32 | + 'plural' => 'posts', | |
| 33 | + 'ajax' => false, | |
| 34 | + ] | |
| 35 | + ); | |
| 29 | 36 | $this->tag = 'gallery'; |
| 30 | 37 | add_filter('removable_query_args', [&$this, 'remove_args']); |
| 31 | 38 | } |
| 32 | 39 | |
| @@ -34,14 +41,14 @@ | ||
| 34 | 41 | * List of columns displayed in the table |
| 35 | 42 | * |
| 36 | 43 | * @return array |
| 37 | 44 | */ |
| 38 | - public function get_columns() { | |
| 45 | + public function get_columns(): array { | |
| 39 | 46 | return [ |
| 40 | - 'cb' => '<input type="checkbox" />', | |
| 41 | - 'title' => esc_html__('Post Title', 'photonic'), | |
| 42 | - 'type' => esc_html__('Post Type', 'photonic'), | |
| 43 | - 'status' => esc_html__('Post Status', 'photonic'), | |
| 47 | + 'cb' => '<input type="checkbox" />', | |
| 48 | + 'title' => esc_html__('Post Title', 'photonic'), | |
| 49 | + 'type' => esc_html__('Post Type', 'photonic'), | |
| 50 | + 'status' => esc_html__('Post Status', 'photonic'), | |
| 44 | 51 | 'shortcodes' => esc_html__('Gallery Shortcodes used by Photonic', 'photonic'), |
| 45 | 52 | ]; |
| 46 | 53 | } |
| 47 | 54 | |
| @@ -49,12 +56,12 @@ | ||
| 49 | 56 | * List of columns sortable by the user |
| 50 | 57 | * |
| 51 | 58 | * @return array |
| 52 | 59 | */ |
| 53 | - public function get_sortable_columns() { | |
| 60 | + public function get_sortable_columns(): array { | |
| 54 | 61 | return [ |
| 55 | - 'type' => ['type', true], | |
| 56 | - 'title' => ['title', true], | |
| 62 | + 'type' => ['type', true], | |
| 63 | + 'title' => ['title', true], | |
| 57 | 64 | 'status' => ['status', false], |
| 58 | 65 | ]; |
| 59 | 66 | } |
| 60 | 67 | |
| @@ -86,13 +93,13 @@ | ||
| 86 | 93 | preg_match_all('/' . $pattern . '/s', $post['post_content'], $matches, PREG_OFFSET_CAPTURE); |
| 87 | 94 | if (!empty($matches) && !empty($matches[0]) && !empty($matches[1]) && !empty($matches[2]) && !empty($matches[3])) { |
| 88 | 95 | $to_change = []; |
| 89 | 96 | foreach ($matches[1] as $instance => $start) { |
| 90 | - if ($start[0] === '') { | |
| 97 | + if ('' === $start[0]) { | |
| 91 | 98 | if (!empty($matches[3][$instance])) { |
| 92 | 99 | $attributes = shortcode_parse_atts($matches[3][$instance][0]); |
| 93 | - if ((!empty($attributes['type']) && in_array($attributes['type'], $types)) || | |
| 94 | - (empty($attributes['type']) && !empty($attributes['style']) && in_array($attributes['style'], $layouts))) { | |
| 100 | + if ((!empty($attributes['type']) && in_array($attributes['type'], $types, true)) || | |
| 101 | + (empty($attributes['type']) && !empty($attributes['style']) && in_array($attributes['style'], $layouts, true))) { | |
| 95 | 102 | $to_change[] = "<code>" . esc_html($matches[0][$instance][0]) . "</code>"; |
| 96 | 103 | } |
| 97 | 104 | } |
| 98 | 105 | } |
| @@ -98,12 +105,12 @@ | ||
| 98 | 105 | } |
| 99 | 106 | } |
| 100 | 107 | if (!empty($to_change)) { |
| 101 | 108 | $data[] = [ |
| 102 | - 'id' => $post['ID'], | |
| 103 | - 'type' => $post['post_type'], | |
| 104 | - 'status' => $post['post_status'], | |
| 105 | - 'title' => $post['post_title'], | |
| 109 | + 'id' => $post['ID'], | |
| 110 | + 'type' => $post['post_type'], | |
| 111 | + 'status' => $post['post_status'], | |
| 112 | + 'title' => $post['post_title'], | |
| 106 | 113 | 'shortcodes' => $to_change, |
| 107 | 114 | ]; |
| 108 | 115 | } |
| 109 | 116 | } |
| @@ -109,15 +116,17 @@ | ||
| 109 | 116 | } |
| 110 | 117 | } |
| 111 | 118 | $current_page = $this->get_pagenum(); |
| 112 | 119 | $total_items = count($data); |
| 113 | - $data = array_slice($data, (($current_page-1) * $this->per_page), $this->per_page); | |
| 120 | + $data = array_slice($data, (($current_page - 1) * $this->per_page), $this->per_page); | |
| 114 | 121 | $this->items = $data; |
| 115 | - $this->set_pagination_args([ | |
| 116 | - 'total_items' => $total_items, | |
| 117 | - 'per_page' => $this->per_page, | |
| 118 | - 'total_pages' => ceil($total_items/$this->per_page), | |
| 119 | - ]); | |
| 122 | + $this->set_pagination_args( | |
| 123 | + [ | |
| 124 | + 'total_items' => $total_items, | |
| 125 | + 'per_page' => $this->per_page, | |
| 126 | + 'total_pages' => ceil($total_items / $this->per_page), | |
| 127 | + ] | |
| 128 | + ); | |
| 120 | 129 | } |
| 121 | 130 | |
| 122 | 131 | /** |
| 123 | 132 | * Default output for a column, if a column-specific output is not defined |
| @@ -125,9 +134,9 @@ | ||
| 125 | 134 | * @param object $item |
| 126 | 135 | * @param string $column_name |
| 127 | 136 | * @return null|string |
| 128 | 137 | */ |
| 129 | - protected function column_default($item, $column_name) { | |
| 138 | + protected function column_default($item, $column_name): ?string { | |
| 130 | 139 | return isset($item[$column_name]) ? esc_html($item[$column_name]) : null; |
| 131 | 140 | } |
| 132 | 141 | |
| 133 | 142 | /** |
| @@ -135,50 +144,43 @@ | ||
| 135 | 144 | * |
| 136 | 145 | * @param object $item |
| 137 | 146 | * @return string |
| 138 | 147 | */ |
| 139 | - protected function column_cb($item) { | |
| 148 | + protected function column_cb($item): string { | |
| 140 | 149 | return sprintf('<input type="checkbox" name="photonic_post[]" value="%s" />', $item['id']); |
| 141 | 150 | } |
| 142 | 151 | |
| 143 | - protected function column_title($item) { | |
| 152 | + protected function column_title($item): string { | |
| 144 | 153 | $actions = [ |
| 145 | - 'edit' => '<a href="' . get_edit_post_link($item['id']) . '">' . esc_html__('Edit', 'photonic') . '</a>', | |
| 146 | - 'view' => '<a href="' . get_permalink($item['id']) . '">' . esc_html__('View', 'photonic') . '</a>', | |
| 147 | - 'replace_shortcode_individual' => '<a href="'.admin_url('admin.php?page=photonic-gutenberg&action=replace_shortcode_individual&photonic_post_id='.$item['id']).'" class="photonic-shortcode-replace">' . esc_html__('Replace Shortcodes', 'photonic') . '</a>', | |
| 154 | + 'edit' => '<a href="' . get_edit_post_link($item['id']) . '">' . esc_html__('Edit', 'photonic') . '</a>', | |
| 155 | + 'view' => '<a href="' . get_permalink($item['id']) . '">' . esc_html__('View', 'photonic') . '</a>', | |
| 156 | + 'replace_shortcode_individual' => '<a href="' . admin_url('admin.php?page=photonic-shortcode-replace&action=replace_shortcode_individual&photonic_post_id=' . $item['id']) . '" class="photonic-shortcode-replace">' . esc_html__('Replace Shortcodes', 'photonic') . '</a>', | |
| 148 | 157 | ]; |
| 158 | + | |
| 149 | 159 | return $item['title'] . $this->row_actions($actions); |
| 150 | 160 | } |
| 151 | 161 | |
| 152 | - protected function column_shortcodes($item) { | |
| 162 | + protected function column_shortcodes($item): string { | |
| 153 | 163 | return implode("<br/>\n", $item['shortcodes']); |
| 154 | 164 | } |
| 155 | 165 | |
| 156 | 166 | public function no_items() { |
| 157 | - echo sprintf(esc_html__('No instances of Photonic found with the %s shortcode', 'photonic'), "<code>{$this->tag}</code>"); | |
| 167 | + echo sprintf(esc_html__('No instances of Photonic found with the %s shortcode', 'photonic'), "<code>" . esc_html($this->tag) . "</code>"); | |
| 158 | 168 | } |
| 159 | 169 | |
| 160 | - function get_bulk_actions() { | |
| 161 | - $actions = [ | |
| 170 | + protected function get_bulk_actions(): array { | |
| 171 | + return [ | |
| 162 | 172 | 'replace_shortcode' => esc_html__('Replace Shortcode', 'photonic') |
| 163 | 173 | ]; |
| 164 | - return $actions; | |
| 165 | 174 | } |
| 166 | 175 | |
| 167 | - function process_bulk_action() { | |
| 168 | - if ('replace_shortcode' === $this->current_action()) { | |
| 169 | - if (!empty($_POST['photonic_post'])) { | |
| 170 | - $post_ids = $_POST['photonic_post']; | |
| 171 | - $post_ids = array_map(function ($v) { | |
| 172 | - return "'" . esc_sql($v) . "'"; | |
| 173 | - }, $post_ids); | |
| 174 | - $post_ids = implode(',', $post_ids); | |
| 175 | - } | |
| 176 | + private function process_bulk_action() { | |
| 177 | + if ('replace_shortcode' === $this->current_action() && !empty($_POST['photonic_post']) && !empty($_POST['_photonic_replacement_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_photonic_replacement_nonce'])), 'photonic-replace-shortcode-' . get_current_user_id())) { | |
| 178 | + $post_ids = $_POST['photonic_post']; // Cannot sanitize this since it is an array. Will sanitize each of its components in the array_walk. | |
| 179 | + array_walk($post_ids, 'sanitize_text_field'); | |
| 176 | 180 | } |
| 177 | - else if ('replace_shortcode_individual' === $this->current_action()) { | |
| 178 | - if (!empty($_REQUEST['photonic_post_id'])) { | |
| 179 | - $post_ids = esc_sql($_REQUEST['photonic_post_id']); | |
| 180 | - } | |
| 181 | + elseif ('replace_shortcode_individual' === $this->current_action() && !empty($_REQUEST['photonic_post_id'])) { | |
| 182 | + $post_ids = [sanitize_text_field(wp_unslash($_REQUEST['photonic_post_id']))]; | |
| 181 | 183 | } |
| 182 | 184 | |
| 183 | 185 | if (!empty($post_ids)) { |
| 184 | 186 | global $wpdb, $photonic_alternative_shortcode; |
| @@ -183,10 +185,12 @@ | ||
| 183 | 185 | if (!empty($post_ids)) { |
| 184 | 186 | global $wpdb, $photonic_alternative_shortcode; |
| 185 | 187 | if (empty($photonic_alternative_shortcode) || strtolower($photonic_alternative_shortcode) === 'gallery') { |
| 186 | 188 | echo "<div class='notice notice-error is-dismissible'>\n<p>\n"; |
| 187 | - echo sprintf(esc_html__('Cannot update the posts because a custom shortcode has not been set up under %s.', 'photonic'), | |
| 188 | - '<strong><em>Photonic → Settings → Generic Options → Generic Settings → Custom Shortcode</em></strong>'); | |
| 189 | + echo sprintf( | |
| 190 | + esc_html__('Cannot update the posts because a custom shortcode has not been set up under %s.', 'photonic'), | |
| 191 | + '<strong><em>Photonic → Settings → Generic Options → Generic Settings → Custom Shortcode</em></strong>' | |
| 192 | + ); | |
| 189 | 193 | echo "\n</p>\n</div>\n"; |
| 190 | 194 | return; |
| 191 | 195 | } |
| 192 | 196 | |
| @@ -194,11 +198,15 @@ | ||
| 194 | 198 | $o_len = strlen($this->tag); |
| 195 | 199 | $r_len = strlen($r_tag); |
| 196 | 200 | |
| 197 | 201 | $results = $wpdb->get_results( |
| 198 | - "SELECT ID, post_type, post_status, post_title, post_content FROM {$wpdb->posts} where post_type not in ('revision', 'attachment', 'nav_menu_item', 'oembed_cache') and post_status not in ('trash', 'inherit') and ID in (" . $post_ids . ")", | |
| 202 | + $wpdb->prepare( | |
| 203 | + "SELECT ID, post_type, post_status, post_title, post_content FROM {$wpdb->posts} where post_type not in ('revision', 'attachment', 'nav_menu_item', 'oembed_cache') and post_status not in ('trash', 'inherit') and ID in (" . implode(', ', array_fill(0, count($post_ids), '%s')) . ")", | |
| 204 | + ...$post_ids | |
| 205 | + ), | |
| 199 | 206 | ARRAY_A |
| 200 | 207 | ); |
| 208 | + | |
| 201 | 209 | $pattern = get_shortcode_regex([$this->tag]); |
| 202 | 210 | $types = ['default', 'wp', 'flickr', 'smugmug', 'picasa', 'google', 'zenfolio', 'instagram']; |
| 203 | 211 | $layouts = ['square', 'circle', 'random', 'masonry', 'mosaic', 'strip-above', 'strip-below', 'strip-right', 'no-strip']; |
| 204 | 212 | |
| @@ -210,13 +218,13 @@ | ||
| 210 | 218 | if (!empty($matches) && !empty($matches[0]) && !empty($matches[1]) && !empty($matches[2]) && !empty($matches[3])) { |
| 211 | 219 | $instances = []; |
| 212 | 220 | $init = $post['post_content']; |
| 213 | 221 | foreach ($matches[1] as $instance => $start) { |
| 214 | - if ($start[0] === '') { | |
| 222 | + if ('' === $start[0]) { | |
| 215 | 223 | if (!empty($matches[3][$instance])) { |
| 216 | 224 | $attributes = shortcode_parse_atts($matches[3][$instance][0]); |
| 217 | - if ((!empty($attributes['type']) && in_array($attributes['type'], $types)) || | |
| 218 | - (empty($attributes['type']) && !empty($attributes['style']) && in_array($attributes['style'], $layouts))) { | |
| 225 | + if ((!empty($attributes['type']) && in_array($attributes['type'], $types, true)) || | |
| 226 | + (empty($attributes['type']) && !empty($attributes['style']) && in_array($attributes['style'], $layouts, true))) { | |
| 219 | 227 | $offset = count($instances) * ($r_len - $o_len); |
| 220 | 228 | $upto = substr($init, 0, $matches[0][$instance][1] + $offset); |
| 221 | 229 | $instances[] = $instance; |
| 222 | 230 | $replacement = str_replace('[' . $this->tag, '[' . $r_tag, $matches[0][$instance][0]); |
| @@ -228,14 +236,12 @@ | ||
| 228 | 236 | } |
| 229 | 237 | } |
| 230 | 238 | if ($changed) { |
| 231 | 239 | $update = $wpdb->update($wpdb->posts, ['post_content' => $init], ['ID' => $post['ID']]); |
| 232 | - if ($update === false) { | |
| 240 | + if (false === $update) { | |
| 233 | 241 | $got_error = true; |
| 234 | 242 | } |
| 235 | - else if ($update === 0) { | |
| 236 | - } | |
| 237 | - else { | |
| 243 | + elseif (0 !== $update) { | |
| 238 | 244 | $count++; |
| 239 | 245 | } |
| 240 | 246 | } |
| 241 | 247 | } |
| @@ -243,30 +249,35 @@ | ||
| 243 | 249 | if ($got_error) { |
| 244 | 250 | $type = 'error'; |
| 245 | 251 | $message = esc_html__('Failed to replace shortcodes due to an error. Please open a support ticket.', 'photonic'); |
| 246 | 252 | } |
| 247 | - else if ($count === 0) { | |
| 253 | + elseif (0 === $count) { | |
| 248 | 254 | $type = 'warning'; |
| 249 | 255 | $message = esc_html__('0 replacements made. If this is not what you were expecting please open a support ticket.', 'photonic'); |
| 250 | 256 | } |
| 251 | 257 | else { |
| 252 | 258 | $type = 'success'; |
| 253 | - $message = esc_html(sprintf(_n( | |
| 254 | - '%d post updated with the shortcode replacement.', | |
| 255 | - '%d posts updated with the shortcode replacement.', | |
| 256 | - $count, 'photonic'), | |
| 257 | - $count) | |
| 259 | + $message = esc_html( | |
| 260 | + sprintf( | |
| 261 | + _n( | |
| 262 | + '%d post updated with the shortcode replacement.', | |
| 263 | + '%d posts updated with the shortcode replacement.', | |
| 264 | + $count, | |
| 265 | + 'photonic' | |
| 266 | + ), | |
| 267 | + $count | |
| 268 | + ) | |
| 258 | 269 | ); |
| 259 | 270 | } |
| 260 | 271 | |
| 261 | - echo "<div class='notice notice-$type is-dismissible'>\n<p>\n"; | |
| 262 | - echo $message; | |
| 272 | + echo "<div class='notice notice-" . sanitize_html_class($type) . " is-dismissible'>\n<p>\n"; | |
| 273 | + echo wp_kses_post($message); | |
| 263 | 274 | echo "\n</p>\n</div>\n"; |
| 264 | 275 | } |
| 265 | 276 | } |
| 266 | 277 | |
| 267 | - function remove_args($args) { | |
| 278 | + public function remove_args($args) { | |
| 268 | 279 | $args[] = 'action'; |
| 269 | 280 | $args[] = 'photonic_post_id'; |
| 270 | 281 | return $args; |
| 271 | 282 | } |
| 272 | -} | |
| 283 | +} | |