html = $html;
$this->plugin = $plugin;
}
protected abstract function cloneItemMeta(int $fromPostId, int $toPostId);
protected function getActionClone(): string
{
return static::NAME . '_clone';
}
protected function getActionCloned(): string
{
return static::NAME . '_cloned';
}
public function setHooks(): void
{
add_action('admin_init', [$this, 'cloneItemAction']);
add_action('admin_notices', [$this, 'showItemClonedMessage']);
add_action('add_meta_boxes', [$this, 'addMetaboxes']);
add_action('admin_enqueue_scripts', [$this, 'addCodemirrorToCPTEditScreens']);
add_filter('views_edit-' . static::NAME, [$this, 'printPostTypeDescription',]);
add_filter('post_row_actions', [$this, 'getRowActions',], 10, 2);
}
public function cloneItemAction(): void
{
if (! isset($_GET[$this->getActionClone()])) {
return;
}
$viewId = (int)$_GET[$this->getActionClone()];
$viewPost = get_post($viewId);
if (! $viewPost ||
static::NAME !== $viewPost->post_type) {
return;
}
check_admin_referer('bulk-posts');
$args = [
'post_type' => $viewPost->post_type,
'post_status' => 'draft',
'post_name' => $viewPost->post_name,
'post_title' => $viewPost->post_title . ' Clone',
'post_author' => $viewPost->post_author,
];
$newPostId = wp_insert_post($args);
// something went wrong
if (is_wp_error($newPostId)) {
return;
}
$this->cloneItemMeta($viewPost->ID, $newPostId);
$targetUrl = get_admin_url(null, '/edit.php?post_type=' . static::NAME);
$targetUrl .= '&' . $this->getActionCloned() . '=1';
wp_redirect($targetUrl);
exit;
}
public function showItemClonedMessage(): void
{
if (! isset($_GET[$this->getActionCloned()])) {
return;
}
echo '
' .
'
Item success cloned.
' .
'
';
}
public function getRowActions(array $actions, WP_Post $view): array
{
if (static::NAME !== $view->post_type) {
return $actions;
}
$trash = str_replace('>Trash<', '>Delete<', $actions['trash']);
// quick edit
unset($actions['inline hide-if-no-js']);
unset($actions['trash']);
$cloneLink = get_admin_url(null, '/edit.php?post_type=' . static::NAME);
$cloneLink .= '&' . $this->getActionClone() . '=' . $view->ID . '&_wpnonce=' . wp_create_nonce(
'bulk-posts'
);
$actions['clone'] = "Clone";
$actions['trash'] = $trash;
return $actions;
}
protected function addProBannerMetabox(): void
{
add_meta_box(
'acf-views_pro',
'PRO',
function ($post, $meta) {
echo $this->html->proBanner('https://wplake.org/acf-views/', $this->plugin->getAssetsUrl('pro.png'));
},
[
static::NAME,
],
'side'
);
}
public function addMetaboxes(): void
{
add_meta_box(
'acf-views_review',
'Rate & Review',
function ($post, $meta) {
echo $this->html->postboxReview();
},
[
static::NAME,
],
'side'
);
add_meta_box(
'acf-views_support',
'Having issues?',
function ($post, $meta) {
echo $this->html->postboxSupport();
},
[
static::NAME,
],
'side'
);
$this->addProBannerMetabox();
}
public function addCodemirrorToCPTEditScreens()
{
$currentScreen = get_current_screen();
if (in_array($currentScreen->id, [static::NAME,], true) ||
in_array($currentScreen->post_type, [static::NAME], true)) {
$cmSettings = [
'_html' => wp_enqueue_code_editor([
'type' => 'text/html',
'codemirror' => [
'lint' => true,
],
]),
'_css' => wp_enqueue_code_editor([
'type' => 'text/css',
'codemirror' => [
'lint' => true,
],
]),
'_js' => wp_enqueue_code_editor([
'type' => 'javascript',
'codemirror' => [
'lint' => true,
'matchBrackets' => true,
'autoCloseBrackets' => true,
],
]),
];
wp_localize_script('jquery', '_cm_settings', $cmSettings);
wp_enqueue_script('wp-theme-plugin-editor');
wp_enqueue_style('wp-codemirror');
}
}
public function printPostTypeDescription($views)
{
$screen = get_current_screen();
$postType = get_post_type_object($screen->post_type);
if ($postType->description) {
printf('%s
', esc_html($postType->description));
}
return $views; // return original input unchanged
}
}