gallery shortcode for Photonic.
* This is used as an interim conversion step for switching shortcodes to a different, non-gallery shortcode.
* This is crucial for Gutenberg support, as the "Convert to Blocks" capability of Gutenberg is broken (https://github.com/WordPress/gutenberg/issues/10674).
*
* @package Photonic
* @subpackage Gutenberg
* @since 2.10
*/
if(!class_exists('WP_List_Table')){
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
}
class Photonic_Shortcode_Usage extends WP_List_Table {
public $items = array();
public $tag;
var $per_page = 100;
public function __construct($args = array()) {
parent::__construct(array(
'singular' => 'post',
'plural' => 'posts',
'ajax' => false,
));
$this->tag = 'gallery';
add_filter('removable_query_args', array(&$this, 'remove_args'));
}
/**
* List of columns displayed in the table
*
* @return array
*/
public function get_columns() {
return array(
'cb' => '',
'title' => esc_html__('Post Title', 'photonic'),
'type' => esc_html__('Post Type', 'photonic'),
'status' => esc_html__('Post Status', 'photonic'),
'shortcodes' => esc_html__('Gallery Shortcodes used by Photonic', 'photonic'),
);
}
/**
* List of columns sortable by the user
*
* @return array
*/
public function get_sortable_columns() {
return array(
'type' => array('type', true),
'title' => array('title', true),
'status' => array('status', false),
);
}
/**
* Main method to build out the list items
*/
public function prepare_items() {
$this->_column_headers = array(
$this->get_columns(),
array(),
$this->get_sortable_columns(),
'title'
);
$this->process_bulk_action();
global $wpdb;
$results = $wpdb->get_results(
"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')",
ARRAY_A
);
$pattern = get_shortcode_regex(array($this->tag));
$types = array('default', 'wp', 'flickr', 'smugmug', 'picasa', 'google', 'zenfolio', 'instagram');
$layouts = array('square', 'circle', 'random', 'masonry', 'mosaic', 'strip-above', 'strip-below', 'strip-right', 'no-strip');
$data = array();
foreach ($results as $post) {
preg_match_all('/' . $pattern . '/s', $post['post_content'], $matches, PREG_OFFSET_CAPTURE);
if (!empty($matches) && !empty($matches[0]) && !empty($matches[1]) && !empty($matches[2]) && !empty($matches[3])) {
$to_change = array();
foreach ($matches[1] as $instance => $start) {
if ($start[0] === '') {
if (!empty($matches[3][$instance])) {
$attributes = shortcode_parse_atts($matches[3][$instance][0]);
if ((!empty($attributes['type']) && in_array($attributes['type'], $types)) ||
(empty($attributes['type']) && !empty($attributes['style']) && in_array($attributes['style'], $layouts))) {
$to_change[] = "" . esc_html($matches[0][$instance][0]) . "";
}
}
}
}
if (!empty($to_change)) {
$data[] = array(
'id' => $post['ID'],
'type' => $post['post_type'],
'status' => $post['post_status'],
'title' => $post['post_title'],
'shortcodes' => $to_change,
);
}
}
}
$current_page = $this->get_pagenum();
$total_items = count($data);
$data = array_slice($data, (($current_page-1) * $this->per_page), $this->per_page);
$this->items = $data;
$this->set_pagination_args(array(
'total_items' => $total_items,
'per_page' => $this->per_page,
'total_pages' => ceil($total_items/$this->per_page),
));
}
/**
* Default output for a column, if a column-specific output is not defined
*
* @param object $item
* @param string $column_name
* @return null|string
*/
protected function column_default($item, $column_name) {
return isset($item[$column_name]) ? esc_html($item[$column_name]) : null;
}
/**
* Adds the "Checkbox" column for bulk actions
*
* @param object $item
* @return string
*/
protected function column_cb($item) {
return sprintf('', $item['id']);
}
protected function column_title($item) {
$actions = array(
'edit' => '' . esc_html__('Edit', 'photonic') . '',
'view' => '' . esc_html__('View', 'photonic') . '',
'replace_shortcode_individual' => '' . esc_html__('Replace Shortcodes', 'photonic') . '',
);
return $item['title'] . $this->row_actions($actions);
}
protected function column_shortcodes($item) {
return implode("
\n", $item['shortcodes']);
}
public function no_items() {
echo sprintf(esc_html__('No instances of Photonic found with the %s shortcode', 'photonic'), "{$this->tag}");
}
function get_bulk_actions() {
$actions = array(
'replace_shortcode' => esc_html__('Replace Shortcode', 'photonic')
);
return $actions;
}
function process_bulk_action() {
if ('replace_shortcode' === $this->current_action()) {
if (!empty($_POST['photonic_post'])) {
$post_ids = $_POST['photonic_post'];
$post_ids = array_map(function ($v) {
return "'" . esc_sql($v) . "'";
}, $post_ids);
$post_ids = implode(',', $post_ids);
}
}
else if ('replace_shortcode_individual' === $this->current_action()) {
if (!empty($_REQUEST['photonic_post_id'])) {
$post_ids = esc_sql($_REQUEST['photonic_post_id']);
}
}
if (!empty($post_ids)) {
global $wpdb, $photonic_alternative_shortcode;
if (empty($photonic_alternative_shortcode) || strtolower($photonic_alternative_shortcode) === 'gallery') {
echo "
\n"; echo sprintf(esc_html__('Cannot update the posts because a custom shortcode has not been set up under %s.', 'photonic'), 'Photonic → Settings → Generic Options → Generic Settings → Custom Shortcode'); echo "\n
\n\n"; echo $message; echo "\n
\n