[
'SITE_TITLE' => 'Site Title',
'SITE_TAGLINE' => 'Site Tagline',
'SITE_URL' => 'Site URL',
'CURRENT_DATE' => 'Current date',
'CURRENT_TIME' => 'Current time',
],
'Post/Page' => [
'POST_ID' => 'Post ID',
'POST_TITLE' => 'Post title',
'POST_CATEGORY' => 'Post category',
'POST_FEATURED_IMAGE' => 'Post Featured image',
'POST_EXCERPT' => 'Post excerpt',
'POST_URL' => 'Post URL',
'POST_DATE' => 'Post date',
'POST_TIME' => 'Post time',
'POST_COMMENTS_NUMBER' => 'Number of comments',
],
'Archive' => [
'ARCHIVE_ID' => 'Archive ID',
'ARCHIVE_TITLE' => 'Archive title',
'ARCHIVE_URL' => 'Archive URL',
],
'Author' => [
'AUTHOR_ID' => 'Author ID',
'AUTHOR_FIRST_NAME' => 'Author first name',
'AUTHOR_LAST_NAME' => 'Author last name',
'AUTHOR_DISPLAY_NAME' => 'Author display name',
'AUTHOR_EMAIL' => 'Author e-mail',
'AUTHOR_URL' => 'Author Profile URL',
'AUTHOR_POSTS_URL' => 'Author Posts URL',
],
];
public function __construct() {
if ( isset( $_GET['adv-vis-ele-library-reset'] ) && $_GET['adv-vis-ele-library-reset'] === 'true' ) {
update_option( 'adv_vis_ele_library_info_v2', [] );
}
$library_info = array_filter( get_option( 'adv_vis_ele_library_info_v2', [] ) );
if ( !empty( $library_info ) ) {
$this->library_info = $library_info;
} else {
$data = [
'request' => 'elements-json',
];
$response = wp_safe_remote_post( ADV_VIS_ELE_PLUGIN_WEBSITE . '/adv-vis-ele/v2/index.php', [
'body' => $data,
'timeout' => 10,
] );
$output = wp_remote_retrieve_body( $response );
if ( !empty( $output ) ) {
$decoded = json_decode( $output );
if ( is_object( $decoded ) ) {
if ( isset( $decoded->response ) && $decoded->response === 'success' && isset( $decoded->data->elements_json ) && !empty( $decoded->data->elements_json ) ) {
if ( Adv_Vis_Ele_Admin::is_dev() && isset( $_GET['adv-vis-ele-library-dev'] ) && $_GET['adv-vis-ele-library-dev'] === 'true' && isset( $decoded->data_dev->elements_json ) && !empty( $decoded->data_dev->elements_json ) ) {
$live_json_ids = array_column( $decoded->data->elements_json, 'id' );
$dev_json_ids = array_column( $decoded->data_dev->elements_json, 'id' );
foreach ( $dev_json_ids as $did ) {
if ( in_array( $did, $live_json_ids ) ) {
foreach ( $decoded->data->elements_json as $kid => $lid ) {
if ( $did === $lid->id ) {
unset($decoded->data->elements_json[$kid]);
}
}
}
}
$decoded->data->elements_json = array_merge( $decoded->data->elements_json, $decoded->data_dev->elements_json );
}
$this->library_info = $decoded->data->elements_json;
update_option( 'adv_vis_ele_library_info_v2', $decoded->data->elements_json );
}
}
}
}
// WooCommerce dynamic settings array
if ( class_exists( 'woocommerce' ) ) {
Adv_Vis_Ele_Library::$dynamic_settings['WooCommerce'] = [
'WOO_PRODUCT_SKU' => 'Product SKU',
'WOO_PRODUCT_IMAGE' => 'Product image',
'WOO_PRODUCT_SHORT_DESCRIPTION' => 'Product short description',
'WOO_PRODUCT_LONG_DESCRIPTION' => 'Product long description',
'WOO_PRODUCT_REGULAR_PRICE' => 'Product regular price',
'WOO_PRODUCT_SALE_PRICE' => 'Product sale price',
'WOO_CURRENCY_SYMBOL' => 'WooCommerce currency symbol',
'WOO_PRODUCT_STOCK_STATUS' => 'Product stock status',
'WOO_PRODUCT_STOCK_QUANTITY' => 'Product stock quantity',
'WOO_PRODUCT_SHIPPING_CLASS' => 'Product shipping class',
'WOO_ADD_TO_CART_TEXT' => 'WooCommerce "Add to cart" text',
];
}
// Make meta field last array value
Adv_Vis_Ele_Library::$dynamic_settings['Meta/ACF'] = [
'META_FIELD' => 'Meta or ACF field',
];
}
/**
* Get library info
*/
public function get_library_info( $limit = false, $page = false, $order = false ) {
if ( !$order ) {
$order = 'asc';
}
if ( empty( $this->library_info ) ) {
update_option( 'adv_vis_ele_library_info_v2', [] );
// reset for the next constructor
return [];
// should never happen
}
array_multisort( array_column( $this->library_info, 'name' ), SORT_NATURAL, $this->library_info );
if ( $order === 'desc' ) {
$library_info = array_reverse( $this->library_info );
} else {
$library_info = $this->library_info;
}
if ( is_int( $limit ) && is_int( $page ) ) {
return array_slice( $library_info, $page * $limit - $limit, $limit );
}
return $library_info;
}
/**
* Get all element IDs
*/
public function get_all_element_ids() {
$all_elements = [];
if ( !empty( $this->library_info ) ) {
foreach ( $this->library_info as $e ) {
$all_elements[] = $e->id;
}
}
return $all_elements;
}
/**
* Get all used element IDs
*/
public function get_all_used_element_ids() {
$all_elements = [];
$ave_shortcodes = get_posts( [
'post_type' => 'adv-vis-element',
'numberposts' => -1,
'post_status' => 'any',
] );
if ( !empty( $ave_shortcodes ) ) {
foreach ( $ave_shortcodes as $e ) {
$id = get_post_meta( $e->ID, 'adv-vis-ele-element_id', true );
if ( !empty( $id ) ) {
$all_elements[] = $id;
}
}
}
return $all_elements;
}
/**
* Get element info from local library
*/
public function get_element( $element_id ) {
$info = $this->get_library_info();
$key = array_search( $element_id, array_column( $info, 'id' ) );
if ( $key === false ) {
return false;
}
if ( is_object( $info[$key] ) ) {
return $info[$key];
}
return false;
}
/**
* Get element options from local library
*/
public function get_element_setting( $element_id, $setting_name ) {
$element = $this->get_element( $element_id );
if ( $element ) {
$key = array_search( $setting_name, array_column( $element->settings, 'id' ) );
if ( $key !== false ) {
return $element->settings[$key];
}
}
return false;
}
/**
* Get elements HTML
*/
public function get_element_html( $element ) {
ob_start();
?>
name );
?>
id . '/';
?>
settings ) && is_array( $element->settings ) ) {
foreach ( $element->settings as $setting ) {
$settings_string .= '';
}
}
if ( empty( $settings_string ) ) {
$settings_string = __( 'No options listed.', 'advanced-visual-elements' );
}
?>
;
?>/maxresdefault.jpg)
license != 'free' ) {
?>
is__premium_only() ? 'PRO' : 'PRO' );
?>
performance_heavy ) && $element->performance_heavy === true ) {
?>
!
license !== 'free' && !wpave_fs()->is__premium_only() ) {
?>
get_library_info( false, false, $order );
} else {
$elements = $this->get_library_info( $limit, $page, $order );
}
$filtered_elements = [];
if ( !empty( $search ) ) {
foreach ( $elements as $element ) {
if ( stristr( $element->name, $search ) !== false || stristr( $element->id, $search ) !== false ) {
$filtered_elements[] = $element;
}
}
$elements = array_slice( $filtered_elements, $page * $limit - $limit, $limit );
if ( empty( $elements ) ) {
$r = [
'message' => 'No search results.',
];
$r['hide'] = true;
wp_send_json_error( $r );
}
} else {
if ( empty( $elements ) ) {
$r = [
'message' => 'Loaded all elements.',
];
$r['hide'] = true;
wp_send_json_error( $r );
}
}
$html = '';
foreach ( $elements as $element ) {
$element->new_element = in_array( $element->id, ADV_VIS_ELE_NEW_ELEMENTS );
$html .= $this->get_element_html( $element );
}
$r = [
'message' => wp_kses_post( $html ),
];
if ( !empty( $search ) ) {
if ( count( $filtered_elements ) > $page * $limit ) {
$r['hide'] = false;
} else {
$r['hide'] = true;
}
} else {
if ( count( $this->library_info ) > $page * $limit ) {
$r['hide'] = false;
} else {
$r['hide'] = true;
}
}
wp_send_json_success( $r );
}
/**
* Render setting field
*/
public function render_setting_field( $setting ) {
ob_start();
echo '';
if ( $setting->type === 'color' ) {
$color_format = ( $setting->format === 'rgba' ? ' data-alpha-enabled="true"' : '' );
echo '';
} else {
if ( $setting->type === 'text' ) {
echo '';
} else {
if ( $setting->type === 'number' ) {
$min = '';
$max = '';
if ( isset( $setting->min ) ) {
$min = 'min="' . esc_attr( $setting->min ) . '"';
}
if ( isset( $setting->max ) ) {
$max = 'max="' . esc_attr( $setting->max ) . '"';
}
echo '';
} else {
if ( $setting->type === 'float' ) {
$min = '';
$max = '';
if ( isset( $setting->min ) ) {
$min = 'min="' . esc_attr( $setting->min ) . '"';
}
if ( isset( $setting->max ) ) {
$max = 'max="' . esc_attr( $setting->max ) . '"';
}
$step = '0.1';
if ( isset( $setting->step ) ) {
$step = $setting->step;
}
echo '';
} else {
if ( $setting->type === 'select' ) {
if ( isset( $setting->multiple ) && $setting->multiple ) {
$size = Adv_Vis_Ele_Helpers::clamp( count( $setting->options ), 1, 10 );
echo '';
} else {
echo '';
}
} else {
if ( $setting->type === 'boolean' ) {
echo '';
// this is needed to submit FALSE value
echo '';
} else {
if ( $setting->type === 'content' ) {
wp_editor( wp_kses_post( wpautop( $setting->value ) ), esc_attr( $setting->id ), [
'wpautop' => false,
'textarea_name' => 'adv-vis-ele-editor-settings[' . esc_attr( $setting->id ) . ']',
'editor_height' => 150,
] );
} else {
if ( $setting->type === 'image' ) {
echo '';
echo '' . __( 'Choose Image', 'adv-vis-ele' ) . '';
} else {
if ( $setting->type === 'video' ) {
echo '';
echo '' . __( 'Choose Video', 'adv-vis-ele' ) . '';
}
}
}
}
}
}
}
}
}
// dynamic post values
if ( $setting->type === 'text' || $setting->type === 'content' || $setting->type === 'image' ) {
echo '';
}
return ob_get_clean();
}
/**
* Full import (AJAX)
*/
public function ajax_full_import() {
$nonce_verified = isset( $_POST['nonce'] ) && wp_verify_nonce( $_POST['nonce'], 'ave_ajax' );
if ( !$nonce_verified ) {
wp_send_json_error( [
'code' => '010',
] );
}
$current_user = wp_get_current_user();
$author = $current_user->ID;
$settings = [];
foreach ( $_POST['settings'] as $key => $setting ) {
$key = sanitize_text_field( $key );
if ( is_array( $setting ) ) {
$settings[$key] = array_map( 'wp_kses_post', $setting );
} else {
$settings[$key] = wp_kses_post( $setting );
}
if ( $settings[$key] === 'true' ) {
$settings[$key] = 'on';
}
}
if ( !empty( $settings ) ) {
$element_id = $settings['adv-vis-ele-element_id'];
unset($settings['adv-vis-ele-element_id']);
// remove the Element ID because we want only settings
$post = [
'post_title' => 'Imported Element (' . $element_id . ')',
'post_status' => 'draft',
'post_type' => 'adv-vis-element',
'post_author' => $author,
];
$new_post_id = wp_insert_post( $post );
if ( is_int( $new_post_id ) && $new_post_id > 0 ) {
add_post_meta( $new_post_id, 'adv-vis-ele-element_id', $element_id );
$saving = $this->get_saving_settings( $settings, $element_id );
Adv_Vis_Ele_Admin::save_element_meta_settings( $saving, $new_post_id );
$redirection_url = admin_url() . 'post.php?post=' . $new_post_id . '&action=edit';
wp_send_json_success( [
'redirection_url' => $redirection_url,
] );
}
}
wp_send_json_error();
}
/**
* Get Element options for saving
*/
public function get_saving_settings( $settings, $element_id ) {
$saving = [];
foreach ( $settings as $setting_name => $setting_value ) {
$setting = $this->get_element_setting( $element_id, $setting_name );
if ( $setting ) {
if ( $setting->license !== 'free' ) {
$saving[$setting_name] = $setting->default;
} else {
$saving[$setting_name] = Adv_Vis_Ele_Admin::filter_setting( $setting, $setting_value );
}
}
}
return $saving;
}
/**
* Quick save preview (AJAX)
*/
public function ajax_quick_save() {
$nonce_verified = isset( $_POST['nonce'] ) && wp_verify_nonce( $_POST['nonce'], 'ave_ajax' );
if ( !$nonce_verified ) {
wp_send_json_error( [
'code' => '010',
] );
}
if ( !isset( $_POST['id'] ) ) {
return;
}
$post_id = (int) $_POST['id'];
$element_id = sanitize_text_field( $_POST['element-id'] );
$settings = [];
foreach ( $_POST['settings'] as $key => $setting ) {
$key = sanitize_text_field( $key );
if ( is_array( $setting ) ) {
$settings[$key] = array_map( 'wp_kses_post', $setting );
} else {
$settings[$key] = wp_kses_post( $setting );
}
}
$meta_settings = Adv_Vis_Ele_Admin::get_element_meta_settings( $post_id, true );
if ( !empty( $meta_settings ) ) {
foreach ( $settings as $name => $setting ) {
if ( in_array( $name, array_keys( $meta_settings ) ) ) {
$meta_settings[$name] = $setting;
}
}
} else {
$meta_settings = $settings;
}
$saving = $this->get_saving_settings( $meta_settings, $element_id );
Adv_Vis_Ele_Admin::save_element_meta_settings( $saving, $post_id, true );
$saving['adv-vis-ele-element_id'] = $element_id;
wp_send_json_success( [
'export' => json_encode( $saving ),
'time' => (int) time(),
] );
}
}