<?php
/**
* Class Kadence_Pro\Elements_Controller
*
* @package Kadence Pro
*/
namespace Kadence_Pro;
use DateTime;
use Kadence_Blocks_Frontend;
use FLBuilder;
use FLBuilderModel;
use DOMDocument;
use \Elementor\Plugin;
use \Elementor\Core\Files\CSS\Post;
use Brizy_Editor;
use Brizy_Editor_Post;
use Brizy_Editor_Project;
use Brizy_Editor_CompiledHtml;
use Brizy_Public_Main;
use SiteOrigin_Panels_Settings;
use function Kadence\kadence;
use function get_editable_roles;
use function siteorigin_panels_render;
use function do_shortcode;
use function tutor;
use function extension_loaded;
use function libxml_use_internal_errors;
/**
* Class managing the template areas post type.
*/
class Elements_Controller {
const SLUG = 'kadence_element';
const TYPE_SLUG = 'element_type';
const TYPE_META_KEY = '_kad_element_type';
/**
* Set check if user wants ace block.
*
* @var $aceblock bool for ace block.
*/
private static $aceblock = null;
/**
* Current condition
*
* @var null
*/
public static $current_condition = null;
/**
* Current user
*
* @var null
*/
public static $current_user = null;
/**
* Gather placeholders for elements
* (This method was adopted from the advanaced ads plugin)
*
* @var array $placeholders_for_elements
*/
private static $placeholders_for_elements = array();
/**
* Instance Control
*
* @var null
*/
private static $instance = null;
/**
* Status if single template is being used.
*
* @var null
*/
public static $single_template = null;
/**
* Current single override
*
* @var null
*/
public static $loop_override = null;
/**
* Throw error on object clone.
*
* The whole idea of the singleton design pattern is that there is a single
* object therefore, we don't want the object to be cloned.
*
* @return void
*/
public function __clone() {
// Cloning instances of the class is forbidden.
_doing_it_wrong( __FUNCTION__, esc_html__( 'Cloning instances of the class is Forbidden', 'kadence-pro' ), '1.0' );
}
/**
* Disable un-serializing of the class.
*
* @return void
*/
public function __wakeup() {
// Unserializing instances of the class is forbidden.
_doing_it_wrong( __FUNCTION__, esc_html__( 'Unserializing instances of the class is forbidden', 'kadence-pro' ), '1.0' );
}
/**
* Instance Control.
*/
public static function get_instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor function.
*/
public function __construct() {
add_action( 'init', array( $this, 'register_post_type' ), 1 );
add_filter( 'user_has_cap', array( $this, 'filter_post_type_user_caps' ) );
add_action( 'init', array( $this, 'plugin_register' ), 20 );
add_action( 'init', array( $this, 'register_meta' ), 20 );
add_action( 'init', array( $this, 'setup_content_filter' ), 9 );
add_action( 'enqueue_block_editor_assets', array( $this, 'script_enqueue' ) );
add_action( 'wp', array( $this, 'init_frontend_hooks' ), 99 );
add_action( 'kadence_single_product_ajax_added_to_cart', array( $this, 'init_mini_cart_hooks' ) );
add_filter( 'kadence_post_layout', array( $this, 'element_single_layout' ), 99 );
add_action( 'init', array( $this, 'element_gutenberg_template' ) );
add_action( 'init', array( $this, 'register_ace_script_block' ) );
add_action( 'kadence_theme_admin_menu', array( $this, 'create_admin_page' ) );
add_filter( 'submenu_file', array( $this, 'current_menu_fix' ) );
add_shortcode( self::SLUG, array( $this, 'shortcode_output' ) );
add_action( 'wp', array( $this, 'elements_single_only_logged_in_editors' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
//add_action( 'rest_request_before_callbacks', array( $this, 'filter_rest_request_for_kses' ), 100, 3 );
add_filter( 'the_content', array( $this, 'undo_filters_for_code_output' ), 1 );
// allow shortcode in widgets.
add_filter( 'widget_text', 'do_shortcode' );
//add_action( 'in_admin_header', array( $this, 'add_element_tabs' ) );
// add_action(
// 'kadence_theme_admin_menu',
// function() {
// $this->fix_admin_menu_entry();
// }
// );
$slug = self::SLUG;
add_filter(
"manage_{$slug}_posts_columns",
function( array $columns ) : array {
return $this->filter_post_type_columns( $columns );
}
);
add_action(
"manage_{$slug}_posts_custom_column",
function( string $column_name, int $post_id ) {
$this->render_post_type_column( $column_name, $post_id );
},
10,
2
);
// if ( is_admin() ) {
// add_action( 'load-post.php', array( $this, 'init_classic_metabox' ) );
// add_action( 'load-post-new.php', array( $this, 'init_classic_metabox' ) );
// }
//add_filter( 'wpseo_sitemap_exclude_post_type', array( $this, 'sitemap_exclude_elements' ), 10, 2 );
//add_action( 'add_meta_boxes', array( $this, 'yoast_exclude_elements' ), 100 );
// Add tabs for element "types". Here is where that happens.
add_filter( 'views_edit-' . self::SLUG, array( $this, 'admin_print_tabs' ) );
add_action( 'pre_get_posts', array( $this, 'admin_filter_results' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'action_enqueue_admin_scripts' ) );
add_action( 'wp_ajax_kadence_elements_change_status', array( $this, 'ajax_change_status' ) );
if ( class_exists( 'Kadence_Pro\Duplicate_Elements' ) ) {
new Duplicate_Elements( self::SLUG );
}
}
/**
* Enqueues a script that adds sticky for single products
*/
public function action_enqueue_admin_scripts() {
$current_page = get_current_screen();
if ( 'edit-' . self::SLUG === $current_page->id ) {
// Enqueue the post styles.
wp_enqueue_style( 'kadence-elements-admin', KTP_URL . 'dist/elements/kadence-pro-element-post-admin.css', false, KTP_VERSION );
wp_enqueue_script( 'kadence_elements-admin', KTP_URL . 'dist/elements/kadence-pro-element-post-admin.min.js', array( 'jquery' ), KTP_VERSION, true );
wp_localize_script(
'kadence_elements-admin',
'kadence_elements_params',
array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'ajax_nonce' => wp_create_nonce( 'kadence_elements-ajax-verification' ),
'draft' => esc_attr__( 'Draft', 'kadence-pro' ),
'publish' => esc_attr__( 'Published', 'kadence-pro' ),
)
);
}
}
/**
* Ajax callback function.
*/
public function ajax_change_status() {
check_ajax_referer( 'kadence_elements-ajax-verification', 'security' );
if ( ! isset ( $_POST['post_id'] ) || ! isset( $_POST['post_status'] ) ) {
wp_send_json_error( __( 'Error: No post information was retrieved.', 'kadence-pro' ) );
}
$post_id = empty( $_POST['post_id'] ) ? '' : sanitize_text_field( wp_unslash( $_POST['post_id'] ) );
$post_status = empty( $_POST['post_status'] ) ? '' : sanitize_text_field( wp_unslash( $_POST['post_status'] ) );
$response = false;
if ( 'publish' === $post_status ) {
$response = $this->change_post_status( $post_id, 'draft' );
} else if ( 'draft' === $post_status ) {
$response = $this->change_post_status( $post_id, 'publish' );
}
if ( ! $response ) {
$error = new WP_Error( '001', 'Post Status invalid.' );
wp_send_json_error( $error );
}
wp_send_json_success();
}
/**
* Change the post status
* @param number $post_id - The ID of the post you'd like to change.
* @param string $status - The post status publish|pending|draft|private|static|object|attachment|inherit|future|trash.
*/
public function change_post_status( $post_id, $status ) {
if ( 'publish' === $status || 'draft' === $status ) {
$current_post = get_post( $post_id );
$current_post->post_status = $status;
return wp_update_post( $current_post );
} else {
return false;
}
}
/**
* Filter the post results if tabs selected.
*
* @param object $query An array of available list table views.
*/
public function admin_filter_results( $query ) {
if ( ! ( is_admin() && $query->is_main_query() ) ) {
return $query;
}
if ( ! ( isset( $query->query['post_type'] ) && 'kadence_element' === $query->query['post_type'] ) ) {
return $query;
}
$screen = get_current_screen();
if ( ! empty( $screen ) && $screen->id == 'edit-kadence_element' ) {
if ( isset( $_REQUEST[ self::TYPE_SLUG ] ) ) {
$type_slug = sanitize_text_field( $_REQUEST[ self::TYPE_SLUG ] );
if ( ! empty( $type_slug ) ) {
$query->query_vars['meta_query'] = array(
array(
'key' => self::TYPE_META_KEY,
'value' => $type_slug,
),
);
}
}
$query->query_vars['order'] = 'ASC';
$query->query_vars['orderby'] = 'menu_order';
}
return $query;
}
/**
* Print admin tabs.
*
* Used to output the conversion tabs with their labels.
*
*
* @param array $views An array of available list table views.
*
* @return array An updated array of available list table views.
*/
public function admin_print_tabs( $views ) {
$current_type = '';
$active_class = ' nav-tab-active';
if ( ! empty( $_REQUEST[ self::TYPE_SLUG ] ) ) {
$current_type = $_REQUEST[ self::TYPE_SLUG ];
$active_class = '';
}
$url_args = [
'post_type' => self::SLUG,
];
$baseurl = add_query_arg( $url_args, admin_url( 'edit.php' ) );
?>
<div id="kadence-element-tabs-wrapper" class="nav-tab-wrapper">
<a class="nav-tab<?php echo esc_attr( $active_class ); ?>" href="<?php echo esc_url( $baseurl ); ?>">
<?php echo esc_html__( 'All Element Items', 'kadence-pro' ); ?>
</a>
<?php
$types = array(
'default' => array(
'label' => __( 'Sections', 'kadence-pro' ),
),
'fixed' => array(
'label' => __( 'Fixed Items', 'kadence-pro' ),
),
'template' => array(
'label' => __( 'Templates', 'kadence-pro' ),
),
'script' => array(
'label' => __( 'HTML code', 'kadence-pro' ),
),
);
foreach ( $types as $key => $type ) :
$active_class = '';
if ( $current_type === $key ) {
$active_class = ' nav-tab-active';
}
$type_url = esc_url( add_query_arg( self::TYPE_SLUG, $key, $baseurl ) );
$type_label = $type['label'];
echo "<a class='nav-tab{$active_class}' href='{$type_url}'>{$type_label}</a>";
endforeach;
?>
</div>
<?php
return $views;
}
/**
* Meta box initialization.
*/
public function init_classic_metabox() {
$path = KTP_URL . 'build/';
wp_enqueue_style( 'kadence-element-meta', KTP_URL . 'dist/build/meta-controls.css', array( 'wp-components' ), KTP_VERSION );
wp_register_script(
'kadence-element-classic-meta',
$path . 'classic-meta.js',
array( 'moment', 'react', 'react-dom', 'wp-components', 'wp-compose', 'wp-data', 'wp-element', 'wp-i18n', 'wp-polyfill', 'wp-edit-post' ),
KTP_VERSION
);
add_action( 'add_meta_boxes', array( $this, 'add_metabox' ) );
}
/**
Loading ...