<?php
/**
* Meta Fields.
*
* @package CartFlows
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class Cartflows_Meta_Fields.
*/
class Cartflows_Meta_Fields {
/**
* Instance
*
* @var $instance
*/
private static $instance;
/**
* Initiator
*/
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor
*/
public function __construct() {
/* Add Scripts */
add_action( 'admin_enqueue_scripts', array( $this, 'admin_meta_scripts' ), 20 );
add_action( 'wp_ajax_wcf_json_search_coupons', array( $this, 'json_search_coupons' ) );
add_action( 'wp_ajax_wcf_json_search_products_and_variations', array( $this, 'json_search_products' ) );
add_action( 'wp_ajax_wcf_json_search_pages', array( $this, 'json_search_pages' ) );
add_filter( 'cartflows_admin_js_localize', array( $this, 'localize_vars' ) );
}
/**
* Admin meta scripts
*/
public function admin_meta_scripts() {
global $pagenow;
global $post;
$localize = array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
);
$screen = get_current_screen();
$current_action = isset( $_GET['action'] ) ? sanitize_text_field( $_GET['action'] ) : ''; //phpcs:ignore
if ( is_object( $screen ) &&
( wcf()->utils->is_step_post_type( $screen->post_type ) || 'wcf-edit-step' === $current_action ) ) {
$localize['google_fonts'] = CartFlows_Font_Families::get_google_fonts();
$localize['system_fonts'] = CartFlows_Font_Families::get_system_fonts();
$localize['font_weights'] = array(
'100' => __( 'Thin 100', 'cartflows' ),
'200' => __( 'Extra-Light 200', 'cartflows' ),
'300' => __( 'Light 300', 'cartflows' ),
'400' => __( 'Normal 400', 'cartflows' ),
'500' => __( 'Medium 500', 'cartflows' ),
'600' => __( 'Semi-Bold 600', 'cartflows' ),
'700' => __( 'Bold 700', 'cartflows' ),
'800' => __( 'Extra-Bold 800', 'cartflows' ),
'900' => __( 'Ultra-Bold 900', 'cartflows' ),
);
}
$localize = apply_filters( 'wcf_js_localize', $localize );
$localize_script = '<!-- script to print the admin localized variables -->';
$localize_script .= '<script type="text/javascript">';
$localize_script .= 'var wcf = ' . wp_json_encode( $localize ) . ';';
$localize_script .= '</script>';
echo $localize_script;
if ( is_object( $screen ) &&
( 'post-new.php' == $pagenow || 'post.php' == $pagenow ) &&
wcf()->utils->is_step_post_type( $screen->post_type )
) {
wp_enqueue_style( 'woocommerce_admin_styles' );
wp_enqueue_script( 'select2' );
wp_enqueue_script( 'wc-enhanced-select' );
wp_enqueue_script(
'wcf-admin-meta',
CARTFLOWS_URL . 'admin-legacy/assets/js/admin-edit.js',
array( 'jquery', 'wp-color-picker' ),
CARTFLOWS_VER,
true
);
wp_enqueue_style( 'wcf-admin-meta', CARTFLOWS_URL . 'admin-legacy/assets/css/admin-edit.css', array( 'wp-color-picker' ), CARTFLOWS_VER );
wp_style_add_data( 'wcf-admin-meta', 'rtl', 'replace' );
do_action( 'cartflows_admin_meta_scripts' );
}
}
/**
* Function to search products.
*/
public function json_search_products() {
check_admin_referer( 'wcf-json-search-products-and-variations', 'security' );
global $wpdb;
// get search term.
$term = (string) urldecode( sanitize_text_field( wp_unslash( $_GET['term'] ) ) ); // phpcs:ignore
if ( empty( $term ) ) {
die();
}
// CartFlows supported product types.
$supported_product_types = array( 'simple', 'variable', 'variation', 'subscription', 'variable-subscription', 'subscription_variation', 'course' );
// Allowed product types.
if ( isset( $_GET['allowed'] ) && ! empty( $_GET['allowed'] ) ) {
$allowed_product_types = sanitize_text_field( ( wp_unslash( $_GET['allowed'] ) ) );
$allowed_product_types = $this->sanitize_data_attributes( $allowed_product_types );
$supported_product_types = $allowed_product_types;
}
// Include product types.
if ( isset( $_GET['included'] ) && ! empty( $_GET['included'] ) ) {
$include_product_types = sanitize_text_field( ( wp_unslash( $_GET['included'] ) ) );
$include_product_types = $this->sanitize_data_attributes( $include_product_types );
$supported_product_types = array_merge( $supported_product_types, $include_product_types );
}
// Exclude product types.
if ( isset( $_GET['excluded'] ) && ! empty( $_GET['excluded'] ) ) {
$excluded_product_types = sanitize_text_field( ( wp_unslash( $_GET['excluded'] ) ) );
$excluded_product_types = $this->sanitize_data_attributes( $excluded_product_types );
$supported_product_types = array_diff( $supported_product_types, $excluded_product_types );
}
// Get all products data.
$data = WC_Data_Store::load( 'product' );
$ids = $data->search_products( $term, '', true, false, 11 );
// Get all product objects.
$product_objects = array_filter( array_map( 'wc_get_product', $ids ), 'wc_products_array_filter_readable' );
// Remove the product objects whose product type are not in supported array.
$product_objects = array_filter(
$product_objects,
function ( $arr ) use ( $supported_product_types ) {
return $arr && is_a( $arr, 'WC_Product' ) && in_array( $arr->get_type(), $supported_product_types, true );
}
);
$products_found = array();
foreach ( $product_objects as $product_object ) {
$formatted_name = $product_object->get_formatted_name();
$managing_stock = $product_object->managing_stock();
if ( $managing_stock && ! empty( $_GET['display_stock'] ) ) {
$stock_amount = $product_object->get_stock_quantity();
/* Translators: %d stock amount */
$formatted_name .= ' – ' . sprintf( __( 'Stock: %d', 'cartflows' ), wc_format_stock_quantity_for_display( $stock_amount, $product_object ) );
}
$products_found[ $product_object->get_id() ] = rawurldecode( $formatted_name );
}
wp_send_json( $products_found );
}
/**
* Function to sanitize the product type data attribute.
*
* @param array $product_types product types.
*/
public function sanitize_data_attributes( $product_types = array() ) {
if ( ! is_array( $product_types ) ) {
$product_types = explode( ',', $product_types );
}
// Sanitize the excluded types against valid product types.
foreach ( $product_types as $index => $value ) {
$product_types[ $index ] = strtolower( trim( $value ) );
}
return $product_types;
}
/**
* Function to search coupons
*/
public function json_search_coupons() {
check_admin_referer( 'wcf-json-search-coupons', 'security' );
global $wpdb;
$term = (string) urldecode( sanitize_text_field( wp_unslash( $_GET['term'] ) ) ); // phpcs:ignore
if ( empty( $term ) ) {
die();
}
$posts = wp_cache_get( 'wcf_search_coupons', 'wcf_funnel_Cart' );
if ( false === $posts ) {
$posts = $wpdb->get_results( // phpcs:ignore
$wpdb->prepare(
"SELECT *
FROM {$wpdb->prefix}posts
WHERE post_type = %s
AND post_title LIKE %s
AND post_status = %s",
'shop_coupon',
$wpdb->esc_like( $term ) . '%',
'publish'
)
);
wp_cache_set( 'wcf_search_coupons', $posts, 'wcf_funnel_Cart' );
}
$coupons_found = array();
$all_discount_types = wc_get_coupon_types();
if ( $posts ) {
foreach ( $posts as $post ) {
$discount_type = get_post_meta( $post->ID, 'discount_type', true );
if ( ! empty( $all_discount_types[ $discount_type ] ) ) {
$coupons_found[ get_the_title( $post->ID ) ] = get_the_title( $post->ID ) . ' (Type: ' . $all_discount_types[ $discount_type ] . ')';
}
}
}
wp_send_json( $coupons_found );
}
/**
* Function to search coupons
*/
public function json_search_pages() {
check_ajax_referer( 'wcf-json-search-pages', 'security' );
$term = (string) urldecode( sanitize_text_field( wp_unslash( $_GET['term'] ) ) ); // phpcs:ignore
if ( empty( $term ) ) {
die( 'not found' );
}
$search_string = $term;
$data = array();
$result = array();
add_filter( 'posts_search', array( $this, 'search_only_titles' ), 10, 2 );
$query = new WP_Query(
array(
's' => $search_string,
'post_type' => 'page',
'posts_per_page' => -1,
)
);
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$title = get_the_title();
$title .= ( 0 != $query->post->post_parent ) ? ' (' . get_the_title( $query->post->post_parent ) . ')' : '';
$id = get_the_id();
$data[] = array(
'id' => $id,
'text' => $title,
);
}
}
if ( is_array( $data ) && ! empty( $data ) ) {
$result[] = array(
'text' => '',
'children' => $data,
);
}
wp_reset_postdata();
// return the result in json.
wp_send_json( $result );
}
/**
* Search only titles.
*
* @param string $search Field data.
* @param object $wp_query Query.
*
* @return array.
*/
public function search_only_titles( $search, $wp_query ) {
if ( ! empty( $search ) && ! empty( $wp_query->query_vars['search_terms'] ) ) {
global $wpdb;
$q = $wp_query->query_vars;
$n = ! empty( $q['exact'] ) ? '' : '%';
$search = array();
foreach ( (array) $q['search_terms'] as $term ) {
$search[] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $n . $wpdb->esc_like( $term ) . $n );
}
Loading ...