_x('ASO Configurations', "all-signs-options-free"),
'singular_name' => _x('ASO Configurations', "all-signs-options-free"),
'add_new' => _x('New ASO configuration', "all-signs-options-free"),
'add_new_item' => _x('New ASO configuration', "all-signs-options-free"),
'edit_item' => _x('Edit ASO configuration', "all-signs-options-free"),
'new_item' => _x('New ASO configuration', "all-signs-options-free"),
'view_item' => _x('View ASO configuration', "all-signs-options-free"),
'not_found' => _x('No ASO configuration found', "all-signs-options-free"),
'not_found_in_trash' => _x('No ASO configuration in the trash', "all-signs-options-free"),
'menu_name' => _x('All Signs Options', "all-signs-options-free"),
'all_items' => _x('ASO Configurations', "all-signs-options-free"),
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'description' => 'ASO Configurations',
'supports' => array('title'),
'public' => false,
'show_in_rest' => true,
'show_ui' => true,
'show_in_menu' => false,
'show_in_nav_menus' => false,
'publicly_queryable' => false,
'exclude_from_search' => true,
'has_archive' => false,
'query_var' => false,
'can_export' => true,
);
register_post_type('asowp-configs', $args);
}
/**
* Create meta data of asowp-configs-meta
*/
public function register_asowp_config_meta()
{
register_meta(
'asowp-configs',
'asowp-configs-meta',
array(
'show_in_rest' => array(
'schema' => array(
'type' => 'array',
'items' => array(
'type' => 'array',
'items' => array(
'type' => 'mixed'
)
)
)
),
'type' => 'array',
'single' => true,
)
);
}
/**
* Create meta data of asowp-configs-meta
*/
public function register_asowp_config_templates()
{
register_meta(
'asowp-configs',
'asowp-configs-templates',
array(
'show_in_rest' => array(
'schema' => array(
'type' => 'array',
'items' => array(
'type' => 'array',
'items' => array(
'type' => 'mixed'
)
)
)
),
'type' => 'array',
'single' => true,
)
);
}
/**
* Add short code on config page
*/
public function get_editor_shortcode_handler($content)
{
global $wp_query;
$page_settings = get_option("asowp_config_page");
if ((get_the_ID() == $page_settings["configuratorPage"]) && is_page($page_settings["configuratorPage"])) {
if (!isset($wp_query->query_vars['asowp-product-id'])) {
ob_start();
?>
query_vars['asowp-tplid'])) {
$content .= do_shortcode("[asowp-configurator productid='" . $wp_query->query_vars['asowp-product-id'] . "' tplid='" . $wp_query->query_vars['asowp-tplid'] . "']");
} else {
$content .= do_shortcode("[asowp-configurator productid='" . $wp_query->query_vars['asowp-product-id'] . "']");
}
}
}
return $content;
}
public function redirect_if_no_product_id()
{
global $wp_query;
$page_settings = get_option("asowp_config_page");
// If no configuration page is set, or the setting is invalid, do nothing.
if (empty($page_settings) || !is_array($page_settings) || !isset($page_settings["configuratorPage"]) || empty($page_settings["configuratorPage"])) {
return;
}
// Check if we are on the configurator page and if no product ID is set
if (is_page($page_settings["configuratorPage"]) && !isset($wp_query->query_vars['asowp-product-id'])) {
$product_id_to_redirect = 0;
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'product-asowp-metas',
'compare' => 'EXISTS',
),
),
'fields' => 'ids',
);
$products = new \WP_Query($args);
if ($products->have_posts()) {
foreach ($products->posts as $product_id) {
$meta = get_post_meta($product_id, 'product-asowp-metas', true);
if (is_array($meta) && isset($meta[$product_id]['config-id']) && !empty($meta[$product_id]['config-id'])) {
$product_id_to_redirect = $product_id;
break;
}
}
}
wp_reset_postdata();
if ($product_id_to_redirect > 0) {
$redirect_url = get_permalink($page_settings["configuratorPage"]);
if (get_option('permalink_structure')) {
if (substr($redirect_url, -1) !== '/') {
$redirect_url .= '/';
}
$redirect_url .= 'asowp-design/' . $product_id_to_redirect . '/';
} else {
$redirect_url = add_query_arg('asowp-product-id', $product_id_to_redirect, $redirect_url);
}
wp_redirect($redirect_url);
exit;
}
}
}
public function asowp_add_query_vars($a_vars)
{
$a_vars[] = 'asowp-product-id';
$a_vars[] = 'asowp-tplid';
$a_vars[] = 'edit';
$a_vars[] = 'design-index';
$a_vars[] = 'vcid';
return $a_vars;
}
public function asowp_add_design_page_rewrite_rules()
{
global $wp_rewrite;
$page_settings = get_option("asowp_config_page");
if (!empty($page_settings) && $page_settings != false) {
$asowp_page_id = $page_settings["configuratorPage"];
$asowp_page = get_post($asowp_page_id);
if (is_object($asowp_page)) {
$raw_slug = get_permalink($asowp_page->ID);
$home_url = home_url('/');
$slug = trim(str_replace($home_url, '', $raw_slug), '/');
add_rewrite_rule(
$slug . '/asowp-design/([^/]+)/([^/]+)/?$',
'index.php?pagename=' . $slug . '&asowp-product-id=$matches[1]&asowp-tplid=$matches[2]',
'top'
);
add_rewrite_rule(
$slug . '/asowp-design/([^/]+)/?$',
'index.php?pagename=' . $slug . '&asowp-product-id=$matches[1]',
'top'
);
}
}
$wp_rewrite->flush_rules(false);
}
}