diff --git a/classes/Admin.php b/classes/Admin.php index e643423..2c7cf26 100644 --- a/classes/Admin.php +++ b/classes/Admin.php @@ -1,106 +1,114 @@ array( + 'name' => __( 'Enquiry Messages', 'catalogx' ), + 'subtab' => '', + ), + 'quote-requests' => array( + 'name' => __( 'Quotation Requests', 'catalogx' ), + 'subtab' => '', + ), + 'wholesale-users' => array( + 'name' => __( 'Wholesale Users', 'catalogx' ), + 'subtab' => '', + ), + 'rules' => array( + 'name' => __( 'Dynamic Pricing Rules', 'catalogx' ), + 'subtab' => '', + ), + 'settings' => array( + 'name' => __( 'Settings', 'catalogx' ), + 'subtab' => 'all-settings', + ), + 'modules' => array( + 'name' => __( 'Modules', 'catalogx' ), + 'subtab' => '', + ), ); - add_submenu_page( - 'catalogx', - __('Dynamic Pricing Rules', 'catalogx'), - __('Dynamic Pricing Rules', 'catalogx'), - 'manage_woocommerce', - 'catalogx#&tab=rules', - '__return_null' - ); + foreach ( $submenus as $slug => $submenu ) { + // prepare subtab if subtab is exist. + $subtab = ''; - add_submenu_page( - 'catalogx', - __( 'Settings', 'catalogx' ), - __( 'Settings', 'catalogx' ), - 'manage_woocommerce', - 'catalogx#&tab=settings&subtab=all-settings', - '__return_null' - ); + if ( $submenu['subtab'] ) { + $subtab = '&subtab=' . $submenu['subtab']; + } - add_submenu_page( - 'catalogx', - __( 'Modules', 'catalogx' ), - __( 'Modules', 'catalogx' ), - 'manage_woocommerce', - 'catalogx#&tab=modules', - '__return_null' - ); + add_submenu_page( + 'catalogx', + $submenu['name'], + $submenu['name'], + 'manage_woocommerce', + 'catalogx#&tab=' . $slug . $subtab, + '__return_null' + ); + } if ( ! Utill::is_khali_dabba() ) { - $submenu[ 'catalogx' ][] = [ + add_submenu_page( + 'catalogx', + __( 'Upgrade to Pro', 'catalogx' ), ' -
' . __( 'Upgrade to pro', 'catalogx' ). '
', +
' . __( 'Upgrade to pro', 'catalogx' ) . '
', 'manage_woocommerce', - CATALOGX_PRO_SHOP_URL - ]; + '', + array( $this, 'handle_external_redirects' ) + ); } remove_submenu_page( 'catalogx', 'catalogx' ); @@ -108,6 +116,7 @@ public function add_menu() { /** * Callback function for menu page + * * @return void */ public function menu_page_callback() { @@ -116,192 +125,74 @@ public function menu_page_callback() { /** * Enqueue javascript and css + * * @return void */ - public function enqueue_script() { - - if ( get_current_screen()->id !== 'toplevel_page_catalogx' ) return ; - - // Support for media - wp_enqueue_media(); - - // Prepare data of all pages - $pages = get_pages(); - $all_pages = []; + public function enqueue_admin_script() { - if ( $pages ) { - foreach ( $pages as $page ) { - $all_pages[] = [ - 'value' => $page->ID, - 'label' => $page->post_title, - 'key' => $page->ID, - ]; - } - } - - // Prepare data of all user roles - $roles = wp_roles()->roles; - $all_roles = []; - - if ( $roles ) { - foreach ( $roles as $key => $role ) { - $all_roles[] = [ - 'value' => $key, - 'label' => $role[ 'name' ], - 'key' => $key, - ]; - } - } - - // Get all users id and name and prepare data - $users = get_users( [ 'fields' => [ 'display_name', 'id' ] ] ); - $all_users = []; - - foreach ( $users as $user ) { - $all_users[] = [ - 'value' => $user->ID, - 'label' => $user->display_name, - 'key' => $user->ID, - ]; - } - - // Prepare all products - $products_ids = wc_get_products( [ 'limit' => -1, 'return' => 'ids' ] ); - $all_products = []; - - foreach ( $products_ids as $id ) { - $product_name = get_the_title( $id ); - - $all_products[] = [ - 'value' => $id, - 'label' => $product_name, - 'key' => $id, - ]; - } - - // Prepare all product terms - $terms = get_terms( 'product_cat', [ 'orderby' => 'name', 'order' => 'ASC' ] ); - $product_cat = []; - - if ( $terms && empty($terms->errors)) { - foreach ($terms as $term) { - $product_cat[] = [ - 'value' => $term->term_id, - 'label' => $term->name, - 'key' => $term->term_id, - ]; - } - } - - // Prepare all product tages - $tags = get_terms( 'product_tag', [ 'hide_empty' => false ] ); - $product_tags = []; - if ( $tags ) { - foreach ( $tags as $tag ) { - $product_tags[] = [ - 'value' => $tag->term_id, - 'label' => $tag->name, - 'key' => $tag->term_id, - ]; - } + if ( get_current_screen()->id !== 'toplevel_page_catalogx' ) { + return; } - $brands = get_terms('product_brand', [ 'hide_empty' => false ]); - $all_product_brand = []; - if ($brands) { - foreach ($brands as $brand) { - $all_product_brand[] = [ - 'value' => $brand->term_id, - 'label' => $brand->name, - 'key' => $brand->term_id, - ]; - } - } + // Support for media. + wp_enqueue_media(); - // Get current user role - $current_user = wp_get_current_user(); - $current_user_role = ''; - if ( ! empty( $current_user->roles ) && is_array( $current_user->roles ) ) { - $current_user_role = reset($current_user->roles); - } + // Enque script and style. + FrontendScripts::admin_load_scripts(); + FrontendScripts::enqueue_script( 'catalogx-admin-script' ); + FrontendScripts::enqueue_script( 'catalogx-components-script' ); + FrontendScripts::enqueue_style( 'catalogx-components-style' ); + FrontendScripts::localize_scripts( 'catalogx-admin-script' ); + } - // Get all tab setting's database value - $settings_value = []; - $tabs_names = [ 'enquiry-catalog-customization', 'all-settings', 'enquiry-form-customization', 'enquiry-quote-exclusion', 'tools', 'enquiry-email-temp', 'wholesale', 'wholesale-registration', 'pages' ]; - foreach ( $tabs_names as $tab_name ) { - $settings_value[ $tab_name ] = CatalogX()->setting->get_option( str_replace( '-', '_', 'catalogx_' . $tab_name . '_settings' ) ); - } + /** + * Redirct to pro shop url. + * + * @return never + */ + public function handle_external_redirects() { + wp_safe_redirect( esc_url_raw( CATALOGX_PRO_SHOP_URL ) ); + exit; + } + + /** + * Allow CatalogX domain for safe redirection using wp_safe_redirect(). + * + * @param string[] $hosts List of allowed hosts. + * @return string[] Modified list with CatalogX domain included. + */ + public function allow_catalogx_redirect_host( $hosts ) { + $parsed_url = wp_parse_url( CATALOGX_PRO_SHOP_URL ); - if ($current_user_role === 'administrator') { - $quote_base_url = admin_url('admin.php?page=wc-orders&action=edit&id='); - } elseif ($current_user_role === 'customer') { - $quote_base_url = site_url('/my-account/view-quote/'); - } else { - $quote_base_url = '/'; + if ( isset( $parsed_url['host'] ) ) { + $hosts[] = $parsed_url['host']; } - // Enque script and style - wp_enqueue_style('catalogx-style', CatalogX()->plugin_url . 'build/index.css'); - wp_enqueue_script('catalogx-script', CatalogX()->plugin_url . 'build/index.js', [ 'wp-element', 'wp-i18n', 'react-jsx-runtime' ], Catalogx()->version, true); - wp_set_script_translations( 'catalogx-script', 'catalogx' ); - - // Localize script - wp_localize_script( 'catalogx-script', 'appLocalizer', apply_filters( 'catalogx_settings', [ - 'apiurl' => untrailingslashit( get_rest_url() ), - 'nonce' => wp_create_nonce('wp_rest'), - 'tab_name' => "CatalogX", - 'restUrl' => CatalogX()->rest_namespace, - 'all_pages' => $all_pages, - 'role_array' => $all_roles, - 'all_users' => $all_users, - 'all_products' => $all_products, - 'all_product_cat' => $product_cat, - 'all_product_brand' => $all_product_brand, - 'all_product_tag' => $product_tags, - 'settings_databases_value' => $settings_value, - 'active_modules' => CatalogX()->modules->get_active_modules(), - 'user_role' => $current_user_role, - 'banner_img' => CatalogX()->plugin_url . 'assets/images/catalog-pro-add-admin-banner.jpg', - 'default_img' => CatalogX()->plugin_url . 'src/assets/images/default.png', - 'template1' => CatalogX()->plugin_url . 'assets/images/email/templates/catalogx-email-template-default.png', - 'template2' => CatalogX()->plugin_url . 'assets/images/email/templates/catalogx-email-template-1.png', - 'template3' => CatalogX()->plugin_url . 'assets/images/email/templates/catalogx-email-template-2.png', - 'template4' => CatalogX()->plugin_url . 'assets/images/email/templates/catalogx-email-template-3.png', - 'template5' => CatalogX()->plugin_url . 'assets/images/email/templates/catalogx-email-template-4.png', - 'template6' => CatalogX()->plugin_url . 'assets/images/email/templates/catalogx-email-template-5.png', - 'template7' => CatalogX()->plugin_url . 'assets/images/email/templates/catalogx-email-template-6.png', - 'khali_dabba' => Utill::is_khali_dabba(), - 'pro_url' => esc_url( CATALOGX_PRO_SHOP_URL ), - 'order_edit' => admin_url( "admin.php?page=wc-orders&action=edit" ), - 'site_url' => admin_url( 'admin.php?page=catalogx#&tab=settings&subtab=all-settings' ), - 'module_page_url' => admin_url( 'admin.php?page=catalogx#&tab=modules' ), - 'settings_page_url' => admin_url( 'admin.php?page=catalogx#&tab=settings&subtab=all-settings' ), - 'enquiry_form_settings_url' => admin_url( 'admin.php?page=catalogx#&tab=settings&subtab=enquiry-form-customization' ), - 'customization_settings_url' => admin_url( 'admin.php?page=catalogx#&tab=settings&subtab=enquiry-catalog-customization' ), - 'wholesale_settings_url' => admin_url( 'admin.php?page=catalogx#&tab=settings&subtab=wholesale' ), - 'rule_url' => admin_url( 'admin.php?page=catalogx#&tab=rules' ), - 'currency' => get_woocommerce_currency(), - 'notifima_active' => Utill::is_active_plugin('notifima'), - 'mvx_active' => Utill::is_active_plugin('multivendorx'), - 'quote_module_active' => CatalogX()->modules->is_active('quote'), - 'quote_base_url' => $quote_base_url - ])); + return $hosts; } - public function textdomain_relative_path($path, $url) { - if (strpos($url, 'woocommerce-catalog-enquiry') !== false) { - foreach (CatalogX()->block_paths as $key => $new_path) { - if (strpos($url, $key) !== false) { + /** + * Filters the relative path for the plugin's textdomain. + * + * This method can be used to adjust the location where translation files are loaded from. + * + * @param string $path Relative path to the .mo file. + * @param string $url URL to the .mo file. + * @return string Modified path. + */ + public function textdomain_relative_path( $path, $url ) { + if ( strpos( $url, 'woocommerce-catalog-enquiry' ) !== false ) { + foreach ( CatalogX()->block_paths as $key => $new_path ) { + if ( strpos( $url, $key ) !== false ) { $path = $new_path; } } - - if (strpos($url, 'block') === false) { + + if ( strpos( $url, 'block' ) === false ) { $path = 'build/index.js'; } } - + return $path; } - } diff --git a/classes/Block.php b/classes/Block.php index eecf86a..6794f8e 100644 --- a/classes/Block.php +++ b/classes/Block.php @@ -1,4 +1,9 @@ blocks = $this->initialize_blocks(); - // Register block category - add_filter( 'block_categories_all', [$this, 'register_block_category'] ); - // Register the block - add_action( 'init', [$this, 'register_blocks'] ); - // Localize the script for block - add_action( 'enqueue_block_assets', [ $this,'enqueue_all_block_assets'] ); - + // Register block category. + add_filter( 'block_categories_all', array( $this, 'register_block_category' ) ); + // Register the block. + add_action( 'init', array( $this, 'register_blocks' ) ); + // Localize the script for block. + add_action( 'enqueue_block_assets', array( $this, 'enqueue_all_block_assets' ) ); } - + + /** + * Initialize blocks based on active modules. + * + * @return array List of blocks with their configuration. + */ public function initialize_blocks() { - $blocks = []; + $blocks = array(); - if (CatalogX()->modules->is_active('enquiry')) { - $blocks[] = [ - 'name' => 'enquiry-button', // block name + if ( CatalogX()->modules->is_active( 'enquiry' ) ) { + $blocks[] = array( + 'name' => 'enquiry-button', // block name. 'textdomain' => 'catalogx', - 'block_path' => CatalogX()->plugin_path . 'build/blocks/', - ]; + 'block_path' => CatalogX()->plugin_path . FrontendScripts::get_build_path_name() . 'js/block/', + ); - //this path is set for load the translation - CatalogX()->block_paths += [ - 'blocks/enquiry-button' => 'build/blocks/enquiry-button/index.js', - 'blocks/enquiryForm' => 'build/blocks/enquiryForm/index.js', - ]; + // this path is set for load the translation. + CatalogX()->block_paths += array( + 'block/enquiry-button' => FrontendScripts::get_build_path_name() . 'js/block/enquiry-button/index.js', + 'block/enquiryForm' => FrontendScripts::get_build_path_name() . 'js/block/enquiryForm/index.js', + ); } - if (CatalogX()->modules->is_active('quote')) { - $blocks[] = [ - 'name' => 'quote-button', // block name + if ( CatalogX()->modules->is_active( 'quote' ) ) { + $blocks[] = array( + 'name' => 'quote-button', // block name. 'textdomain' => 'catalogx', - 'block_path' => CatalogX()->plugin_path . 'build/blocks/', - ]; + 'block_path' => CatalogX()->plugin_path . FrontendScripts::get_build_path_name() . 'js/block/', + ); - $blocks[] = [ - 'name' => 'quote-cart', // block name + $blocks[] = array( + 'name' => 'quote-cart', // block name. 'textdomain' => 'catalogx', - 'block_path' => CatalogX()->plugin_path . 'build/blocks/', - ]; + 'block_path' => CatalogX()->plugin_path . FrontendScripts::get_build_path_name() . 'js/block/', + ); - //this path is set for load the translation - CatalogX()->block_paths += [ - 'blocks/quote-cart' => 'build/blocks/quote-cart/index.js', - 'blocks/quote-button' => 'build/blocks/quote-button/index.js', - ]; + // this path is set for load the translation. + CatalogX()->block_paths += array( + 'block/quote-cart' => FrontendScripts::get_build_path_name() . 'js/block/quote-cart/index.js', + 'block/quote-button' => FrontendScripts::get_build_path_name() . 'js/block/quote-button/index.js', + ); } - return apply_filters('catalogx_initialize_blocks', $blocks); + return apply_filters( 'catalogx_initialize_blocks', $blocks ); } + /** + * Enqueue assets and localize scripts for all registered blocks. + * + * @return void + */ public function enqueue_all_block_assets() { FrontendScripts::load_scripts(); - foreach ($this->blocks as $block_script) { - FrontendScripts::localize_scripts($block_script['textdomain'] . '-' . $block_script['name'] . '-editor-script'); - FrontendScripts::localize_scripts($block_script['textdomain'] . '-' . $block_script['name'] . '-script'); + foreach ( $this->blocks as $block_script ) { + FrontendScripts::localize_scripts( $block_script['textdomain'] . '-' . $block_script['name'] . '-editor-script' ); + FrontendScripts::localize_scripts( $block_script['textdomain'] . '-' . $block_script['name'] . '-script' ); } } - public function register_block_category($categories) { + /** + * Register CatalogX block category in the block editor. + * + * @param array $categories Existing block categories. + * @return array Modified block categories. + */ + public function register_block_category( $categories ) { // Adding a new category. - $categories[] = [ + $categories[] = array( 'slug' => 'catalogx', - 'title' => 'CatalogX' - ]; + 'title' => 'CatalogX', + ); return $categories; } - + + /** + * Register all defined blocks. + * + * @return void + */ public function register_blocks() { - foreach ($this->blocks as $block) { - register_block_type( $block['block_path'] . $block['name']); + foreach ( $this->blocks as $block ) { + register_block_type( $block['block_path'] . $block['name'] ); } } - -} \ No newline at end of file +} diff --git a/classes/CatalogX.php b/classes/CatalogX.php index 1823c7d..b487a27 100644 --- a/classes/CatalogX.php +++ b/classes/CatalogX.php @@ -1,51 +1,85 @@ file = $file; - $this->container[ 'plugin_url' ] = trailingslashit( plugins_url( '', $plugin = $file ) ); - $this->container[ 'plugin_path' ] = trailingslashit( dirname( $file ) ); - $this->container[ 'version' ] = CATALOGX_PLUGIN_VERSION; - $this->container[ 'rest_namespace' ] = 'catalogx/v1'; - $this->container[ 'block_paths' ] = []; - $this->container[ 'admin_email' ] = get_option( 'admin_email' ); - $this->container[ 'render_enquiry_btn_via' ] = ''; - $this->container[ 'render_quote_btn_via' ] = ''; - - register_activation_hook( $file, [ $this, 'activate' ] ); - register_deactivation_hook( $file, [ $this, 'deactivate' ] ); - - add_action( 'before_woocommerce_init', [ $this, 'declare_compatibility' ] ); - add_action( 'woocommerce_loaded', [ $this, 'init_plugin' ] ); - add_action( 'plugins_loaded', [ $this, 'is_woocommerce_loaded'] ); - add_filter( 'woocommerce_email_classes', [ $this, 'load_emails' ] ); - - add_action( 'init', [ $this, 'migrate_from_previous' ] ); - // Major update notice - add_action( 'in_plugin_update_message-woocommerce-catalog-enquiry/Woocommerce_Catalog_Enquiry.php', [ $this, 'catalogx_plugin_update_message' ] ); + $this->file = $file; + $this->container['plugin_url'] = trailingslashit( plugins_url( '', $plugin = $file ) ); + $this->container['plugin_path'] = trailingslashit( dirname( $file ) ); + $this->container['plugin_base'] = plugin_basename( $file ); + $this->container['version'] = CATALOGX_PLUGIN_VERSION; + $this->container['rest_namespace'] = 'catalogx/v1'; + $this->container['block_paths'] = array(); + $this->container['admin_email'] = get_option( 'admin_email' ); + $this->container['render_enquiry_btn_via'] = ''; + $this->container['render_quote_btn_via'] = ''; + $this->container['is_dev'] = defined( 'WP_ENV' ) && WP_ENV === 'development'; + + register_activation_hook( $file, array( $this, 'activate' ) ); + register_deactivation_hook( $file, array( $this, 'deactivate' ) ); + + add_action( 'before_woocommerce_init', array( $this, 'declare_compatibility' ) ); + add_action( 'woocommerce_loaded', array( $this, 'init_plugin' ) ); + add_action( 'plugins_loaded', array( $this, 'is_woocommerce_loaded' ) ); + + add_action( 'init', array( $this, 'migrate_from_previous' ) ); + // Major update notice. + add_action( 'in_plugin_update_message-woocommerce-catalog-enquiry/Woocommerce_Catalog_Enquiry.php', array( $this, 'catalogx_plugin_update_message' ) ); } + /** + * Show a plugin update message on the plugin update screen for major versions. + * + * This is hooked to display a custom admin notice when the installed version is less than 6.0.0. + * + * @return void + */ public function catalogx_plugin_update_message() { if ( version_compare( get_option( 'catalogx_plugin_version' ), '6.0.0', '<' ) ) { echo '

Heads up! 6.0.0 is a major update. Make a full site backup and before upgrading your marketplace to avoid any undesirable situations.

'; @@ -55,61 +89,80 @@ public function catalogx_plugin_update_message() { /** * Add High Performance Order Storage Support + * * @return void */ public function declare_compatibility() { - FeaturesUtil::declare_compatibility ( 'custom_order_tables', WP_CONTENT_DIR.'/plugins/woocommerce-catalog-enquiry/Woocommerce_Catalog_Enquiry.php', true ); + FeaturesUtil::declare_compatibility( 'custom_order_tables', WP_CONTENT_DIR . '/plugins/woocommerce-catalog-enquiry/Woocommerce_Catalog_Enquiry.php', true ); } + /** + * Handle migration tasks for previous plugin versions. + * + * If the current plugin version is newer than the saved version, + * initiate the Install routine to perform necessary updates. + * + * @return void + */ public function migrate_from_previous() { - if ( version_compare( get_option( 'catalogx_plugin_version' ), '6.0.7', '<' ) ) { + if ( version_compare( get_option( 'catalogx_plugin_version' ), CatalogX()->version, '<' ) ) { new Install(); } } - + + /** + * Initialize the CatalogX plugin after WooCommerce is loaded. + * + * Loads text domain, hooks plugin links in admin, initializes core classes, + * registers plugin strings for translation, sets up wizard, and registers + * custom email classes. + * + * @return void + */ public function init_plugin() { $this->load_plugin_textdomain(); - - if (is_admin() && !defined('DOING_AJAX')) { - add_filter('plugin_action_links_' . plugin_basename($this->file), [ $this, 'plugin_link' ] ); - add_filter( 'plugin_row_meta', [ $this, 'plugin_row_meta'], 10, 2 ); + + if ( is_admin() && ! defined( 'DOING_AJAX' ) ) { + add_filter( 'plugin_action_links_' . plugin_basename( $this->file ), array( $this, 'plugin_link' ) ); + add_filter( 'plugin_row_meta', array( $this, 'plugin_row_meta' ), 10, 2 ); } $this->init_classes(); - - add_action( 'init', [ $this, 'catalogx_register_strings_and_setup_wizard' ] ); - + + add_action( 'init', array( $this, 'catalogx_register_strings_and_setup_wizard' ) ); + do_action( 'catalogx_loaded' ); + add_filter( 'woocommerce_email_classes', array( $this, 'load_emails' ) ); } - + /** * Load setup class and register string */ public function catalogx_register_strings_and_setup_wizard() { new SetupWizard(); - if (get_option('catalogx_plugin_activated')) { - delete_option('catalogx_plugin_activated'); - wp_redirect(admin_url('admin.php?page=catalogx-setup')); + if ( get_option( 'catalogx_plugin_activated' ) ) { + delete_option( 'catalogx_plugin_activated' ); + wp_safe_redirect( admin_url( 'admin.php?page=catalogx-setup' ) ); exit; } if ( ! function_exists( 'icl_register_string' ) ) { return; } - - $strings = [ - 'add_to_quote' => 'Add to Quote', - 'view_quote' => 'View Quote', + + $strings = array( + 'add_to_quote' => 'Add to Quote', + 'view_quote' => 'View Quote', 'send_an_enquiry' => 'Send an enquiry', - ]; - + ); + foreach ( $strings as $key => $value ) { icl_register_string( 'catalogx', $key, $value ); } - $form_settings = CatalogX()->setting->get_option('catalogx_enquiry_form_customization_settings'); + $form_settings = CatalogX()->setting->get_option( 'catalogx_enquiry_form_customization_settings' ); if ( function_exists( 'icl_register_string' ) ) { foreach ( $form_settings['formsettings']['formfieldlist'] as $field ) { @@ -125,7 +178,7 @@ public function catalogx_register_strings_and_setup_wizard() { } } } - + foreach ( $form_settings['freefromsetting'] as $free_field ) { if ( isset( $free_field['label'] ) ) { icl_register_string( 'catalogx', 'free_form_label_' . $free_field['key'], $free_field['label'] ); @@ -137,46 +190,76 @@ public function catalogx_register_strings_and_setup_wizard() { pll_register_string( 'my-quote', 'my-quote', 'catalogx' ); } - // Save the form settings to the options table + // Save the form settings to the options table. update_option( 'catalogx_enquiry_form_customization_settings', $form_settings ); } - //load all classes + /** + * Initialize and load all core plugin classes into the container. + * + * This includes settings, admin/backend logic, frontend handling, + * REST API endpoints, utilities, modules, shortcodes, session management, + * quote cart logic, Gutenberg blocks, and frontend scripts. + * + * @return void + */ public function init_classes() { - $this->container['setting'] = new Setting(); - $this->container['admin'] = new Admin(); - $this->container['frontend'] = new Frontend(); - $this->container['restapi'] = new Rest(); - $this->container['util'] = new Utill(); - $this->container['modules'] = new Modules(); - $this->container['shortcode'] = new Shortcode(); - $this->container['session'] = new Core\Session(); - $this->container['quotecart'] = new Core\QuoteCart(); - - // Load all active modules + $this->container['setting'] = new Setting(); + $this->container['admin'] = new Admin(); + $this->container['frontend'] = new Frontend(); + $this->container['restapi'] = new Rest(); + $this->container['util'] = new Utill(); + $this->container['modules'] = new Modules(); + $this->container['shortcode'] = new Shortcode(); + $this->container['session'] = new Core\Session(); + $this->container['quotecart'] = new Core\QuoteCart(); + + // Load all active modules. $this->container['modules']->load_active_modules(); - - $this->container['block'] = new Block(); + + $this->container['block'] = new Block(); $this->container['frontendscripts'] = new FrontendScripts(); } - public function plugin_link( $links ) { - $plugin_links = [ + /** + * Add custom action links to the plugin on the plugins page. + * + * Adds "Settings", "Support", and optionally "Upgrade to Pro" links. + * + * @param array $links Existing plugin action links. + * @return array Modified plugin action links with CatalogX links. + */ + public function plugin_link( $links ) { + $plugin_links = array( '' . __( 'Settings', 'catalogx' ) . '', - '' . __( 'Support', 'catalogx' ) . '', - ]; - $links = array_merge( $plugin_links, $links ); - if ( apply_filters( 'catalogx_free_active', true ) ) { - $links[] = '' . __( 'Upgrade to Pro', 'catalogx' ) . ''; + ); + $links = array_merge( $plugin_links, $links ); + if ( ! Utill::is_khali_dabba() ) { + $links[] = '' . __( 'Upgrade to Pro', 'catalogx' ) . ''; } return $links; } + /** + * Add custom metadata links below the plugin description on the Plugins page. + * + * This typically shows small links like "Upgrade to Pro" in the plugin row footer. + * + * @param array $links An array of the existing plugin metadata links. + * @param string $file Path to the plugin file relative to the plugins directory. + * @return array Modified array of plugin metadata links. + */ public function plugin_row_meta( $links, $file ) { - if ( $file == 'woocommerce-catalog-enquiry/Woocommerce_Catalog_Enquiry.php' && apply_filters( 'catalogx_free_active', true ) ) { - $row_meta = [ - 'pro' => '' . __( 'Upgrade to Pro', 'catalogx' ) . '' - ]; + if ( CatalogX()->plugin_base === $file ) { + $row_meta = array( + 'docs' => '' . esc_html__( 'Docs', 'catalogx' ) . '', + 'support' => '' . __( 'Support', 'catalogx' ) . '', + ); + + if ( ! Utill::is_khali_dabba() ) { + $row_meta['pro'] = '' . __( 'Upgrade to Pro', 'catalogx' ) . ''; + } + return array_merge( $links, $row_meta ); } else { return $links; @@ -185,84 +268,93 @@ public function plugin_row_meta( $links, $file ) { /** * Take action based on if woocommerce is not loaded + * * @return void */ public function is_woocommerce_loaded() { if ( ! did_action( 'woocommerce_loaded' ) && is_admin() ) { - add_action( 'admin_notices', [ $this , 'woocommerce_admin_notice' ] ); + add_action( 'admin_notices', array( $this, 'woocommerce_admin_notice' ) ); } } /** * Display Woocommerce inactive notice. + * * @return void */ public function woocommerce_admin_notice() { ?>

- tag, 2. Closing tag, 3. Opening tag for WooCommerce plugin link, 4. Closing tag, 5. Opening tag for plugin activation link, 6. Closing tag */ - __('%1$sCatalogX is inactive.%2$s The %3$sWooCommerce plugin%4$s must be active for CatalogX to work. Please %5$sinstall & activate WooCommerce%6$s', 'catalogx'), - '', '', - '', '', - '', ' »' - ); + esc_html__( '%1$sCatalogX is inactive.%2$s The %3$sWooCommerce plugin%4$s must be active for CatalogX to work. Please %5$sinstall & activate WooCommerce%6$s', 'catalogx' ), + '', + '', + '', + '', + '', + ' »' + ); ?>

container['install'] = new Install(); - if (!get_option('catalogx_plugin_installed')) { - add_option('catalogx_plugin_installed', true); - add_option('catalogx_plugin_activated', true); + if ( ! get_option( 'catalogx_plugin_installed' ) ) { + add_option( 'catalogx_plugin_installed', true ); + add_option( 'catalogx_plugin_activated', true ); } - if (!get_option('catalogx_tour_active')) { - add_option('catalogx_tour_active', true); - add_option('catalogx_tour_version', CatalogX()->version); + if ( ! get_option( 'catalogx_tour_active' ) ) { + add_option( 'catalogx_tour_active', true ); + add_option( 'catalogx_tour_version', CatalogX()->version ); } flush_rewrite_rules(); ob_end_clean(); } /** - *Catalog enquery deactivation function. + * Catalog enquery deactivation function. + * * @return void */ public function deactivate() { - // Nothing to do write now + // Nothing to do write now. } /** * Load Localisation files. * Note: the first-loaded translation file overrides any following ones if the same translation is present + * * @return void */ public function load_plugin_textdomain() { if ( version_compare( $GLOBALS['wp_version'], '6.7', '<' ) ) { - load_plugin_textdomain('catalogx', false, plugin_basename(dirname(dirname(__FILE__))) . '/languages'); + load_plugin_textdomain( 'catalogx', false, plugin_basename( dirname( __DIR__ ) ) . '/languages' ); } else { load_textdomain( 'catalogx', WP_LANG_DIR . '/plugins/catalogx-' . determine_locale() . '.mo' ); } @@ -270,15 +362,16 @@ public function load_plugin_textdomain() { /** * Magic getter function to get the reference of class. - * Accept class name, If valid return reference, else Wp_Error. - * @param mixed $class + * Accept class name, If valid return reference, else Wp_Error. + * + * @param mixed $class The key name of the class to retrieve from the container. * @return object | \WP_Error */ - public function __get( $class ) { + public function __get( $class ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.classFound if ( array_key_exists( $class, $this->container ) ) { return $this->container[ $class ]; } - return new \WP_Error( sprintf('Call to unknown class %s.', $class ) ); + return new \WP_Error( sprintf( 'Call to unknown class %s.', $class ) ); } /** @@ -288,7 +381,7 @@ public function __get( $class ) { * @param string $class The class name or key to store the instance. * @param object $value The instance of the class to store. */ - public function __set( $class, $value ) { + public function __set( $class, $value ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.classFound $this->container[ $class ] = $value; } @@ -296,11 +389,12 @@ public function __set( $class, $value ) { * Initializes the catalog enquiry class. * Checks for an existing instance * And if it doesn't find one, create it. - * @param mixed $file + * + * @param mixed $file The full path to the plugin's main file. * @return object | null */ public static function init( $file ) { - if ( self::$instance === null ) { + if ( null === self::$instance ) { self::$instance = new self( $file ); } diff --git a/classes/Core/QuoteCart.php b/classes/Core/QuoteCart.php index 23a3413..b1db796 100644 --- a/classes/Core/QuoteCart.php +++ b/classes/Core/QuoteCart.php @@ -1,21 +1,46 @@ session = new Session(); $this->set_session(); } - function init_callback() { + /** + * Initializes necessary components when WordPress has loaded. + * + * @return void + */ + public function init_callback() { $this->get_quote_cart_session(); - $this->session->set_customer_session_cookie(true); + $this->session->set_customer_session_cookie( true ); $this->quote_cron_schedule(); } - function get_quote_cart_session() { + /** + * Retrieves the current quote cart session data. + * + * @return array + */ + public function get_quote_cart_session() { $this->quote_cart_content = $this->session->get( 'quote_cart', array() ); return $this->quote_cart_content; } - public function quote_cron_schedule(){ - - if ( !wp_next_scheduled( 'quote_clean_cron' ) ) { + /** + * Schedules the cron event to clean quote cart data hourly. + * + * @return void + */ + public function quote_cron_schedule() { + if ( ! wp_next_scheduled( 'quote_clean_cron' ) ) { wp_schedule_event( time(), 'hourly', 'quote_clean_cron' ); } } - public function clean_session(){ + /** + * Clean all CatalogX session data from the options table. + * + * Deletes all options where the option_name starts with '_catalogx_session_'. + * + * @return void + */ + public function clean_session() { global $wpdb; - $query = $wpdb->query("DELETE FROM ". $wpdb->prefix ."options WHERE option_name LIKE '_catalogx_session_%'"); + + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching + $query = $wpdb->query( 'DELETE FROM ' . $wpdb->prefix . "options WHERE option_name LIKE '_catalogx_session_%'" ); } /** - * Sets the php session data for the enquiry cart. + * Sets the PHP session data for the enquiry cart. + * + * @param array $cart_session The session data to set for the quote cart. + * @param bool $can_be_empty Whether the session can be set to an empty array. + * + * @return void */ - public function set_session($cart_session = array(), $can_be_empty = false) { + public function set_session( $cart_session = array(), $can_be_empty = false ) { - if ( empty( $cart_session ) && !$can_be_empty) { + if ( empty( $cart_session ) && ! $can_be_empty ) { $cart_session = $this->get_quote_cart_session(); } - // Set quote_cart session data + // Set quote_cart session data. $this->session->set( 'quote_cart', $cart_session ); } + /** + * Unsets the quote cart session data. + * + * @return void + */ public function unset_session() { $this->session->__unset( 'quote_cart' ); } - function maybe_set_cart_cookies() { + /** + * Conditionally sets or unsets cart cookies. + * + * @return void + */ + public function maybe_set_cart_cookies() { $set = true; - if ( !headers_sent() ) { + if ( ! headers_sent() ) { if ( sizeof( $this->quote_cart_content ) > 0 ) { $this->set_cart_cookies( true ); $set = true; - } - elseif ( isset( $_COOKIE['quote_items_in_cart'] ) ) { + } elseif ( isset( $_COOKIE['quote_items_in_cart'] ) ) { $this->set_cart_cookies( false ); $set = false; } @@ -99,48 +161,61 @@ function maybe_set_cart_cookies() { do_action( 'quote_set_cart_cookies', $set ); } + /** + * Set or clear quote cart cookies. + * + * @param bool $set Whether to set (true) or clear (false) the cart cookies. + * @return void + */ private function set_cart_cookies( $set = true ) { if ( $set ) { wc_setcookie( 'quote_items_in_cart', 1 ); wc_setcookie( 'quote_hash', md5( json_encode( $this->quote_cart_content ) ) ); - } - elseif ( isset( $_COOKIE['quote_items_in_cart'] ) ) { + } elseif ( isset( $_COOKIE['quote_items_in_cart'] ) ) { wc_setcookie( 'quote_items_in_cart', 0, time() - HOUR_IN_SECONDS ); wc_setcookie( 'quote_hash', '', time() - HOUR_IN_SECONDS ); } } + /** + * Handles the "Add to Quote" action. + * + * Retrieves product ID, variation ID, and quantity from the query string, + * prepares the request data, and adds the item to the quote cart. + * + * @return void + */ public function add_to_quote_action() { $add_to_quote = filter_input( INPUT_GET, 'add-to-quote', FILTER_SANITIZE_NUMBER_INT ); if ( ! $add_to_quote ) { return; } - - $product_id = absint( $add_to_quote ); - $variation_id = filter_input( INPUT_GET, 'variation_id', FILTER_SANITIZE_NUMBER_INT ) ?: ''; - $quantity = filter_input( INPUT_GET, 'quantity', FILTER_SANITIZE_NUMBER_INT ); - $quantity = empty( $quantity ) ? 1 : wc_stock_amount( intval( $quantity ) ); - + + $product_id = absint( $add_to_quote ); + $variation_id = filter_input( INPUT_GET, 'variation_id', FILTER_SANITIZE_NUMBER_INT ) ?: ''; + $quantity = filter_input( INPUT_GET, 'quantity', FILTER_SANITIZE_NUMBER_INT ); + $quantity = empty( $quantity ) ? 1 : wc_stock_amount( intval( $quantity ) ); + $adding_to_quote = wc_get_product( $product_id ); - + if ( ! $adding_to_quote ) { return; } - + $raq_data = array(); - + if ( $adding_to_quote->is_type( 'variable' ) && $variation_id ) { $variation = wc_get_product( $variation_id ); $attributes = $variation->get_attributes(); - + if ( ! empty( $attributes ) ) { foreach ( $attributes as $name => $value ) { $raq_data[ 'attribute_' . $name ] = $value; } } } - - // Merge request data into array + + // Merge request data into array. $raq_data = array_merge( array( 'product_id' => $product_id, @@ -149,40 +224,47 @@ public function add_to_quote_action() { ), $raq_data ); - - // Add item to quote cart + + // Add item to quote cart. $return = $this->add_cart_item( $raq_data ); - - // Handle response messages + + // Handle response messages. if ( 'true' === $return ) { wc_add_notice( 'product_added', 'success' ); } elseif ( 'exists' === $return ) { wc_add_notice( 'already_in_quote', 'notice' ); } } - + /** + * Add a product to the quote cart. + * + * Checks if the product already exists in the cart. If not, adds it to the session. + * + * @param array $cart_data The data of the product being added to the quote cart. + * Must include 'product_id', 'variation' (array), and optionally 'quantity'. + * @return string Returns 'true' on success, or 'exists' if the item is already in the quote cart. + */ public function add_cart_item( $cart_data ) { $cart_data['quantity'] = ( isset( $cart_data['quantity'] ) ) ? (int) $cart_data['quantity'] : 1; - $return = ''; - + $return = ''; + do_action( 'catalogx_add_to_quote_cart', $cart_data ); - - if ( !$this->exists_in_cart( $cart_data['product_id'] ) ) { + + if ( ! $this->exists_in_cart( $cart_data['product_id'] ) ) { $enquiry = array( - 'product_id' => $cart_data['product_id'], - 'variation' => $cart_data['variation'], - 'quantity' => $cart_data['quantity'] + 'product_id' => $cart_data['product_id'], + 'variation' => $cart_data['variation'], + 'quantity' => $cart_data['quantity'], ); - $this->quote_cart_content[md5( $cart_data['product_id'] )] = $enquiry; - } - else { + $this->quote_cart_content[ md5( $cart_data['product_id'] ) ] = $enquiry; + } else { $return = 'exists'; } - if ( $return != 'exists' ) { + if ( 'exists' != $return ) { $this->set_session( $this->quote_cart_content ); $return = 'true'; $this->set_cart_cookies( sizeof( $this->quote_cart_content ) > 0 ); @@ -190,6 +272,13 @@ public function add_cart_item( $cart_data ) { return $return; } + /** + * Check if a product (and optional variation) already exists in the quote cart. + * + * @param int $product_id The ID of the product to check. + * @param int|false $variation_id Optional. The variation ID of the product. Default false. + * @return bool True if the product (and variation, if provided) exists in the cart, false otherwise. + */ public function exists_in_cart( $product_id, $variation_id = false ) { if ( $variation_id ) { $key_to_find = md5( $product_id . $variation_id ); @@ -203,53 +292,84 @@ public function exists_in_cart( $product_id, $variation_id = false ) { return false; } + /** + * Retrieve all data from the quote cart session. + * + * @return array The quote cart content. + */ public function get_cart_data() { return $this->quote_cart_content; } + /** + * Retrieve the URL of the request quote page. + * + * @return string + */ public function get_request_quote_page_url() { $catalogx_quote_page_id = get_option( 'catalogx_request_quote_page' ); - $translated_id = function_exists( 'pll_get_post' ) ? pll_get_post( $catalogx_quote_page_id ) : $catalogx_quote_page_id; - $base_url = get_the_permalink( $translated_id ); + $translated_id = function_exists( 'pll_get_post' ) ? pll_get_post( $catalogx_quote_page_id ) : $catalogx_quote_page_id; + $base_url = get_the_permalink( $translated_id ); return apply_filters( 'catalogx_request_quote_page_url', $base_url ); } + /** + * Check if the quote cart is empty. + * + * @return bool True if the quote cart is empty, false otherwise. + */ public function is_empty_cart() { return empty( $this->quote_cart_content ); } + /** + * Remove an item from the quote cart by its key. + * + * @param string $key The cart item key to remove. + * @return bool True if the item was removed, false if it was not found. + */ public function remove_cart( $key ) { - if ( isset( $this->quote_cart_content[$key] ) ) { - unset( $this->quote_cart_content[$key] ); + if ( isset( $this->quote_cart_content[ $key ] ) ) { + unset( $this->quote_cart_content[ $key ] ); $this->set_session( $this->quote_cart_content, true ); return true; - } - else { + } else { return false; } } + /** + * Clear all items from the quote cart. + * + * @return void + */ public function clear_cart() { $this->quote_cart_content = array(); $this->set_session( $this->quote_cart_content, true ); } + /** + * Update an item in the quote cart. + * + * @param string $key The cart item key (usually a hashed product ID). + * @param string|bool $field Optional. The specific field of the item to update. If false, replaces the entire item. + * @param mixed $value The new value for the field or item. + * + * @return bool True on success, false on failure. + */ public function update_cart( $key, $field = false, $value = '' ) { - if ( $field && isset( $this->quote_cart_content[$key][$field] ) ) { - $this->quote_cart_content[$key][$field] = $value; + if ( $field && isset( $this->quote_cart_content[ $key ][ $field ] ) ) { + $this->quote_cart_content[ $key ][ $field ] = $value; $this->set_session( $this->quote_cart_content ); - } - elseif ( isset( $this->quote_cart_content[$key] ) ) { - $this->quote_cart_content[$key] = $value; + } elseif ( isset( $this->quote_cart_content[ $key ] ) ) { + $this->quote_cart_content[ $key ] = $value; $this->set_session( $this->quote_cart_content ); - } - else { + } else { return false; } $this->set_session( $this->quote_cart_content ); return true; } - -} \ No newline at end of file +} diff --git a/classes/Core/Session.php b/classes/Core/Session.php index 4afdd3f..a013462 100644 --- a/classes/Core/Session.php +++ b/classes/Core/Session.php @@ -1,4 +1,10 @@ plugin_path().'/includes/abstracts/abstract-wc-session.php'); + include_once WC()->plugin_path() . '/includes/abstracts/abstract-wc-session.php'; } /** @@ -15,18 +21,34 @@ * * WC_Session. */ -class Session extends \WC_Session { +class Session extends \WC_Session { - /** @var string cookie name */ + /** + * Cookie name + * + * @var string + */ private $_cookie; - /** @var string session due to expire timestamp */ + /** + * Session due to expire timestamp + * + * @var string + */ private $_session_expiring; - /** @var string session expiration timestamp */ + /** + * Session expiration timestamp + * + * @var string + */ private $_session_expiration; - /** $var bool Bool based on whether a cookie exists **/ + /** + * Bool based on whether a cookie exists + * + * @var bool + */ private $_has_cookie = false; /** @@ -34,12 +56,13 @@ class Session extends \WC_Session { */ public function __construct() { global $wpdb; - if ( !defined( 'COOKIEHASH' ) ) { + if ( ! defined( 'COOKIEHASH' ) ) { $siteurl = get_site_option( 'siteurl' ); - if ( $siteurl ) + if ( $siteurl ) { define( 'COOKIEHASH', md5( $siteurl ) ); - else - define( 'COOKIEHASH', md5( wp_guess_url() ) ); + } else { + define( 'COOKIEHASH', md5( wp_guess_url() ) ); + } } $this->_cookie = 'catalogx_session_' . COOKIEHASH; @@ -50,7 +73,7 @@ public function __construct() { $this->_session_expiring = $cookie[2]; $this->_has_cookie = true; - // Update session if its close to expiring + // Update session if its close to expiring. if ( time() > $this->_session_expiring ) { $this->set_session_expiration(); $session_expiry_option = 'catalogx_session_expires_' . $this->_customer_id; @@ -61,16 +84,14 @@ public function __construct() { update_option( $session_expiry_option, $this->_session_expiration ); } } - } else { $this->set_session_expiration(); $this->_customer_id = $this->generate_customer_id(); } - $this->_data = $this->get_session_data(); - // Actions + // Actions. add_action( 'woocommerce_cleanup_sessions', array( $this, 'cleanup_sessions' ), 10 ); add_action( 'shutdown', array( $this, 'save_data' ), 20 ); add_action( 'wp_logout', array( $this, 'destroy_session' ) ); @@ -86,16 +107,18 @@ public function __construct() { * Since the cookie name (as of 2.1) is prepended with wp, cache systems like batcache will not cache pages when set. * * Warning: Cookies will only be set if this is called before the headers are sent. + * + * @param bool $set Whether to set the session cookie. */ public function set_customer_session_cookie( $set ) { if ( $set ) { - // Set/renew our cookie + // Set/renew our cookie. $to_hash = $this->_customer_id . '|' . $this->_session_expiration; $cookie_hash = hash_hmac( 'md5', $to_hash, wp_hash( $to_hash ) ); $cookie_value = $this->_customer_id . '||' . $this->_session_expiration . '||' . $this->_session_expiring . '||' . $cookie_hash; $this->_has_cookie = true; - // Set the cookie + // Set the cookie. wc_setcookie( $this->_cookie, $cookie_value, $this->_session_expiration, apply_filters( 'catalogx_session_use_secure_cookie', false ) ); } } @@ -128,7 +151,7 @@ public function generate_customer_id() { if ( is_user_logged_in() ) { return get_current_user_id(); } else { - require_once( ABSPATH . 'wp-includes/class-phpass.php'); + require_once ABSPATH . 'wp-includes/class-phpass.php'; $hasher = new \PasswordHash( 8, false ); return md5( $hasher->get_random_bytes( 32 ) ); } @@ -146,7 +169,7 @@ public function get_session_cookie() { list( $customer_id, $session_expiration, $session_expiring, $cookie_hash ) = explode( '||', $_COOKIE[ $this->_cookie ] ); - // Validate hash + // Validate hash. $to_hash = $customer_id . '|' . $session_expiration; $hash = hash_hmac( 'md5', $to_hash, wp_hash( $to_hash ) ); @@ -170,9 +193,8 @@ public function get_session_data() { * Save data. */ public function save_data() { - // Dirty if something changed - prevents saving nothing new + // Dirty if something changed - prevents saving nothing new. if ( $this->_dirty && $this->has_session() ) { - $session_option = '_catalogx_session_' . $this->_customer_id; $session_expiry_option = '_catalogx_session_expires_' . $this->_customer_id; @@ -189,12 +211,12 @@ public function save_data() { * Destroy all session data. */ public function destroy_session() { - // Clear cookie + // Clear cookie. wc_setcookie( $this->_cookie, '', time() - YEAR_IN_SECONDS, apply_filters( 'catalogx_session_use_secure_cookie', false ) ); $this->delete_session( $this->_customer_id ); - // Clear data + // Clear data. $this->_data = array(); $this->_dirty = false; $this->_customer_id = $this->generate_customer_id(); @@ -207,29 +229,30 @@ public function cleanup_sessions() { global $wpdb; if ( ! defined( 'WP_SETUP_CONFIG' ) && ! defined( 'WP_INSTALLING' ) ) { - - $now = time(); - $expired_sessions = array(); + $now = time(); + $expired_sessions = array(); + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared $wc_session_expires = $wpdb->get_col( "SELECT option_name FROM $wpdb->options WHERE option_name LIKE '\_wcce\_session\_expires\_%' AND option_value < '$now'" ); foreach ( $wc_session_expires as $option_name ) { $session_id = substr( $option_name, 20 ); - $expired_sessions[] = $option_name; // Expires key - $expired_sessions[] = "_catalogx_session_$session_id"; // Session key + $expired_sessions[] = $option_name; // Expires key. + $expired_sessions[] = "_catalogx_session_$session_id"; // Session key. } if ( ! empty( $expired_sessions ) ) { $expired_sessions_chunked = array_chunk( $expired_sessions, 100 ); foreach ( $expired_sessions_chunked as $chunk ) { if ( wp_using_ext_object_cache() ) { - // delete from object cache first, to avoid cached but deleted options + // delete from object cache first, to avoid cached but deleted options. foreach ( $chunk as $option ) { wp_cache_delete( $option, 'options' ); } } - // delete from options table + // delete from options table. $option_names = implode( "','", $chunk ); + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name IN ('$option_names')" ); } } @@ -239,8 +262,8 @@ public function cleanup_sessions() { /** * Returns the session. * - * @param string $customer_id - * @param mixed $default + * @param string $customer_id The unique identifier for the customer. + * @param mixed $default The default value to return if no session is found. * @return string|array */ public function get_session( $customer_id, $default = false ) { @@ -250,20 +273,20 @@ public function get_session( $customer_id, $default = false ) { return false; } - return get_option( '_catalogx_session_' . $customer_id); + return get_option( '_catalogx_session_' . $customer_id ); } /** * Delete the session from the cache and database. * - * @param int $customer_id + * @param int $customer_id The ID of the customer whose session should be deleted. */ public function delete_session( $customer_id ) { - // Delete session + // Delete session. $session_option = '_catalogx_session_' . $customer_id; $session_expiry_option = '_catalogx_session_expires_' . $customer_id; delete_option( $session_option ); delete_option( $session_expiry_option ); } -} \ No newline at end of file +} diff --git a/classes/Emails/EnquiryEmail.php b/classes/Emails/EnquiryEmail.php index eab8991..9f886f7 100644 --- a/classes/Emails/EnquiryEmail.php +++ b/classes/Emails/EnquiryEmail.php @@ -1,6 +1,12 @@ id = 'catalogx_enquiry_sent'; $this->title = __( 'Enquiry sent', 'catalogx' ); $this->description = __( 'Admin will get an email when a customer enquires about a product.', 'catalogx' ); - - $this->initialize_templates(); - - // Call parent constructor + // Default values. + $defaults = array( + 'email_setting' => '', + 'template_map' => array( + 'template1' => 'emails/default-enquiry-template.php', + 'template2' => 'emails/enquiry-template1.php', + 'template3' => 'emails/enquiry-template2.php', + 'template4' => 'emails/enquiry-template3.php', + 'template5' => 'emails/enquiry-template4.php', + 'template6' => 'emails/enquiry-template5.php', + 'template7' => 'emails/enquiry-template6.php', + ), + 'base_path' => CatalogX()->plugin_path . 'templates/', + 'plain_template' => 'emails/plain/enquiry-email.php', + 'default_html' => 'emails/enquiry-email.php', + 'template_loader' => CatalogX()->util, + ); + $this->args = apply_filters( 'catalogx_enquiry_email_template', $defaults ); + // Set the appropriate template paths. + $this->template_loader = $this->args['template_loader']; + $this->template_html = $this->args['template_map'][ $this->args['email_setting'] ] ?? $this->args['default_html']; + $this->template_plain = $this->args['plain_template']; + $this->template_base = $this->args['base_path']; + // Call parent constructor. parent::__construct(); } - protected function initialize_templates() { - // Determine the base template path based on Pro status and email setting - $is_khali_dabba = Utill::is_khali_dabba(); - $email_setting = $is_khali_dabba ? CatalogX()->setting->get_setting('selected_email_tpl') : ''; - - // Use Pro template path if Pro is active and email setting is not empty, otherwise fallback to default path - $base_template_path = ($is_khali_dabba && !empty($email_setting)) - ? CatalogX_Pro()->plugin_path . 'templates/' - : CatalogX()->plugin_path . 'templates/'; - - // Define available email templates - $template_map = [ - 'template1' => 'emails/default-enquiry-template.php', - 'template2' => 'emails/enquiry-template1.php', - 'template3' => 'emails/enquiry-template2.php', - 'template4' => 'emails/enquiry-template3.php', - 'template5' => 'emails/enquiry-template4.php', - 'template6' => 'emails/enquiry-template5.php', - 'template7' => 'emails/enquiry-template6.php', - ]; - - // Set the appropriate template paths - $this->template_html = $template_map[$email_setting] ?? 'emails/enquiry-email.php'; - $this->template_plain = $is_khali_dabba ? 'emails/plain/enquiry-email-plain.php' : 'emails/plain/enquiry-email.php'; - $this->template_base = $base_template_path; - } - - /** * Trigger the email. + * + * @param string $recipient The primary recipient email address. + * @param array $enquiry_data Associative array containing enquiry details. + * @param array $attachments List of file paths to attach to the email. + * + * @return bool Whether the email was sent successfully. */ - public function trigger($recipient, $enquiry_data, $attachments) { - $this->recipient = $recipient; - $this->attachments = $attachments; - $this->product_id = $enquiry_data['product_id']; - $this->enquiry_data = $enquiry_data; - $this->cust_name = $enquiry_data['user_name']; - $this->cust_email = $enquiry_data['user_email']; + public function trigger( $recipient, $enquiry_data, $attachments ) { + $this->recipient = $recipient; + $this->attachments = $attachments; + $this->product_id = $enquiry_data['product_id']; + $this->enquiry_data = $enquiry_data; + $this->cust_name = $enquiry_data['user_name']; + $this->cust_email = $enquiry_data['user_email']; $this->customer_email = $this->cust_email; - if (!$this->is_enabled() || !$this->get_recipient()) { + if ( ! $this->is_enabled() || ! $this->get_recipient() ) { return false; } $this->add_vendor_emails(); - $product = wc_get_product(key($this->product_id)); - $this->find = ['{PRODUCT_NAME}', '{USER_NAME}']; - $this->replace = [ - is_array($this->product_id) && count($this->product_id) > 1 ? 'MULTIPLE PRODUCTS' : $product->get_title(), - $this->cust_name - ]; + $product = wc_get_product( key( $this->product_id ) ); + $this->find = array( '{PRODUCT_NAME}', '{USER_NAME}' ); + $this->replace = array( + is_array( $this->product_id ) && count( $this->product_id ) > 1 ? 'MULTIPLE PRODUCTS' : $product->get_title(), + $this->cust_name, + ); - return $this->send($this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments()); + return $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() ); } /** * Add vendor emails to the recipient list. */ protected function add_vendor_emails() { - if (!Utill::is_active_plugin('multivendorx')) return; + if ( ! Utill::is_active_plugin( 'multivendorx' ) ) { + return; + } - foreach ($this->product_id as $product_id => $quantity) { - $vendor = function_exists('get_mvx_product_vendors') ? get_mvx_product_vendors($product_id) : null; + foreach ( $this->product_id as $product_id => $quantity ) { + $vendor = function_exists( 'get_mvx_product_vendors' ) ? get_mvx_product_vendors( $product_id ) : null; - if ($vendor) { - $vendor_email = sanitize_email($vendor->user_data->user_email); + if ( $vendor ) { + $vendor_email = sanitize_email( $vendor->user_data->user_email ); $this->recipient .= ', ' . $vendor_email; - if (strpos($this->recipient, $vendor_email) !== false) { - $email_setting = get_user_meta($vendor->id, 'vendor_enquiry_settings', true)['selected_email_tpl'] ?? ''; - $this->template_html = $this->get_vendor_template($email_setting); + if ( strpos( $this->recipient, $vendor_email ) !== false ) { + $email_setting = get_user_meta( $vendor->id, 'vendor_enquiry_settings', true )['selected_email_tpl'] ?? ''; + $this->template_html = $this->args['template_map'][ $email_setting ] ?? $this->args['default_html']; } } } } - /** - * Get the appropriate template for the vendor. - */ - protected function get_vendor_template($email_setting) { - $template_map = [ - 'template1' => 'emails/default-enquiry-template.php', - 'template2' => 'emails/enquiry-template1.php', - 'template3' => 'emails/enquiry-template2.php', - 'template4' => 'emails/enquiry-template3.php', - 'template5' => 'emails/enquiry-template4.php', - 'template6' => 'emails/enquiry-template5.php', - 'template7' => 'emails/enquiry-template6.php' - ]; - - return $template_map[$email_setting] ?? 'emails/enquiry-email.php'; - } - /** * Get email subject. */ public function get_default_subject() { - return apply_filters('catalogx_enquiry_admin_email_subject', __('Product Enquiry for {PRODUCT_NAME} by {USER_NAME}', 'catalogx'), $this->object); + return apply_filters( 'catalogx_enquiry_admin_email_subject', __( 'Product Enquiry for {PRODUCT_NAME} by {USER_NAME}', 'catalogx' ), $this->object ); } /** * Get email heading. */ public function get_default_heading() { - return apply_filters('catalogx_enquiry_admin_email_heading', __('Enquiry for {PRODUCT_NAME}', 'catalogx'), $this->object); + return apply_filters( 'catalogx_enquiry_admin_email_heading', __( 'Enquiry for {PRODUCT_NAME}', 'catalogx' ), $this->object ); } /** * Get email attachments. */ public function get_attachments() { - return apply_filters('catalogx_enquiry_admin_email_attachments', $this->attachments, $this->id, $this->object); + return apply_filters( 'catalogx_enquiry_admin_email_attachments', $this->attachments, $this->id, $this->object ); } /** * Get email headers. */ public function get_headers() { - $header = "Content-Type: " . $this->get_content_type() . "\r\n"; + $header = 'Content-Type: ' . $this->get_content_type() . "\r\n"; $header .= 'Reply-to: ' . $this->cust_name . ' <' . $this->cust_email . ">\r\n"; - return apply_filters('catalogx_enquiry_admin_email_headers', $header, $this->id, $this->object); + return apply_filters( 'catalogx_enquiry_admin_email_headers', $header, $this->id, $this->object ); } /** @@ -163,11 +188,7 @@ public function get_headers() { */ public function get_content_html() { ob_start(); - if (Utill::is_khali_dabba() && $this->template_html !== 'emails/enquiry-email.php') { - CatalogX_Pro()->utill->get_template($this->template_html, $this->get_template_args()); - } else { - CatalogX()->util->get_template($this->template_html, $this->get_template_args()); - } + $this->template_loader->get_template( $this->template_html, $this->get_template_args() ); return ob_get_clean(); } @@ -176,11 +197,7 @@ public function get_content_html() { */ public function get_content_plain() { ob_start(); - if (Utill::is_khali_dabba() && !empty(CatalogX()->setting->get_setting('selected_email_tpl'))) { - CatalogX_Pro()->utill->get_template($this->template_plain, $this->get_template_args()); - } else { - CatalogX()->util->get_template($this->template_plain, $this->get_template_args()); - } + $this->template_loader->get_template( $this->template_plain, $this->get_template_args() ); return ob_get_clean(); } @@ -188,13 +205,13 @@ public function get_content_plain() { * Get template arguments. */ protected function get_template_args() { - return [ - 'email_heading' => $this->get_heading(), - 'product_id' => $this->product_id, - 'enquiry_data' => $this->enquiry_data, - 'customer_email' => $this->customer_email, - 'sent_to_admin' => true, - 'plain_text' => false - ]; + return array( + 'email_heading' => $this->get_heading(), + 'product_id' => $this->product_id, + 'enquiry_data' => $this->enquiry_data, + 'customer_email' => $this->customer_email, + 'sent_to_admin' => true, + 'plain_text' => false, + ); } } diff --git a/classes/Emails/RequestQuoteSendEmail.php b/classes/Emails/RequestQuoteSendEmail.php index 3395bea..fd51b97 100644 --- a/classes/Emails/RequestQuoteSendEmail.php +++ b/classes/Emails/RequestQuoteSendEmail.php @@ -1,28 +1,55 @@ template_plain = 'emails/plain/request-quote.php'; $this->template_base = CatalogX()->plugin_path . 'templates/'; - // Call parent constuctor - parent::__construct(); - } - /** - * trigger function. - * - * @access public - * @return void - */ - function trigger( $products, $customer_data ) { - $additional_email = CatalogX()->setting->get_setting( 'additional_alert_email' ); - - $this->recipient = $additional_email; - $this->products = $products; - $this->customer_data = $customer_data; - $this->find[ ] = '{customer_name}'; - $this->replace[ ] = $customer_data['name']; - - $admin_email = CatalogX()->admin_email; - // Get the user by email - $admin_user = get_user_by('email', $admin_email); - if ($admin_user) { - // Return the display name of the admin user - $this->admin = $admin_user->display_name; - } - - if ( ! $this->is_enabled() || ! $this->get_recipient() ) { - return; + // Call parent constuctor. + parent::__construct(); } - - $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() ); - } - - /** - * Get email subject. - * - * @access public - * @return string - */ - public function get_default_subject() { - return apply_filters( 'catalogx_request_send_email_subject', __( 'New Quote Request from {customer_name}', 'catalogx' ), $this->object ); - } - - /** - * Get email heading. - * - * @access public - * @return string - */ - public function get_default_heading() { - return apply_filters( 'catalogx_request_send_email_heading', __( "New Quote Submitted by {customer_name} - Please Review", 'catalogx' ), $this->object ); - } - - /** - * get_content_html function. - * - * @access public - * @return string - */ - function get_content_html() { - ob_start(); - CatalogX()->util->get_template($this->template_html, $this->get_template_args()); - return ob_get_clean(); - } - - /** - * get_content_plain function. - * - * @access public - * @return string - */ - function get_content_plain() { - ob_start(); - CatalogX()->util->get_template($this->template_plain, $this->get_template_args()); - return ob_get_clean(); - } - - /** - * Get template arguments. - */ - protected function get_template_args() { - return [ - 'email_heading' => $this->get_heading(), - 'products' => $this->products, - 'customer_email' => $this->recipient, - 'customer_data' => $this->customer_data, - 'admin' => $this->admin, - 'sent_to_admin' => false, - 'plain_text' => true, - 'email' => $this, - ]; - } -} + + /** + * Trigger the email when a customer requests a quote. + * + * @access public + * @param array $products List of products included in the quote request. + * @param array $customer_data Data submitted by the customer. + * @return void + */ + public function trigger( $products, $customer_data ) { + $additional_email = CatalogX()->setting->get_setting( 'additional_alert_email' ); + + $this->recipient = $additional_email; + $this->products = $products; + $this->customer_data = $customer_data; + $this->find[] = '{customer_name}'; + $this->replace[] = $customer_data['name']; + + $admin_email = CatalogX()->admin_email; + // Get the user by email. + $admin_user = get_user_by( 'email', $admin_email ); + if ( $admin_user ) { + // Return the display name of the admin user. + $this->admin = $admin_user->display_name; + } + + if ( ! $this->is_enabled() || ! $this->get_recipient() ) { + return; + } + + $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() ); + } + + /** + * Get email subject. + * + * @access public + * @return string + */ + public function get_default_subject() { + return apply_filters( 'catalogx_request_send_email_subject', __( 'New Quote Request from {customer_name}', 'catalogx' ), $this->object ); + } + + /** + * Get email heading. + * + * @access public + * @return string + */ + public function get_default_heading() { + return apply_filters( 'catalogx_request_send_email_heading', __( 'New Quote Submitted by {customer_name} - Please Review', 'catalogx' ), $this->object ); + } + + /** + * Get_content_html function. + * + * @access public + * @return string + */ + public function get_content_html() { + ob_start(); + CatalogX()->util->get_template( $this->template_html, $this->get_template_args() ); + return ob_get_clean(); + } + + /** + * Get_content_plain function. + * + * @access public + * @return string + */ + public function get_content_plain() { + ob_start(); + CatalogX()->util->get_template( $this->template_plain, $this->get_template_args() ); + return ob_get_clean(); + } + + /** + * Get template arguments. + */ + protected function get_template_args() { + return array( + 'email_heading' => $this->get_heading(), + 'products' => $this->products, + 'customer_email' => $this->recipient, + 'customer_data' => $this->customer_data, + 'admin' => $this->admin, + 'sent_to_admin' => false, + 'plain_text' => true, + 'email' => $this, + ); + } + } } diff --git a/classes/Frontend.php b/classes/Frontend.php index 582f932..bec9002 100644 --- a/classes/Frontend.php +++ b/classes/Frontend.php @@ -1,85 +1,94 @@ setting->get_setting( 'shop_page_possition_setting', [] ); + // Get shop page button settings. + $position_settings = CatalogX()->setting->get_setting( 'shop_page_possition_setting', array() ); - // Priority of colide position + // Priority of colide position. $possiton_priority = 1; - // Possiotion after a particular section - $possition_after = 'sku_category'; + // Possiotion after a particular section. + $possition_after = 'sku_category'; - // If possition settings exists + // If possition settings exists. if ( $position_settings ) { - // Get the colide possition priority - $possiton_priority = array_search( 'custom_button', array_keys( $position_settings ) ) + 1; + // Get the colide possition priority. + $possiton_priority = array_search( 'custom_button', array_keys( $position_settings ), true ) + 1; - // Get the possition after - $possition_after = $position_settings[ 'custom_button' ]; + // Get the possition after. + $possition_after = $position_settings['custom_button']; } - // Display button group in a hooked based on possition setting + // Display button group in a hooked based on possition setting. switch ( $possition_after ) { case 'sku_category': - add_action( 'woocommerce_product_meta_end', [ $this, 'add_button_group' ], 99 + $possiton_priority ); + add_action( 'woocommerce_product_meta_end', array( $this, 'add_button_group' ), 99 + $possiton_priority ); break; case 'add_to_cart': - add_action( 'woocommerce_after_add_to_cart_button', [ $this, 'add_button_group' ], 99 + $possiton_priority ); + add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'add_button_group' ), 99 + $possiton_priority ); break; case 'product_description': - add_action( 'woocommerce_before_add_to_cart_form', [ $this, 'add_button_group' ], 99 + $possiton_priority ); + add_action( 'woocommerce_before_add_to_cart_form', array( $this, 'add_button_group' ), 99 + $possiton_priority ); break; case 'price_section': - add_action( 'woocommerce_single_product_summary', [ $this, 'add_button_group' ], 10 + $possiton_priority ); + add_action( 'woocommerce_single_product_summary', array( $this, 'add_button_group' ), 10 + $possiton_priority ); break; default: - add_action( 'woocommerce_single_product_summary', [ $this, 'add_button_group' ], 6 + $possiton_priority ); + add_action( 'woocommerce_single_product_summary', array( $this, 'add_button_group' ), 6 + $possiton_priority ); break; } } /** * Display all button group + * * @return void */ - function add_button_group() { + public function add_button_group() { ?>
@@ -90,6 +99,7 @@ function add_button_group() { /** * Display product price and description in single product page. + * * @return void */ public function display_price_and_description() { @@ -98,17 +108,16 @@ public function display_price_and_description() { add_filter( 'woocommerce_show_variation_price', '__return_false' ); remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 ); remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 ); - // for block support + // for block support. add_filter( 'woocommerce_get_price_html', '__return_empty_string' ); } - + $desc_hide_product_page = CatalogX()->setting->get_setting( 'hide_product_desc' ); if ( $desc_hide_product_page ) { - remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20); - // for block support - add_filter('woocommerce_short_description', '__return_empty_string'); - add_filter('render_block_core/post-excerpt', '__return_empty_string'); + remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 ); + // for block support. + add_filter( 'woocommerce_short_description', '__return_empty_string' ); + add_filter( 'render_block_core/post-excerpt', '__return_empty_string' ); } } - } \ No newline at end of file diff --git a/classes/FrontendScripts.php b/classes/FrontendScripts.php index afd2425..3229b77 100644 --- a/classes/FrontendScripts.php +++ b/classes/FrontendScripts.php @@ -1,214 +1,603 @@ is_dev ) { + return 'release/assets/'; + } + return 'assets/'; + } + + /** + * Enqueue external JavaScript files. + * + * @return void + */ + public static function enqueue_external_scripts() { + $base_dir = plugin_dir_path( __FILE__ ) . '../' . self::get_build_path_name() . 'js/'; + $base_url = CatalogX()->plugin_url . self::get_build_path_name() . 'js/'; + self::enqueue_scripts_from_dir( $base_dir . 'externals/', $base_url . 'externals/' ); + if ( CatalogX()->is_dev ) { + self::enqueue_scripts_from_dir( + $base_dir, + $base_url, + array( 'index.js', 'components.js' ), + '/min\.js$/i' + ); + } + } + + /** + * Enqueue JavaScript files from a directory, optionally excluding some by name or pattern. + * + * @param string $dir Full filesystem path to the JS directory. + * @param string $url Corresponding URL for the directory. + * @param string[] $exclude_files Array of filenames to exclude. + * @param string $exclude_pattern Optional regex pattern to exclude. + * @return void + */ + private static function enqueue_scripts_from_dir( $dir, $url, $exclude_files = array(), $exclude_pattern = '' ) { + if ( ! is_dir( $dir ) ) { + return; + } + $js_files = glob( $dir . '*.js' ); + foreach ( $js_files as $chunk_path ) { + $chunk_file = basename( $chunk_path ); + // Exclude based on filename or regex. + if ( in_array( $chunk_file, $exclude_files, true ) || + ( $exclude_pattern && preg_match( $exclude_pattern, $chunk_file ) ) + ) { + continue; + } + $chunk_handle = 'catalogx-script-' . sanitize_title( $chunk_file ); + $asset_file = str_replace( '.js', '.asset.php', $chunk_path ); + $deps = array(); + $version = filemtime( $chunk_path ); + if ( file_exists( $asset_file ) ) { + $asset = include $asset_file; + $deps = $asset['dependencies'] ?? array(); + $version = $asset['version'] ?? $version; + } + wp_enqueue_script( + $chunk_handle, + $url . $chunk_file, + $deps, + $version, + true + ); + } } - public static function register_script( $handle, $path, $deps = [], $version="", $text_domain="" ) { + /** + * Register and store a script for later use. + * + * @param string $handle Name of the script. + * @param string $path URL to the script. + * @param array $deps Optional. An array of dependencies. Default empty array. + * @param string $version Optional. Script version. Default empty string. + */ + public static function register_script( $handle, $path, $deps = array(), $version = '' ) { self::$scripts[] = $handle; wp_register_script( $handle, $path, $deps, $version, true ); - wp_set_script_translations( $handle, $text_domain ); + wp_set_script_translations( $handle, 'catalogx' ); } - public static function register_style( $handle, $path, $deps = [], $version="" ) { + /** + * Register and store a style for later use. + * + * @param string $handle Name of the script. + * @param string $path URL to the script. + * @param array $deps Optional. An array of dependencies. Default empty array. + * @param string $version Optional. Script version. Default empty string. + */ + public static function register_style( $handle, $path, $deps = array(), $version = '' ) { self::$styles[] = $handle; wp_register_style( $handle, $path, $deps, $version ); } + /** + * Register frontend scripts using filters and enqueue required external scripts. + * + * @return void + */ public static function register_scripts() { $version = CatalogX()->version; + self::enqueue_external_scripts(); - $register_scripts = apply_filters('catalogx_register_scripts', array( - 'catalogx-enquiry-frontend-script' => [ - 'src' => CatalogX()->plugin_url . 'modules/Enquiry/assets/js/frontend.js', - 'deps' => [ 'jquery', 'jquery-blockui' ], - 'version' => $version, - 'text_domain' => 'catalogx' - ], - 'catalogx-enquiry-form-script' => [ - 'src' => CatalogX()->plugin_url . 'build/blocks/enquiryForm/index.js', - 'deps' => [ 'jquery', 'jquery-blockui', 'wp-element', 'wp-i18n', 'wp-blocks', 'wp-hooks' ], - 'version' => $version, - 'text_domain' => 'catalogx' - ], - 'catalogx-quote-cart-script' => [ - 'src' => CatalogX()->plugin_url . 'build/blocks/quote-cart/index.js', - 'deps' => [ 'jquery', 'jquery-blockui', 'wp-element', 'wp-i18n', 'wp-blocks' ], - 'version' => $version, - 'text_domain' => 'catalogx' - ], - 'catalogx-add-to-quote-cart-script' => [ - 'src' => CatalogX()->plugin_url . 'modules/Quote/js/frontend.js', - 'deps' => [ 'jquery' ], - 'version' => $version, - 'text_domain' => 'catalogx' - ], - ) ); + $register_scripts = apply_filters( + 'catalogx_register_scripts', + array( + 'catalogx-enquiry-frontend-script' => array( + 'src' => CatalogX()->plugin_url . self::get_build_path_name() . 'modules/Enquiry/js/' . CATALOGX_PLUGIN_SLUG . '-frontend.min.js', + 'deps' => array( 'jquery', 'jquery-blockui' ), + 'version' => $version, + ), + 'catalogx-enquiry-form-script' => array( + 'src' => CatalogX()->plugin_url . self::get_build_path_name() . 'js/block/enquiryForm/index.js', + 'deps' => array( 'jquery', 'jquery-blockui', 'wp-element', 'wp-i18n', 'wp-blocks', 'wp-hooks' ), + 'version' => $version, + ), + 'catalogx-quote-cart-script' => array( + 'src' => CatalogX()->plugin_url . self::get_build_path_name() . 'js/block/quote-cart/index.js', + 'deps' => array( 'jquery', 'jquery-blockui', 'wp-element', 'wp-i18n', 'wp-blocks' ), + 'version' => $version, + ), + 'catalogx-add-to-quote-cart-script' => array( + 'src' => CatalogX()->plugin_url . self::get_build_path_name() . 'modules/Quote/js/' . CATALOGX_PLUGIN_SLUG . '-frontend.min.js', + 'deps' => array( 'jquery' ), + 'version' => $version, + ), + ) + ); foreach ( $register_scripts as $name => $props ) { - self::register_script( $name, $props['src'], $props['deps'], $props['version'], $props['text_domain'] ); + self::register_script( $name, $props['src'], $props['deps'], $props['version'] ); } } + /** + * Register frontend styles using filters. + * + * @return void + */ public static function register_styles() { $version = CatalogX()->version; - $register_styles = apply_filters('catalogx_register_styles', [ - 'catalogx-frontend-style' => [ - 'src' => CatalogX()->plugin_url . 'assets/css/frontend.css', - 'deps' => array(), - 'version' => $version, - ], - 'catalogx-enquiry-form-style' => [ - 'src' => CatalogX()->plugin_url . 'build/blocks/enquiryForm/index.css', - 'deps' => array(), - 'version' => $version, - ], - 'catalogx-quote-cart-style' => [ - 'src' => CatalogX()->plugin_url . 'build/blocks/quote-cart/index.css', - 'deps' => array(), - 'version' => $version, - ], - ] ); + $register_styles = apply_filters( + 'catalogx_register_styles', + array( + 'catalogx-frontend-style' => array( + 'src' => CatalogX()->plugin_url . self::get_build_path_name() . 'styles/' . CATALOGX_PLUGIN_SLUG . '-frontend.min.css', + 'deps' => array(), + 'version' => $version, + ), + 'catalogx-enquiry-form-style' => array( + 'src' => CatalogX()->plugin_url . self::get_build_path_name() . 'styles/block/enquiryForm/index.css', + 'deps' => array(), + 'version' => $version, + ), + ) + ); + foreach ( $register_styles as $name => $props ) { + self::register_style( $name, $props['src'], $props['deps'], $props['version'] ); + } + } + + /** + * Register admin scripts using filters. + * + * Loads admin-specific JavaScript assets and chunked dependencies. + * + * @return void + */ + public static function admin_register_scripts() { + $version = CatalogX()->version; + // Enqueue all chunk files (External dependencies). + self::enqueue_external_scripts(); + $index_asset = include plugin_dir_path( __FILE__ ) . '../' . self::get_build_path_name() . 'js/index.asset.php'; + $component_asset = include plugin_dir_path( __FILE__ ) . '../' . self::get_build_path_name() . 'js/components.asset.php'; + $register_scripts = apply_filters( + 'admin_catalogx_register_scripts', + array( + 'catalogx-admin-script' => array( + 'src' => CatalogX()->plugin_url . self::get_build_path_name() . 'js/index.js', + 'deps' => $index_asset['dependencies'], + 'version' => $version, + ), + 'catalogx-components-script' => array( + 'src' => CatalogX()->plugin_url . self::get_build_path_name() . 'js/components.js', + 'deps' => $component_asset['dependencies'], + 'version' => $version, + ), + ) + ); + foreach ( $register_scripts as $name => $props ) { + self::register_script( $name, $props['src'], $props['deps'], $props['version'] ); + } + } + + /** + * Register admin styles using filters. + * + * @return void + */ + public static function admin_register_styles() { + $version = CatalogX()->version; + + $register_styles = apply_filters( + 'admin_catalogx_register_styles', + array( + 'catalogx-components-style' => array( + 'src' => CatalogX()->plugin_url . self::get_build_path_name() . 'styles/components.css', + 'deps' => array(), + 'version' => $version, + ), + ) + ); + foreach ( $register_styles as $name => $props ) { self::register_style( $name, $props['src'], $props['deps'], $props['version'] ); } } /** - * Register/queue frontend scripts. + * Register/queue frontend scripts and styles */ public static function load_scripts() { self::register_scripts(); self::register_styles(); } + /** + * Register/queue admin scripts and styles + */ + public static function admin_load_scripts() { + self::admin_register_scripts(); + self::admin_register_styles(); + } + /** + * Localize all scripts. + * + * @param string $handle Script handle in which the data will be attached to. + */ public static function localize_scripts( $handle ) { - $current_user = wp_get_current_user(); - - $localize_scripts = apply_filters('catalogx_localize_scripts', array( - 'catalogx-enquiry-frontend-script' => [ - 'object_name' => 'enquiryFrontend', - 'data' => [ - 'ajaxurl' => admin_url('admin-ajax.php'), - ], - ], - 'catalogx-enquiry-form-script' => [ - 'object_name' => 'enquiryFormData', - 'data' => [ - 'apiurl' => untrailingslashit(get_rest_url()), - 'nonce' => wp_create_nonce( 'wp_rest' ), - 'settings_free' => CatalogX()->modules->is_active('enquiry') ? EnquiryFrontend::catalogx_free_form_settings() : [], - 'settings_pro' => CatalogX()->modules->is_active('enquiry') ? EnquiryFrontend::catalogx_pro_form_settings() : [], - 'khali_dabba' => \CatalogX\Utill::is_khali_dabba(), - 'product_data' => (\CatalogX\Utill::is_khali_dabba() && !empty(CatalogX_Pro()->cart->get_cart_data())) ? CatalogX_Pro()->cart->get_cart_data() : '', - 'default_placeholder' => [ - 'name' => $current_user->display_name, - 'email' => $current_user->user_email - ], - 'content_before_form' => apply_filters('catalogx_add_content_before_form', ''), - 'content_after_form' => apply_filters('catalogx_add_content_after_form', ''), - 'error_strings' => [ - 'required' => __("This field is required", "catalogx"), - 'invalid' => __("Invalid email format", "catalogx"), - ] - ], - ], - 'catalogx-add-to-quote-cart-script' => [ - 'object_name' => 'addToQuoteCart', - 'data' => [ - 'ajaxurl' => admin_url('admin-ajax.php'), - 'loader' => admin_url('images/wpspin_light.gif'), - 'no_more_product' => __('No more product in Quote list!', 'catalogx'), - ], - ], - 'catalogx-enquiry-button-editor-script' => [ - 'object_name' => 'enquiryButton', - 'data' => [ - 'apiUrl' => untrailingslashit(get_rest_url()), - 'restUrl' => CatalogX()->rest_namespace, - 'nonce' => wp_create_nonce( 'catalog-security-nonce' ) - ], - ], - 'catalogx-quote-button-editor-script' => [ - 'object_name' => 'quoteButton', - 'data' => [ - 'apiUrl' => untrailingslashit(get_rest_url()), - 'restUrl' => CatalogX()->rest_namespace, - 'nonce' => wp_create_nonce( 'catalog-security-nonce' ) - ], - ], - 'catalogx-quote-cart-editor-script' => [ - 'object_name' => 'quoteCart', - 'data' => [ - 'apiUrl' => untrailingslashit(get_rest_url()), - 'restUrl' => CatalogX()->rest_namespace, - 'nonce' => wp_create_nonce('wp_rest'), - 'name' => $current_user->display_name, - 'email' => $current_user->user_email, - 'quote_my_account_url' => site_url('/my-account/all-quotes/'), - 'khali_dabba' => Utill::is_khali_dabba(), - ], - ], - 'catalogx-quote-button-script' => [ - 'object_name' => 'quoteButton', - 'data' => [ - 'apiUrl' => untrailingslashit(get_rest_url()), - 'restUrl' => CatalogX()->rest_namespace, - 'nonce' => wp_create_nonce( 'catalog-security-nonce' ) - ], - ], - 'catalogx-quote-cart-script' => [ - 'object_name' => 'quoteCart', - 'data' => [ - 'apiUrl' => untrailingslashit(get_rest_url()), - 'restUrl' => CatalogX()->rest_namespace, - 'nonce' => wp_create_nonce('wp_rest'), - 'name' => $current_user->display_name, - 'email' => $current_user->user_email, - 'quote_my_account_url' => site_url('/my-account/all-quotes/'), - 'khali_dabba' => Utill::is_khali_dabba(), - ], - ], - 'catalogx-enquiry-button-script' => [ - 'object_name' => 'enquiryButton', - 'data' => [ - 'apiUrl' => untrailingslashit(get_rest_url()), - 'restUrl' => CatalogX()->rest_namespace, - 'nonce' => wp_create_nonce( 'catalog-security-nonce' ) - ], - ], - )); - + + // Prepare data of all pages. + $pages = get_pages(); + $all_pages = array(); + + if ( $pages ) { + foreach ( $pages as $page ) { + $all_pages[] = array( + 'value' => $page->ID, + 'label' => $page->post_title, + 'key' => $page->ID, + ); + } + } + + // Prepare data of all user roles. + $roles = wp_roles()->roles; + $all_roles = array(); + + if ( $roles ) { + foreach ( $roles as $key => $role ) { + $all_roles[] = array( + 'value' => $key, + 'label' => $role['name'], + 'key' => $key, + ); + } + } + + // Get all users id and name and prepare data. + $users = get_users( array( 'fields' => array( 'display_name', 'id' ) ) ); + $all_users = array(); + + foreach ( $users as $user ) { + $all_users[] = array( + 'value' => $user->ID, + 'label' => $user->display_name, + 'key' => $user->ID, + ); + } + + // Prepare all products. + $products_ids = wc_get_products( + array( + 'limit' => -1, + 'return' => 'ids', + ) + ); + $all_products = array(); + + foreach ( $products_ids as $id ) { + $product_name = get_the_title( $id ); + + $all_products[] = array( + 'value' => $id, + 'label' => $product_name, + 'key' => $id, + ); + } + + // Prepare all product terms. + $terms = get_terms( + array( + 'taxonomy' => 'product_cat', + 'orderby' => 'name', + 'order' => 'ASC', + ) + ); + $product_cat = array(); + + if ( $terms && empty( $terms->errors ) ) { + foreach ( $terms as $term ) { + $product_cat[] = array( + 'value' => $term->term_id, + 'label' => $term->name, + 'key' => $term->term_id, + ); + } + } + + // Prepare all product tags. + $tags = get_terms( + array( + 'taxonomy' => 'product_tag', + 'hide_empty' => false, + ) + ); + $product_tags = array(); + if ( $tags ) { + foreach ( $tags as $tag ) { + $product_tags[] = array( + 'value' => $tag->term_id, + 'label' => $tag->name, + 'key' => $tag->term_id, + ); + } + } + + // Prepare all product brands. + $brands = get_terms( + array( + 'taxonomy' => 'product_brand', + 'hide_empty' => false, + ) + ); + $all_product_brand = array(); + if ( $brands ) { + foreach ( $brands as $brand ) { + $all_product_brand[] = array( + 'value' => $brand->term_id, + 'label' => $brand->name, + 'key' => $brand->term_id, + ); + } + } + + // Get current user role. + $current_user = wp_get_current_user(); + $current_user_role = ''; + if ( ! empty( $current_user->roles ) && is_array( $current_user->roles ) ) { + $current_user_role = reset( $current_user->roles ); + } + + // Get all tab setting's database value. + $settings_value = array(); + $tabs_names = array( 'enquiry-catalog-customization', 'all-settings', 'enquiry-form-customization', 'enquiry-quote-exclusion', 'tools', 'enquiry-email-temp', 'wholesale', 'wholesale-registration', 'pages' ); + foreach ( $tabs_names as $tab_name ) { + $settings_value[ $tab_name ] = CatalogX()->setting->get_option( str_replace( '-', '_', 'catalogx_' . $tab_name . '_settings' ) ); + } + + if ( 'administrator' === $current_user_role ) { + $quote_base_url = admin_url( 'admin.php?page=wc-orders&action=edit&id=' ); + } elseif ( 'customer' === $current_user_role ) { + $quote_base_url = site_url( '/my-account/view-quote/' ); + } else { + $quote_base_url = '/'; + } + + $localize_scripts = apply_filters( + 'catalogx_localize_scripts', + array( + 'catalogx-admin-script' => array( + 'object_name' => 'appLocalizer', + 'data' => array( + 'apiUrl' => untrailingslashit( get_rest_url() ), + 'nonce' => wp_create_nonce( 'wp_rest' ), + 'tab_name' => 'CatalogX', + 'restUrl' => CatalogX()->rest_namespace, + 'all_pages' => $all_pages, + 'role_array' => $all_roles, + 'all_users' => $all_users, + 'all_products' => $all_products, + 'all_product_cat' => $product_cat, + 'all_product_brand' => $all_product_brand, + 'all_product_tag' => $product_tags, + 'settings_databases_value' => $settings_value, + 'active_modules' => CatalogX()->modules->get_active_modules(), + 'user_role' => $current_user_role, + 'banner_img' => CatalogX()->plugin_url . 'assets/images/catalog-pro-add-admin-banner.jpg', + 'default_img' => CatalogX()->plugin_url . 'src/assets/images/default.png', + 'template1' => CatalogX()->plugin_url . 'assets/images/email/templates/catalogx-email-template-default.png', + 'template2' => CatalogX()->plugin_url . 'assets/images/email/templates/catalogx-email-template-1.png', + 'template3' => CatalogX()->plugin_url . 'assets/images/email/templates/catalogx-email-template-2.png', + 'template4' => CatalogX()->plugin_url . 'assets/images/email/templates/catalogx-email-template-3.png', + 'template5' => CatalogX()->plugin_url . 'assets/images/email/templates/catalogx-email-template-4.png', + 'template6' => CatalogX()->plugin_url . 'assets/images/email/templates/catalogx-email-template-5.png', + 'template7' => CatalogX()->plugin_url . 'assets/images/email/templates/catalogx-email-template-6.png', + 'khali_dabba' => Utill::is_khali_dabba(), + 'pro_url' => esc_url( CATALOGX_PRO_SHOP_URL ), + 'order_edit' => admin_url( 'admin.php?page=wc-orders&action=edit' ), + 'site_url' => admin_url( 'admin.php?page=catalogx#&tab=settings&subtab=all-settings' ), + 'module_page_url' => admin_url( 'admin.php?page=catalogx#&tab=modules' ), + 'settings_page_url' => admin_url( 'admin.php?page=catalogx#&tab=settings&subtab=all-settings' ), + 'enquiry_form_settings_url' => admin_url( 'admin.php?page=catalogx#&tab=settings&subtab=enquiry-form-customization' ), + 'customization_settings_url' => admin_url( 'admin.php?page=catalogx#&tab=settings&subtab=enquiry-catalog-customization' ), + 'wholesale_settings_url' => admin_url( 'admin.php?page=catalogx#&tab=settings&subtab=wholesale' ), + 'rule_url' => admin_url( 'admin.php?page=catalogx#&tab=rules' ), + 'currency' => get_woocommerce_currency(), + 'notifima_active' => Utill::is_active_plugin( 'notifima' ), + 'mvx_active' => Utill::is_active_plugin( 'multivendorx' ), + 'quote_module_active' => CatalogX()->modules->is_active( 'quote' ), + 'quote_base_url' => $quote_base_url, + ), + ), + 'catalogx-enquiry-frontend-script' => array( + 'object_name' => 'enquiryFrontend', + 'data' => array( + 'ajaxurl' => admin_url( 'admin-ajax.php' ), + ), + ), + 'catalogx-enquiry-form-script' => array( + 'object_name' => 'enquiryFormData', + 'data' => array( + 'apiUrl' => untrailingslashit( get_rest_url() ), + 'nonce' => wp_create_nonce( 'wp_rest' ), + 'settings_free' => CatalogX()->modules->is_active( 'enquiry' ) ? EnquiryFrontend::catalogx_free_form_settings() : array(), + 'settings_pro' => CatalogX()->modules->is_active( 'enquiry' ) ? EnquiryFrontend::catalogx_pro_form_settings() : array(), + 'khali_dabba' => \CatalogX\Utill::is_khali_dabba(), + 'product_data' => ( \CatalogX\Utill::is_khali_dabba() && ! empty( CatalogX_Pro()->cart->get_cart_data() ) ) ? CatalogX_Pro()->cart->get_cart_data() : '', + 'default_placeholder' => array( + 'name' => $current_user->display_name, + 'email' => $current_user->user_email, + ), + 'content_before_form' => apply_filters( 'catalogx_add_content_before_form', '' ), + 'content_after_form' => apply_filters( 'catalogx_add_content_after_form', '' ), + 'error_strings' => array( + 'required' => __( 'This field is required', 'catalogx' ), + 'invalid' => __( 'Invalid email format', 'catalogx' ), + ), + ), + ), + 'catalogx-add-to-quote-cart-script' => array( + 'object_name' => 'addToQuoteCart', + 'data' => array( + 'ajaxurl' => admin_url( 'admin-ajax.php' ), + 'loader' => admin_url( 'images/wpspin_light.gif' ), + 'no_more_product' => __( 'No more product in Quote list!', 'catalogx' ), + ), + ), + 'catalogx-enquiry-button-editor-script' => array( + 'object_name' => 'enquiryButton', + 'data' => array( + 'apiUrl' => untrailingslashit( get_rest_url() ), + 'restUrl' => CatalogX()->rest_namespace, + 'nonce' => wp_create_nonce( 'wp_rest' ), + ), + ), + 'catalogx-quote-button-editor-script' => array( + 'object_name' => 'quoteButton', + 'data' => array( + 'apiUrl' => untrailingslashit( get_rest_url() ), + 'restUrl' => CatalogX()->rest_namespace, + 'nonce' => wp_create_nonce( 'wp_rest' ), + ), + ), + 'catalogx-quote-cart-editor-script' => array( + 'object_name' => 'quoteCart', + 'data' => array( + 'apiUrl' => untrailingslashit( get_rest_url() ), + 'restUrl' => CatalogX()->rest_namespace, + 'nonce' => wp_create_nonce( 'wp_rest' ), + 'name' => $current_user->display_name, + 'email' => $current_user->user_email, + 'quote_my_account_url' => site_url( '/my-account/all-quotes/' ), + 'khali_dabba' => Utill::is_khali_dabba(), + ), + ), + 'catalogx-quote-button-script' => array( + 'object_name' => 'quoteButton', + 'data' => array( + 'apiUrl' => untrailingslashit( get_rest_url() ), + 'restUrl' => CatalogX()->rest_namespace, + 'nonce' => wp_create_nonce( 'wp_rest' ), + ), + ), + 'catalogx-quote-cart-script' => array( + 'object_name' => 'quoteCart', + 'data' => array( + 'apiUrl' => untrailingslashit( get_rest_url() ), + 'restUrl' => CatalogX()->rest_namespace, + 'nonce' => wp_create_nonce( 'wp_rest' ), + 'name' => $current_user->display_name, + 'email' => $current_user->user_email, + 'quote_my_account_url' => site_url( '/my-account/all-quotes/' ), + 'khali_dabba' => Utill::is_khali_dabba(), + ), + ), + 'catalogx-enquiry-button-script' => array( + 'object_name' => 'enquiryButton', + 'data' => array( + 'apiUrl' => untrailingslashit( get_rest_url() ), + 'restUrl' => CatalogX()->rest_namespace, + 'nonce' => wp_create_nonce( 'wp_rest' ), + ), + ), + ) + ); + if ( isset( $localize_scripts[ $handle ] ) ) { $props = $localize_scripts[ $handle ]; self::localize_script( $handle, $props['object_name'], $props['data'] ); } } - public static function localize_script( $handle, $name, $data = [], ) { + /** + * Localizes a registered script with data for use in JavaScript. + * + * @param string $handle Script handle the data will be attached to. + * @param string $name JavaScript object name. + * @param array $data Data to be made available in JavaScript. + */ + public static function localize_script( $handle, $name, $data = array(), ) { wp_localize_script( $handle, $name, $data ); } + /** + * Enqueues a registered script. + * + * @param string $handle Handle of the registered script to enqueue. + */ public static function enqueue_script( $handle ) { wp_enqueue_script( $handle ); } + /** + * Enqueues a registered style. + * + * @param string $handle Handle of the registered style to enqueue. + */ public static function enqueue_style( $handle ) { wp_enqueue_style( $handle ); } - -} \ No newline at end of file +} diff --git a/classes/Install.php b/classes/Install.php index 972bbc2..e4c0a36 100644 --- a/classes/Install.php +++ b/classes/Install.php @@ -1,17 +1,25 @@ 'name-label', 'email' => 'email-label', 'phone' => 'is-phone', @@ -21,342 +29,373 @@ class Install { 'fileupload' => 'is-fileupload', 'filesize-limit' => 'filesize-limit', 'captcha' => 'is-captcha', - ]; + ); - const PRO_FORM_TYPE_MAP = [ + const PRO_FORM_TYPE_MAP = array( 'p_title' => 'title', 'textbox' => 'text', - ]; + ); + /** + * Previously stored plugin version. + * + * @var string + */ public static $previous_version = ''; - public static $current_version = ''; - /** * Install class constructor functions */ public function __construct() { - // Get the previous version and current version + // Get the previous version and current version. self::$previous_version = get_option( self::VERSION_KEY, '' ); - self::$current_version = CatalogX()->version; - - $this->create_database_tables(); - $this->set_default_modules(); - $this->set_default_settings(); - - // this function should be deleted after 7.0.0 - if (!empty(get_option('mvx_catalog_general_tab_settings'))) { + // this function should be deleted after 7.0.0 . + if ( ! empty( get_option( 'mvx_catalog_general_tab_settings' ) ) ) { + $this->create_database_tables(); + $this->set_default_modules(); + $this->set_default_settings(); $this->migrate_catalog_enquiry_to_catalogx(); + update_option( self::VERSION_KEY, '6.0.0' ); } - - $this->run_default_migration(); - - // Update the version in database - update_option( self::VERSION_KEY, self::$current_version ); + if ( ! ( get_option( self::VERSION_KEY, false ) ) ) { + $this->create_database_tables(); + $this->set_default_modules(); + $this->set_default_settings(); + } else { + $this->run_default_migration(); + } + // Update the version in database. + update_option( self::VERSION_KEY, CatalogX()->version ); } /** * Create database table for plugin + * * @return void */ private function create_database_tables() { + // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared global $wpdb; + // Include upgrade functions if not loaded. + if ( ! function_exists( 'dbDelta' ) ) { + require_once ABSPATH . 'wp-admin/includes/upgrade.php'; + } + $collate = ''; - if ($wpdb->has_cap('collation')) { + if ( $wpdb->has_cap( 'collation' ) ) { $collate = $wpdb->get_charset_collate(); } - if (!empty(get_option('mvx_catalog_general_tab_settings'))) { - // table migration - // Rename the table + if ( ! empty( get_option( 'mvx_catalog_general_tab_settings' ) ) ) { + // table migration. + // Rename the table. $wpdb->query( - "ALTER TABLE `{$wpdb->prefix}catelog_cust_vendor_answers` RENAME TO `{$wpdb->prefix}" . Utill::TABLES[ 'message' ] . "`" + "ALTER TABLE `{$wpdb->prefix}catelog_cust_vendor_answers` RENAME TO `{$wpdb->prefix}" . Utill::TABLES['message'] . '`' ); - // Add column to table + // Add column to table. $wpdb->query( - "ALTER TABLE `{$wpdb->prefix}" . Utill::TABLES[ 'message' ] . "` - ADD COLUMN attachment bigint(20);" + "ALTER TABLE `{$wpdb->prefix}" . Utill::TABLES['message'] . '` + ADD COLUMN attachment bigint(20);' ); $wpdb->query( - "ALTER TABLE `{$wpdb->prefix}" . Utill::TABLES[ 'message' ] . "` - ADD COLUMN reaction varchar(20);" + "ALTER TABLE `{$wpdb->prefix}" . Utill::TABLES['message'] . '` + ADD COLUMN reaction varchar(20);' ); $wpdb->query( - "ALTER TABLE `{$wpdb->prefix}" . Utill::TABLES[ 'message' ] . "` - ADD COLUMN star boolean;" + "ALTER TABLE `{$wpdb->prefix}" . Utill::TABLES['message'] . '` + ADD COLUMN star boolean;' ); $wpdb->query( - "ALTER TABLE `{$wpdb->prefix}" . Utill::TABLES[ 'message' ] . "` - ADD COLUMN reply text DEFAULT NULL;" + "ALTER TABLE `{$wpdb->prefix}" . Utill::TABLES['message'] . '` + ADD COLUMN reply text DEFAULT NULL;' ); - } else { - // Create message table - $wpdb->query( - "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}" . Utill::TABLES[ 'message' ] . "` ( - `chat_message_id` bigint(20) NOT NULL AUTO_INCREMENT, - `to_user_id` bigint(20) NOT NULL, - `from_user_id` bigint(20) NOT NULL, - `chat_message` text NOT NULL, - `product_id` text NOT NULL, - `enquiry_id` bigint(20) NOT NULL, - `status` varchar(20) NOT NULL, - `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, - `attachment` bigint(20), - `reaction` varchar(20), - `star` boolean, - `reply` text DEFAULT NULL, - PRIMARY KEY (`chat_message_id`) - ) $collate;" - ); - } - - // Create enquiry table - $wpdb->query( - "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}" . Utill::TABLES[ 'enquiry' ] . "` ( - `id` bigint(20) NOT NULL AUTO_INCREMENT, - `product_info` text NOT NULL, - `user_id` bigint(20) NOT NULL DEFAULT 0, - `user_name` varchar(50) NOT NULL, - `user_email` varchar(50) NOT NULL, - `user_additional_fields` text NOT NULL, - `pin_msg_id` bigint(20) DEFAULT NULL, + // Create message table. + $sql_message = "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}" . Utill::TABLES['message'] . "` ( + `chat_message_id` bigint(20) NOT NULL AUTO_INCREMENT, + `to_user_id` bigint(20) NOT NULL, + `from_user_id` bigint(20) NOT NULL, + `chat_message` text NOT NULL, + `product_id` text NOT NULL, + `enquiry_id` bigint(20) NOT NULL, + `status` varchar(20) NOT NULL, `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, - PRIMARY KEY (`id`) - ) $collate;" - ); + `attachment` bigint(20), + `reaction` varchar(20), + `star` boolean, + `reply` text DEFAULT NULL, + PRIMARY KEY (`chat_message_id`) + ) $collate;"; + + dbDelta( $sql_message ); + } - // Create rules table - $wpdb->query( - "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}" . Utill::TABLES[ 'rule' ] . "` ( - `id` bigint(20) NOT NULL AUTO_INCREMENT, - `name` text, - `user_id` bigint(20), - `role_id` varchar(100), - `product_id` bigint(20), - `category_id` bigint(20), - `brand_id` bigint(20), - `quentity` bigint(20) NOT NULL, - `type` varchar(20) NOT NULL, - `amount` bigint(20) NOT NULL, - `priority` bigint(20) NOT NULL, - `active` boolean NOT NULL DEFAULT true, - PRIMARY KEY ( `id` ) - ) $collate;" - ); + // Create enquiry table. + $sql_enquiry = "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}" . Utill::TABLES['enquiry'] . "` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `product_info` text NOT NULL, + `user_id` bigint(20) NOT NULL DEFAULT 0, + `user_name` varchar(50) NOT NULL, + `user_email` varchar(50) NOT NULL, + `user_additional_fields` text NOT NULL, + `pin_msg_id` bigint(20) DEFAULT NULL, + `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (`id`) + ) $collate;"; + + dbDelta( $sql_enquiry ); + + // Create rules table. + $sql_rule = "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}" . Utill::TABLES['rule'] . "` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `name` text, + `user_id` bigint(20), + `role_id` varchar(100), + `product_id` bigint(20), + `category_id` bigint(20), + `brand_id` bigint(20), + `quentity` bigint(20) NOT NULL, + `type` varchar(20) NOT NULL, + `amount` bigint(20) NOT NULL, + `priority` bigint(20) NOT NULL, + `active` boolean NOT NULL DEFAULT true, + PRIMARY KEY ( `id` ) + ) $collate;"; + + dbDelta( $sql_rule ); + // phpcs:enable } /** * Set default modules + * * @return void */ - public static function set_default_modules() { - // Enable catalog module by default - $active_modules = get_option( Modules::ACTIVE_MODULES_DB_KEY, [] ); - update_option( Modules::ACTIVE_MODULES_DB_KEY, array_unique( array_merge( $active_modules, ['catalog'] ) ) ); + public static function set_default_modules() { + // Enable catalog module by default. + $active_modules = get_option( Modules::ACTIVE_MODULES_DB_KEY, array() ); + update_option( Modules::ACTIVE_MODULES_DB_KEY, array_unique( array_merge( $active_modules, array( 'catalog' ) ) ) ); } /** * Set default settings + * * @return void */ public function set_default_settings() { - // Update shopping gurnal - $all_settings = [ - 'is_disable_popup' => 'popup', - 'enable_cart_checkout' => [], - 'set_expiry_time' => 'Never', - 'is_enable_multiple_product_enquiry' => ['is_enable_multiple_product_enquiry'], - ]; + // Update shopping gurnal. + $all_settings = array( + 'is_disable_popup' => 'popup', + 'enable_cart_checkout' => array(), + 'set_expiry_time' => 'Never', + 'is_enable_multiple_product_enquiry' => array( 'is_enable_multiple_product_enquiry' ), + ); update_option( 'catalogx_all_settings_settings', $all_settings ); - - $email_settings = [ - 'additional_alert_email' => CatalogX()->admin_email, - ]; + + $email_settings = array( + 'additional_alert_email' => CatalogX()->admin_email, + ); update_option( 'catalogx_enquiry_email_temp_settings', $email_settings ); - // Update pages settings - $page_settings = [ - 'set_enquiry_cart_page' => intval( get_option( 'catalogx_enquiry_cart_page' ) ), - 'set_request_quote_page' => intval( get_option( 'catalogx_request_quote_page' ) ), - 'set_wholesale_products_page' => intval( get_option( 'wholesale_products_page' ) ) - ]; + // Update pages settings. + $page_settings = array( + 'set_enquiry_cart_page' => intval( get_option( 'catalogx_enquiry_cart_page' ) ), + 'set_request_quote_page' => intval( get_option( 'catalogx_request_quote_page' ) ), + 'set_wholesale_products_page' => intval( get_option( 'wholesale_products_page' ) ), + ); update_option( 'catalogx_pages_settings', $page_settings ); - - // Update form settings - $free_form = [ - [ - 'key' => 'name', - 'label' => 'Enter your name', - 'active' => true, - ], - [ - 'key' => 'email', - 'label' => 'Enter your email', - 'active' => true, - ], - [ "key" => "phone" ], - [ "key" => "address" ], - [ "key" => "subject" ], - [ "key" => "comment" ], - [ "key" => "fileupload" ], - [ "key" => "filesize-limit" ], - [ "key" => "captcha" ], - ]; - - $pro_form = [ - [ - 'id' => 1, - 'type' => 'title', - 'label' => 'Enquiry Form', - ], - [ + + // Update form settings. + $free_form = array( + array( + 'key' => 'name', + 'label' => 'Enter your name', + 'active' => true, + ), + array( + 'key' => 'email', + 'label' => 'Enter your email', + 'active' => true, + ), + array( 'key' => 'phone' ), + array( 'key' => 'address' ), + array( 'key' => 'subject' ), + array( 'key' => 'comment' ), + array( 'key' => 'fileupload' ), + array( 'key' => 'filesize-limit' ), + array( 'key' => 'captcha' ), + ); + + $pro_form = array( + array( + 'id' => 1, + 'type' => 'title', + 'label' => 'Enquiry Form', + ), + array( 'id' => 2, 'type' => 'text', 'label' => 'Enter your name', 'required' => true, 'placeholder' => 'I am default place holder', 'name' => 'name', - 'not_editable' => true - ], - [ + 'not_editable' => true, + ), + array( 'id' => 3, 'type' => 'email', 'label' => 'Enter your email', 'required' => true, 'placeholder' => 'I am default place holder', 'name' => 'email', - 'not_editable' => true - ], - ]; + 'not_editable' => true, + ), + ); - $form_settings = [ - 'formsettings' => [ + $form_settings = array( + 'formsettings' => array( 'formfieldlist' => $pro_form, - 'butttonsetting' => [], - ], - 'freefromsetting' => $free_form, - ]; + 'butttonsetting' => array(), + ), + 'freefromsetting' => $free_form, + ); update_option( 'catalogx_enquiry_form_customization_settings', $form_settings ); - $wholesale_form = [ - [ - 'id' => 1, - 'type' => 'title', - 'label' => 'Wholesale Form', - ], - [ + $wholesale_form = array( + array( + 'id' => 1, + 'type' => 'title', + 'label' => 'Wholesale Form', + ), + array( 'id' => 2, 'type' => 'text', 'label' => 'Enter your name', 'required' => true, 'placeholder' => 'I am default place holder', 'name' => 'name', - 'not_editable' => true - ], - [ + 'not_editable' => true, + ), + array( 'id' => 3, 'type' => 'email', 'label' => 'Enter your email', 'required' => true, 'placeholder' => 'I am default place holder', 'name' => 'email', - 'not_editable' => true - ], - ]; + 'not_editable' => true, + ), + ); - $wholesale_from_settings = [ - 'wholesale_from_settings' => [ + $wholesale_from_settings = array( + 'wholesale_from_settings' => array( 'formfieldlist' => $wholesale_form, - 'butttonsetting' => [], - ], - ]; + 'butttonsetting' => array(), + ), + ); update_option( 'catalogx_wholesale_registration_settings', $wholesale_from_settings ); } - // this function is for default migration run + /** + * Run default migration tasks. + * + * @return void + */ public function run_default_migration() { - // Migration by specific version controll + // Migration by specific version controll. if ( version_compare( self::$previous_version, '6.0.7', '<' ) ) { + // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.SchemaChange global $wpdb; $column_exists = $wpdb->get_results( $wpdb->prepare( - "SHOW COLUMNS FROM `{$wpdb->prefix}" . Utill::TABLES[ 'rule' ] . "` LIKE %s", + "SHOW COLUMNS FROM `{$wpdb->prefix}" . Utill::TABLES['rule'] . '` LIKE %s', 'brand_id' ) - ); - if (empty($column_exists)) { + ); + if ( empty( $column_exists ) ) { $wpdb->query( - "ALTER TABLE `{$wpdb->prefix}" . Utill::TABLES[ 'rule' ] . "` - ADD COLUMN brand_id bigint(20);" + "ALTER TABLE `{$wpdb->prefix}" . Utill::TABLES['rule'] . '` + ADD COLUMN brand_id bigint(20);' ); } + // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.SchemaChange - $previous_enquiry_catalog_customization_settings = get_option( 'catalogx_enquiry-catalog-customization_settings', [] ); - update_option( 'catalogx_enquiry_catalog_customization_settings', $previous_enquiry_catalog_customization_settings ); - - $previous_all_settings_settings = get_option( 'catalogx_all-settings_settings', [] ); - update_option( 'catalogx_all_settings_settings', $previous_all_settings_settings ); - - $previous_enquiry_quote_exclusion_settings = get_option( 'catalogx_enquiry-quote-exclusion_settings', [] ); - update_option( 'catalogx_enquiry_quote_exclusion_settings', $previous_enquiry_quote_exclusion_settings ); - - $previous_enquiry_form_customization_settings = get_option( 'catalogx_enquiry-form-customization_settings', [] ); - update_option( 'catalogx_enquiry_form_customization_settings', $previous_enquiry_form_customization_settings ); - - $previous_enquiry_email_temp_settings = get_option( 'catalogx_enquiry-email-temp_settings', [] ); - update_option( 'catalogx_enquiry_email_temp_settings', $previous_enquiry_email_temp_settings ); - - $previous_wholesale_registration_settings = get_option( 'catalogx_wholesale-registration_settings', [] ); - update_option( 'catalogx_wholesale_registration_settings', $previous_wholesale_registration_settings ); - - - delete_option('catalogx_enquiry-catalog-customization_settings'); - delete_option('catalogx_all-settings_settings'); - delete_option('catalogx_enquiry-quote-exclusion_settings'); - delete_option('catalogx_enquiry-form-customization_settings'); - delete_option('catalogx_enquiry-email-temp_settings'); - delete_option('catalogx_wholesale-registration_settings'); + $previous_enquiry_catalog_customization_settings = get_option( 'catalogx_enquiry-catalog-customization_settings', array() ); + if ( ! empty( $previous_enquiry_catalog_customization_settings ) ) { + update_option( 'catalogx_enquiry_catalog_customization_settings', $previous_enquiry_catalog_customization_settings ); + delete_option( 'catalogx_enquiry-catalog-customization_settings' ); + } + $previous_all_settings_settings = get_option( 'catalogx_all-settings_settings', array() ); + if ( ! empty( $previous_all_settings_settings ) ) { + update_option( 'catalogx_all_settings_settings', $previous_all_settings_settings ); + delete_option( 'catalogx_all-settings_settings' ); + } + $previous_enquiry_quote_exclusion_settings = get_option( 'catalogx_enquiry-quote-exclusion_settings', array() ); + if ( ! empty( $previous_enquiry_quote_exclusion_settings ) ) { + update_option( 'catalogx_enquiry_quote_exclusion_settings', $previous_enquiry_quote_exclusion_settings ); + delete_option( 'catalogx_enquiry-quote-exclusion_settings' ); + } + $previous_enquiry_form_customization_settings = get_option( 'catalogx_enquiry-form-customization_settings', array() ); + if ( ! empty( $previous_enquiry_form_customization_settings ) ) { + update_option( 'catalogx_enquiry_form_customization_settings', $previous_enquiry_form_customization_settings ); + delete_option( 'catalogx_enquiry-form-customization_settings' ); + } + $previous_enquiry_email_temp_settings = get_option( 'catalogx_enquiry-email-temp_settings', array() ); + if ( ! empty( $previous_enquiry_email_temp_settings ) ) { + update_option( 'catalogx_enquiry_email_temp_settings', $previous_enquiry_email_temp_settings ); + delete_option( 'catalogx_enquiry-email-temp_settings' ); + } + $previous_wholesale_registration_settings = get_option( 'catalogx_wholesale-registration_settings', array() ); + if ( ! empty( $previous_wholesale_registration_settings ) ) { + update_option( 'catalogx_wholesale_registration_settings', $previous_wholesale_registration_settings ); + delete_option( 'catalogx_wholesale-registration_settings' ); + } } } + /** + * Migrate old Catalog Enquiry data and settings to CatalogX. + * + * @return void + */ public function migrate_catalog_enquiry_to_catalogx() { - // module migration - $previous_settings = get_option( 'mvx_catalog_general_tab_settings', [] ); + // module migration. + $previous_settings = get_option( 'mvx_catalog_general_tab_settings', array() ); - // Enable enquiry module based on previous setting - if ( isset( $previous_settings[ 'is_enable_enquiry' ] ) && reset($previous_settings[ 'is_enable_enquiry' ]) === 'is_enable_enquiry' ) { - $active_modules = get_option( Modules::ACTIVE_MODULES_DB_KEY, [] ); - update_option( Modules::ACTIVE_MODULES_DB_KEY, array_unique( array_merge( $active_modules, ['enquiry'] ) ) ); + // Enable enquiry module based on previous setting. + if ( isset( $previous_settings['is_enable_enquiry'] ) && reset( $previous_settings['is_enable_enquiry'] ) === 'is_enable_enquiry' ) { + $active_modules = get_option( Modules::ACTIVE_MODULES_DB_KEY, array() ); + update_option( Modules::ACTIVE_MODULES_DB_KEY, array_unique( array_merge( $active_modules, array( 'enquiry' ) ) ) ); } - // migrate all enquiry and details from post table to enquiry table + // migrate all enquiry and details from post table to enquiry table. $this->migrate_database_table(); - // migrate all vendor settings + // migrate all vendor settings. $this->migrate_vendor_settings(); - - // migrate setttings - $this->migrate_old_settings(); + // migrate setttings. + $this->migrate_old_settings(); } - + /** - * Migrate database + * Migrate database + * * @return void */ public function migrate_database_table() { global $wpdb; try { - // Get enquiry post and post meta + // Get enquiry post and post meta. + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $enquirys_datas = $wpdb->get_results( "SELECT p.ID as id, p.post_author as user_id, @@ -374,24 +413,25 @@ public function migrate_database_table() { ); foreach ( $enquirys_datas as $id => $enquiry ) { - $product_ids = $enquiry[ 'product_ids' ]; - $product_quantitys = $enquiry[ 'product_quantitys' ]; + $product_ids = $enquiry['product_ids']; + $product_quantitys = $enquiry['product_quantitys']; + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery $wpdb->insert( "{$wpdb->prefix}" . Utill::TABLES['enquiry'], - [ - 'id' => $enquiry[ 'id' ], - 'user_id' => $enquiry[ 'user_id' ], - 'user_name' => $enquiry[ 'username' ], - 'user_email' => $enquiry[ 'useremail' ], - 'user_additional_fields' => $enquiry[ 'additional_fields' ], - 'date' => $enquiry[ 'date' ], - 'product_info' => serialize([ $product_ids => $product_quantitys ]), - ] + array( + 'id' => $enquiry['id'], + 'user_id' => $enquiry['user_id'], + 'user_name' => $enquiry['username'], + 'user_email' => $enquiry['useremail'], + 'user_additional_fields' => $enquiry['additional_fields'], + 'date' => $enquiry['date'], + 'product_info' => wp_json_encode( array( $product_ids => $product_quantitys ) ), + ) ); - // Delete the posts - wp_delete_post( $enquiry[ 'id' ], false ); + // Delete the posts. + wp_delete_post( $enquiry['id'], false ); } } catch ( \Exception $e ) { Utill::log( $e->getMessage() ); @@ -399,347 +439,363 @@ public function migrate_database_table() { } /** - * Migrate vendor settings + * Migrate vendor settings + * * @return void */ public function migrate_vendor_settings() { $vendors = get_users( array( 'role' => 'dc_vendor' ) ); foreach ( $vendors as $vendor ) { - // Check if the vendor has the meta key '_mvx_vendor_catalog_settings' + // Check if the vendor has the meta key '_mvx_vendor_catalog_settings'. $catalogx_vendor_settings = get_user_meta( $vendor->ID, '_mvx_vendor_catalog_settings', true ); if ( ! empty( $catalogx_vendor_settings['woocommerce_product_vendor_list'] ) && is_array( $catalogx_vendor_settings['woocommerce_product_vendor_list'] ) ) { $new_product_list = array(); - $index = 0; - + $index = 0; + foreach ( $catalogx_vendor_settings['woocommerce_product_vendor_list'] as $product_id ) { - // Get the product title (assuming these are product IDs) + // Get the product title (assuming these are product IDs). $product = wc_get_product( $product_id ); - + if ( $product ) { - // Create the new format for each product + // Create the new format for each product. $new_product_list[] = array( 'value' => $product_id, - 'label' => $product->get_name(), // Fetch the product name + 'label' => $product->get_name(), // Fetch the product name. 'index' => $index, ); - $index++; + ++$index; } } } if ( ! empty( $catalogx_vendor_settings['woocommerce_category_vendor_list'] ) && is_array( $catalogx_vendor_settings['woocommerce_category_vendor_list'] ) ) { $new_category_list = array(); - $index = 0; - + $index = 0; + foreach ( $catalogx_vendor_settings['woocommerce_category_vendor_list'] as $category_id ) { - // Get the category name using the category ID + // Get the category name using the category ID. $category = get_term( $category_id, 'product_cat' ); // 'product_cat' is the WooCommerce category taxonomy - + if ( $category ) { - // Create the new format for each category + // Create the new format for each category. $new_category_list[] = array( 'value' => $category_id, - 'label' => $category->name, // Fetch the category name + 'label' => $category->name, // Fetch the category name. 'index' => $index, ); - $index++; + ++$index; } } } - // If the meta key does not exist or is empty, create it with default values - if ( !empty( $catalogx_vendor_settings ) ) { + // If the meta key does not exist or is empty, create it with default values. + if ( ! empty( $catalogx_vendor_settings ) ) { $new_catalogx_vendor_settings = array( - 'selected_email_tpl' => $catalogx_vendor_settings['selected_email_tpl'] ? $catalogx_vendor_settings['selected_email_tpl'] : '', - 'woocommerce_product_list' => $new_product_list, + 'selected_email_tpl' => $catalogx_vendor_settings['selected_email_tpl'] ? $catalogx_vendor_settings['selected_email_tpl'] : '', + 'woocommerce_product_list' => $new_product_list, 'woocommerce_category_list' => $new_category_list, ); - // Update user meta with default catalog settings + // Update user meta with default catalog settings. update_user_meta( $vendor->ID, 'vendor_enquiry_settings', $new_catalogx_vendor_settings ); } } } /** - * Migrate old catalog settings + * Migrate old catalog settings + * * @return void */ - public function migrate_old_settings() { - $previous_general_settings = get_option( 'mvx_catalog_general_tab_settings', [] ); - $previous_button_settings = get_option( 'mvx_catalog_button_appearance_tab_settings', [] ); - - // Update product page builder - $page_builder_setting = [ - 'hide_product_price' => !empty($previous_general_settings[ 'is_remove_price_free' ]) ? true : false, - 'additional_input' => $previous_general_settings[ 'replace_text_in_price' ] ?? '', - 'enquiry_button' => [ - 'button_text_color' => !empty($previous_button_settings['custom_text_color']) ? $previous_button_settings['custom_text_color'] : '', - 'button_background_color_onhover' => !empty($previous_button_settings['custom_hover_background_color']) ? $previous_button_settings['custom_hover_background_color'] : '', - 'button_text_color_onhover' => !empty($previous_button_settings['custom_hover_text_color']) ? $previous_button_settings['custom_hover_text_color'] : '', - 'button_border_color' => !empty($previous_button_settings['custom_border_color']) ? $previous_button_settings['custom_border_color'] : '', - 'button_border_size' => !empty($previous_button_settings['custom_border_size']) ? $previous_button_settings['custom_border_size'] : '', - 'button_border_radious' => !empty($previous_button_settings['custom_border_radius']) ? $previous_button_settings['custom_border_radius'] : '', - 'button_text' => !empty($previous_button_settings['enquiry_button_text']) ? $previous_button_settings['enquiry_button_text'] : '', - 'button_font_size' => !empty($previous_button_settings['custom_font_size']) ? $previous_button_settings['custom_font_size'] : '', - ] - ]; + public function migrate_old_settings() { + $previous_general_settings = get_option( 'mvx_catalog_general_tab_settings', array() ); + $previous_button_settings = get_option( 'mvx_catalog_button_appearance_tab_settings', array() ); + + // Update product page builder. + $page_builder_setting = array( + 'hide_product_price' => ! empty( $previous_general_settings['is_remove_price_free'] ) ? true : false, + 'additional_input' => $previous_general_settings['replace_text_in_price'] ?? '', + 'enquiry_button' => array( + 'button_text_color' => ! empty( $previous_button_settings['custom_text_color'] ) ? $previous_button_settings['custom_text_color'] : '', + 'button_background_color_onhover' => ! empty( $previous_button_settings['custom_hover_background_color'] ) ? $previous_button_settings['custom_hover_background_color'] : '', + 'button_text_color_onhover' => ! empty( $previous_button_settings['custom_hover_text_color'] ) ? $previous_button_settings['custom_hover_text_color'] : '', + 'button_border_color' => ! empty( $previous_button_settings['custom_border_color'] ) ? $previous_button_settings['custom_border_color'] : '', + 'button_border_size' => ! empty( $previous_button_settings['custom_border_size'] ) ? $previous_button_settings['custom_border_size'] : '', + 'button_border_radious' => ! empty( $previous_button_settings['custom_border_radius'] ) ? $previous_button_settings['custom_border_radius'] : '', + 'button_text' => ! empty( $previous_button_settings['enquiry_button_text'] ) ? $previous_button_settings['enquiry_button_text'] : '', + 'button_font_size' => ! empty( $previous_button_settings['custom_font_size'] ) ? $previous_button_settings['custom_font_size'] : '', + ), + ); update_option( 'catalogx_enquiry-catalog-customization_settings', $page_builder_setting ); - - // Update shopping gurnal - $all_settings = [ - 'is_enable_out_of_stock' => $previous_general_settings[ 'is_enable_out_of_stock' ] ?? [], - 'enquiry_user_permission' => is_array($previous_general_settings['for_user_type']) && $previous_general_settings[ 'for_user_type' ]['value'] == '1' ? ['enquiry_logged_out'] : [], - 'is_page_redirect' => $previous_general_settings[ 'is_page_redirect' ] ?? [], - 'redirect_page_id' => $previous_general_settings[ 'redirect_page_id' ] ? $previous_general_settings[ 'redirect_page_id' ]['value'] : '', - 'is_disable_popup' => !empty($previous_general_settings[ 'is_disable_popup' ]) ? 'inline' : 'popup', - 'enable_cart_checkout' => [], - 'set_expiry_time' => 'Never', - 'is_enable_multiple_product_enquiry' => $previous_general_settings[ 'is_enable_multiple_product_enquiry' ] ?? ['is_enable_multiple_product_enquiry'], - ]; + // Update shopping gurnal. + $all_settings = array( + 'is_enable_out_of_stock' => $previous_general_settings['is_enable_out_of_stock'] ?? array(), + 'enquiry_user_permission' => is_array( $previous_general_settings['for_user_type'] ) && '1' === $previous_general_settings['for_user_type']['value'] ? array( 'enquiry_logged_out' ) : array(), + 'is_page_redirect' => $previous_general_settings['is_page_redirect'] ?? array(), + 'redirect_page_id' => $previous_general_settings['redirect_page_id'] ? $previous_general_settings['redirect_page_id']['value'] : '', + 'is_disable_popup' => ! empty( $previous_general_settings['is_disable_popup'] ) ? 'inline' : 'popup', + 'enable_cart_checkout' => array(), + 'set_expiry_time' => 'Never', + 'is_enable_multiple_product_enquiry' => $previous_general_settings['is_enable_multiple_product_enquiry'] ?? array( 'is_enable_multiple_product_enquiry' ), + ); update_option( 'catalogx_all-settings_settings', $all_settings ); - - $email_settings = [ - 'additional_alert_email' => !empty($previous_general_settings[ 'other_emails' ]) ? $previous_general_settings[ 'other_emails' ] : get_option( 'admin_email' ), - ]; - if (!empty($previous_general_settings['is_other_admin_mail'])) { + $email_settings = array( + 'additional_alert_email' => ! empty( $previous_general_settings['other_emails'] ) ? $previous_general_settings['other_emails'] : get_option( 'admin_email' ), + ); + + if ( ! empty( $previous_general_settings['is_other_admin_mail'] ) ) { $admin_email = CatalogX()->admin_email; - - // Convert the string into an array, remove the admin email, and convert back to a string - $emails_array = array_map('trim', explode(',', $email_settings['additional_alert_email'])); - $emails_array = array_diff($emails_array, [$admin_email]); - $email_settings['additional_alert_email'] = implode(', ', $emails_array); + + // Convert the string into an array, remove the admin email, and convert back to a string. + $emails_array = array_map( 'trim', explode( ',', $email_settings['additional_alert_email'] ) ); + $emails_array = array_diff( $emails_array, array( $admin_email ) ); + $email_settings['additional_alert_email'] = implode( ', ', $emails_array ); } update_option( 'catalogx_enquiry-email-temp_settings', $email_settings ); - // Update pages settings - $page_settings = [ - 'set_enquiry_cart_page' => intval( get_option( 'catalogx_enquiry_cart_page' ) ), - 'set_request_quote_page' => intval( get_option( 'catalogx_request_quote_page' ) ), - 'set_wholesale_products_page' => intval( get_option( 'wholesale_products_page' ) ) - ]; + // Update pages settings. + $page_settings = array( + 'set_enquiry_cart_page' => intval( get_option( 'catalogx_enquiry_cart_page' ) ), + 'set_request_quote_page' => intval( get_option( 'catalogx_request_quote_page' ) ), + 'set_wholesale_products_page' => intval( get_option( 'wholesale_products_page' ) ), + ); update_option( 'catalogx_pages_settings', $page_settings ); - - $tool_settings = [ - 'custom_css_product_page' => !empty($previous_button_settings[ 'custom_css_product_page' ]) ? $previous_button_settings[ 'custom_css_product_page' ] : '', - ]; + + $tool_settings = array( + 'custom_css_product_page' => ! empty( $previous_button_settings['custom_css_product_page'] ) ? $previous_button_settings['custom_css_product_page'] : '', + ); update_option( 'catalogx_tools_settings', $tool_settings ); - - // Update form settings - - // Free form migration - $previous_free_from_setting = get_option( 'mvx_catalog_enquiry_form_tab_settings', [] ); - - $free_form = [ - [ - 'key' => 'name', - 'label' => 'Enter your name', - 'active' => true, - ], - [ - 'key' => 'email', - 'label' => 'Enter your email', - 'active' => true, - ], - [ "key" => "phone" ], - [ "key" => "address" ], - [ "key" => "subject" ], - [ "key" => "comment" ], - [ "key" => "fileupload" ], - [ "key" => "filesize-limit" ], - [ "key" => "captcha" ], - ]; - - - $previous_free_from = $previous_free_from_setting[ 'enquiry_form_fileds' ]; + + // Update form settings. + + // Free form migration. + $previous_free_from_setting = get_option( 'mvx_catalog_enquiry_form_tab_settings', array() ); + + $free_form = array( + array( + 'key' => 'name', + 'label' => 'Enter your name', + 'active' => true, + ), + array( + 'key' => 'email', + 'label' => 'Enter your email', + 'active' => true, + ), + array( 'key' => 'phone' ), + array( 'key' => 'address' ), + array( 'key' => 'subject' ), + array( 'key' => 'comment' ), + array( 'key' => 'fileupload' ), + array( 'key' => 'filesize-limit' ), + array( 'key' => 'captcha' ), + ); + + $previous_free_from = $previous_free_from_setting['enquiry_form_fileds']; if ( is_array( $previous_free_from ) ) { - $previous_free_from_keys = array_column( $previous_free_from, 0 ); - $free_form = array_map( function ( $form ) use ( $previous_free_from, $previous_free_from_keys ) { - - // Get label key and active status key - $label_key = self::FREE_FORM_MAP[ $form[ 'key' ] ]; - $active_key = "{$label_key}_checkbox"; + $free_form = array_map( + function ( $form ) use ( $previous_free_from, $previous_free_from_keys ) { - $label_index = array_search( $label_key, $previous_free_from_keys ); - $active_index = array_search( $active_key, $previous_free_from_keys ); + // Get label key and active status key. + $label_key = self::FREE_FORM_MAP[ $form['key'] ]; + $active_key = "{$label_key}_checkbox"; - $label = $form[ 'label' ] ?? ''; - $active = $form[ 'active' ] ?? false; + $label_index = array_search( $label_key, $previous_free_from_keys, true ); + $active_index = array_search( $active_key, $previous_free_from_keys, true ); - $label = $label_index ? $previous_free_from[ $label_index ][ 1 ] : $label; - $active = $active_index ? $previous_free_from[ $active_index ][ 1 ] : $active; + $label = $form['label'] ?? ''; + $active = $form['active'] ?? false; - return [ - 'key' => $form[ 'key' ], - 'label' => $label, - 'active' => $active, - ]; + $label = $label_index ? $previous_free_from[ $label_index ][1] : $label; + $active = $active_index ? $previous_free_from[ $active_index ][1] : $active; - }, $free_form ); + return array( + 'key' => $form['key'], + 'label' => $label, + 'active' => $active, + ); + }, + $free_form + ); } - // Pro form migration - $previous_pro_from_setting = get_option( 'mvx_catalog_pro_enquiry_form_data', [] ); - - if ( !empty($previous_pro_from_setting) ) { - $pro_form = array_map( function ( $form ) { - return [ - ...$form, - 'name' => $form[ 'name' ], - 'type' => self::PRO_FORM_TYPE_MAP[ $form[ 'type' ] ] ?? $form[ 'type' ], - ]; - }, $previous_pro_from_setting ); + // Pro form migration. + $previous_pro_from_setting = get_option( 'mvx_catalog_pro_enquiry_form_data', array() ); + + if ( ! empty( $previous_pro_from_setting ) ) { + $pro_form = array_map( + function ( $form ) { + return array( + ...$form, + 'name' => $form['name'], + 'type' => self::PRO_FORM_TYPE_MAP[ $form['type'] ] ?? $form['type'], + ); + }, + $previous_pro_from_setting + ); } else { - $pro_form = [ - [ - 'id' => 1, - 'type' => 'title', - 'label' => 'Enquiry Form', - ], - [ + $pro_form = array( + array( + 'id' => 1, + 'type' => 'title', + 'label' => 'Enquiry Form', + ), + array( 'id' => 2, 'type' => 'text', 'label' => 'Enter your name', 'required' => true, 'placeholder' => 'I am default place holder', 'name' => 'name', - 'not_editable' => true - ], - [ + 'not_editable' => true, + ), + array( 'id' => 3, 'type' => 'email', 'label' => 'Enter your email', 'required' => true, 'placeholder' => 'I am default place holder', 'name' => 'email', - 'not_editable' => true - ], - ]; + 'not_editable' => true, + ), + ); } - $form_settings = [ - 'formsettings' => [ + $form_settings = array( + 'formsettings' => array( 'formfieldlist' => $pro_form, - 'butttonsetting' => [], - ], - 'freefromsetting' => $free_form, - ]; + 'butttonsetting' => array(), + ), + 'freefromsetting' => $free_form, + ); update_option( 'catalogx_enquiry-form-customization_settings', $form_settings ); - $wholesale_form = [ - [ - 'id' => 1, - 'type' => 'title', - 'label' => 'Wholesale Form', - ], - [ + $wholesale_form = array( + array( + 'id' => 1, + 'type' => 'title', + 'label' => 'Wholesale Form', + ), + array( 'id' => 2, 'type' => 'text', 'label' => 'Enter your name', 'required' => true, 'placeholder' => 'I am default place holder', 'name' => 'name', - 'not_editable' => true - ], - [ + 'not_editable' => true, + ), + array( 'id' => 3, 'type' => 'email', 'label' => 'Enter your email', 'required' => true, 'placeholder' => 'I am default place holder', 'name' => 'email', - 'not_editable' => true - ], - ]; + 'not_editable' => true, + ), + ); - $wholesale_from_settings = [ - 'wholesale_from_settings' => [ + $wholesale_from_settings = array( + 'wholesale_from_settings' => array( 'formfieldlist' => $wholesale_form, - 'butttonsetting' => [], - ], - ]; + 'butttonsetting' => array(), + ), + ); update_option( 'catalogx_wholesale-registration_settings', $wholesale_from_settings ); - // Update exclusion settings - $previous_exclusion_settings = get_option( 'mvx_catalog_exclusion_tab_settings', [] ); - - // Prepare exclusion user list - $exclusion_user_list = $previous_exclusion_settings[ 'woocommerce_user_list' ]; - $exclusion_user_list = is_array( $exclusion_user_list ) ? $exclusion_user_list : []; - - $exclusion_user_list = array_map(function ($user_list) { - return [ - 'key' => $user_list[ 'value' ], - 'label' => $user_list[ 'label' ], - 'value' => $user_list[ 'value' ], - ]; - }, $exclusion_user_list ); - - // Prepare user role list - $exclusion_userroles_list = $previous_exclusion_settings[ 'woocommerce_userroles_list' ]; - $exclusion_userroles_list = is_array( $exclusion_userroles_list ) ? $exclusion_userroles_list : []; - - $exclusion_userroles_list = array_map(function ($user_list) { - return [ - 'key' => $user_list[ 'value' ], - 'label' => $user_list[ 'label' ], - 'value' => $user_list[ 'value' ], - ]; - }, $exclusion_userroles_list ); - - // Prepare product list - $exclusion_product_list = $previous_exclusion_settings[ 'woocommerce_product_list' ]; - $exclusion_product_list = is_array( $exclusion_product_list ) ? $exclusion_product_list : []; - - $exclusion_product_list = array_map(function ($user_list) { - return [ - 'key' => $user_list[ 'value' ], - 'label' => $user_list[ 'label' ], - 'value' => $user_list[ 'value' ], - ]; - }, $exclusion_product_list ); - - // Prepare category list - $exclusion_category_list = $previous_exclusion_settings[ 'woocommerce_category_list' ]; - $exclusion_category_list = is_array( $exclusion_category_list ) ? $exclusion_category_list : []; - - $exclusion_category_list = array_map( function ( $user_list ) { - return [ - 'key' => $user_list[ 'value' ], - 'label' => $user_list[ 'label' ], - 'value' => $user_list[ 'value' ], - ]; - }, $exclusion_category_list ); - - $exclusion_settings = [ - 'catalog_exclusion_user_list' => $exclusion_user_list, - 'enquiry_exclusion_user_list' => $exclusion_user_list, - - 'catalog_exclusion_userroles_list' => $exclusion_userroles_list, - 'enquiry_exclusion_userroles_list' => $exclusion_userroles_list, - - 'enquiry_exclusion_product_list' => $exclusion_product_list, - 'catalog_exclusion_product_list' => $exclusion_product_list, - - 'catalog_exclusion_category_list' => $exclusion_category_list, - 'enquiry_exclusion_category_list' => $exclusion_category_list, - ]; + // Update exclusion settings. + $previous_exclusion_settings = get_option( 'mvx_catalog_exclusion_tab_settings', array() ); + + // Prepare exclusion user list. + $exclusion_user_list = $previous_exclusion_settings['woocommerce_user_list']; + $exclusion_user_list = is_array( $exclusion_user_list ) ? $exclusion_user_list : array(); + + $exclusion_user_list = array_map( + function ( $user_list ) { + return array( + 'key' => $user_list['value'], + 'label' => $user_list['label'], + 'value' => $user_list['value'], + ); + }, + $exclusion_user_list + ); + + // Prepare user role list. + $exclusion_userroles_list = $previous_exclusion_settings['woocommerce_userroles_list']; + $exclusion_userroles_list = is_array( $exclusion_userroles_list ) ? $exclusion_userroles_list : array(); + + $exclusion_userroles_list = array_map( + function ( $user_list ) { + return array( + 'key' => $user_list['value'], + 'label' => $user_list['label'], + 'value' => $user_list['value'], + ); + }, + $exclusion_userroles_list + ); + + // Prepare product list. + $exclusion_product_list = $previous_exclusion_settings['woocommerce_product_list']; + $exclusion_product_list = is_array( $exclusion_product_list ) ? $exclusion_product_list : array(); + + $exclusion_product_list = array_map( + function ( $user_list ) { + return array( + 'key' => $user_list['value'], + 'label' => $user_list['label'], + 'value' => $user_list['value'], + ); + }, + $exclusion_product_list + ); + + // Prepare category list. + $exclusion_category_list = $previous_exclusion_settings['woocommerce_category_list']; + $exclusion_category_list = is_array( $exclusion_category_list ) ? $exclusion_category_list : array(); + + $exclusion_category_list = array_map( + function ( $user_list ) { + return array( + 'key' => $user_list['value'], + 'label' => $user_list['label'], + 'value' => $user_list['value'], + ); + }, + $exclusion_category_list + ); + + $exclusion_settings = array( + 'catalog_exclusion_user_list' => $exclusion_user_list, + 'enquiry_exclusion_user_list' => $exclusion_user_list, + + 'catalog_exclusion_userroles_list' => $exclusion_userroles_list, + 'enquiry_exclusion_userroles_list' => $exclusion_userroles_list, + + 'enquiry_exclusion_product_list' => $exclusion_product_list, + 'catalog_exclusion_product_list' => $exclusion_product_list, + + 'catalog_exclusion_category_list' => $exclusion_category_list, + 'enquiry_exclusion_category_list' => $exclusion_category_list, + ); update_option( 'catalogx_enquiry-quote-exclusion_settings', $exclusion_settings ); - // delete previous option from database + // delete previous option from database. delete_option( 'mvx_catalog_general_tab_settings' ); delete_option( 'mvx_catalog_enquiry_form_tab_settings' ); delete_option( 'mvx_catalog_exclusion_tab_settings' ); @@ -751,4 +807,4 @@ public function migrate_old_settings() { delete_option( 'woocommerce_catalog_enquiry_exclusion_settings' ); delete_option( 'woocommerce_catalog_enquiry_button_appearence_settings' ); } -} \ No newline at end of file +} diff --git a/classes/Modules.php b/classes/Modules.php index 7a4d86c..5557e33 100644 --- a/classes/Modules.php +++ b/classes/Modules.php @@ -1,73 +1,93 @@ modules ) { - - $this->modules = apply_filters( 'catalogx_modules', [ - 'catalog' => [ - 'id' => 'catalog', - 'module_file' => CatalogX()->plugin_path . 'modules/Catalog/Module.php', - 'module_class' => 'CatalogX\Catalog\Module', - ], - 'enquiry' => [ - 'id' => 'enquiry', - 'module_file' => CatalogX()->plugin_path . 'modules/Enquiry/Module.php', - 'module_class' => 'CatalogX\Enquiry\Module', - ], - 'quote' => [ - 'id' => 'quote', - 'module_file' => CatalogX()->plugin_path . 'modules/Quote/Module.php', - 'module_class' => 'CatalogX\Quote\Module', - ], - ]); + $this->modules = apply_filters( + 'catalogx_modules', + array( + 'catalog' => array( + 'id' => 'catalog', + 'module_file' => CatalogX()->plugin_path . 'modules/Catalog/Module.php', + 'module_class' => 'CatalogX\Catalog\Module', + ), + 'enquiry' => array( + 'id' => 'enquiry', + 'module_file' => CatalogX()->plugin_path . 'modules/Enquiry/Module.php', + 'module_class' => 'CatalogX\Enquiry\Module', + ), + 'quote' => array( + 'id' => 'quote', + 'module_file' => CatalogX()->plugin_path . 'modules/Quote/Module.php', + 'module_class' => 'CatalogX\Quote\Module', + ), + ) + ); } return $this->modules; @@ -75,21 +95,23 @@ public function get_all_modules() { /** * Get all active modules + * * @return array */ public function get_active_modules() { - // If active modules are loaded return it + // If active modules are loaded return it. if ( $this->active_modules ) { return $this->active_modules; } - $this->active_modules = CatalogX()->setting->get_option( self::ACTIVE_MODULES_DB_KEY, [] ); + $this->active_modules = CatalogX()->setting->get_option( self::ACTIVE_MODULES_DB_KEY, array() ); return $this->active_modules; } /** * Load all active modules + * * @return void */ public function load_active_modules() { @@ -100,7 +122,7 @@ public function load_active_modules() { $license_active = Utill::is_khali_dabba(); $active_modules = $this->get_active_modules(); $all_modules = $this->get_all_modules(); - $activated_modules = []; + $activated_modules = array(); foreach ( $active_modules as $modules_id ) { if ( ! isset( $all_modules[ $modules_id ] ) ) { @@ -109,34 +131,33 @@ public function load_active_modules() { $module = $all_modules[ $modules_id ]; - - // Check if the module is available + // Check if the module is available. if ( ! $this->is_module_available( $module, $license_active ) ) { continue; } - // Store the module as active module + // Store the module as active module. if ( file_exists( $module['module_file'] ) ) { $activated_modules[] = $modules_id; } - // Activate the module - if ( file_exists( $module['module_file'] ) && ! in_array( $modules_id, $this->container ) ) { + // Activate the module. + if ( file_exists( $module['module_file'] ) && ! in_array( $modules_id, $this->container, true ) ) { require_once $module['module_file']; - $module_class = $module['module_class']; + $module_class = $module['module_class']; $this->container[ $modules_id ] = new $module_class(); /** * Module activation hook - * + * * @param object $name module object */ do_action( 'catalogx_activated_module_' . $modules_id, $this->container[ $modules_id ] ); } } - // store activated module as active module + // store activated module as active module. if ( $activated_modules !== $active_modules ) { update_option( self::ACTIVE_MODULES_DB_KEY, $activated_modules ); } @@ -146,20 +167,21 @@ public function load_active_modules() { /** * Check a perticular module is available or not. - * @param array $module - * @param bool $license_active + * + * @param array $module The module configuration array. + * @param bool $license_active Whether the license for pro modules is active. * @return bool */ private function is_module_available( $module, $license_active ) { $is_pro_module = $module['pro_module'] ?? false; - // if it is free module + // if it is free module. if ( ! $is_pro_module ) { return true; } - // if it is pro module - if( $is_pro_module && $license_active ) { + // if it is pro module. + if ( $is_pro_module && $license_active ) { return true; } @@ -168,6 +190,7 @@ private function is_module_available( $module, $license_active ) { /** * Get list of all module's id + * * @return array */ public function get_all_modules_ids() { @@ -177,15 +200,16 @@ public function get_all_modules_ids() { /** * Get all available modules. + * * @return array */ public function get_available_modules() { $modules = $this->get_all_modules(); $license_active = Utill::is_khali_dabba(); - $available_modules = []; + $available_modules = array(); foreach ( $modules as $module_id => $module ) { - // Check if the module is available + // Check if the module is available. if ( ! $this->is_module_available( $module, $license_active ) ) { continue; } @@ -200,7 +224,8 @@ public function get_available_modules() { /** * Activate modules - * @param array $modules + * + * @param array $modules The module name to activate. * @return array|mixed */ public function activate_modules( $modules ) { @@ -219,14 +244,15 @@ public function activate_modules( $modules ) { /** * Defactivate modules. - * @param array $modules - * @return void + * + * @param array $modules The module name to deactivate. + * @return array */ public function deactivate_modules( $modules ) { $active_modules = $this->get_active_modules(); foreach ( $modules as $module_id ) { - $active_modules = array_diff( $active_modules, [ $module_id ] ); + $active_modules = array_diff( $active_modules, array( $module_id ) ); } $active_modules = array_values( $active_modules ); @@ -236,24 +262,27 @@ public function deactivate_modules( $modules ) { update_option( self::ACTIVE_MODULES_DB_KEY, $this->active_modules ); add_action( - 'shutdown', function () use ( $modules ) { + 'shutdown', + function () use ( $modules ) { foreach ( $modules as $module_id ) { /** * Module deactivation hook + * * @param object $module deactivated module object */ - do_action( 'catalogx_deactivated_module_' . $module_id, $this->container[$module_id] ); + do_action( 'catalogx_deactivated_module_' . $module_id, $this->container[ $module_id ] ); } } ); return $this->active_modules; } - + /** * Get a module is available or not. - * @param mixed $module_id - * @return true + * + * @param mixed $module_id The id of the module to check. + * @return bool */ public function is_available( $module_id ) { $available_modules = $this->get_available_modules(); @@ -263,7 +292,8 @@ public function is_available( $module_id ) { /** * Check a module is active or not - * @param mixed $module_id + * + * @param mixed $module_id The id of the module to check. * @return bool */ public function is_active( $module_id ) { @@ -271,4 +301,4 @@ public function is_active( $module_id ) { return in_array( $module_id, $active_modules, true ); } -} \ No newline at end of file +} diff --git a/classes/Rest.php b/classes/Rest.php index d8cb510..b0bc4ad 100644 --- a/classes/Rest.php +++ b/classes/Rest.php @@ -1,15 +1,23 @@ rest_namespace, '/settings', [ - 'methods' => 'POST', - 'callback' => [ $this, 'set_settings' ], - 'permission_callback' => [ $this, 'catalogx_permission' ] - ] ); - - // enable/disable the module - register_rest_route( CatalogX()->rest_namespace, '/modules', [ - 'methods' => 'POST', - 'callback' => [ $this, 'set_modules' ], - 'permission_callback' => [ $this, 'catalogx_permission' ] - ] ); - - register_rest_route( CatalogX()->rest_namespace, '/tour', [ - [ - 'methods' => 'GET', - 'callback' => [ $this, 'get_tour' ], - 'permission_callback' => [ $this, 'catalogx_permission' ], - ], - [ - 'methods' => 'POST', - 'callback' => [ $this, 'set_tour' ], - 'permission_callback' => [ $this, 'catalogx_permission' ], - ] - ] ); - - register_rest_route( CatalogX()->rest_namespace, '/buttons', [ - 'methods' => 'GET', - 'callback' => [ $this, 'get_buttons' ], - 'permission_callback' => [ $this, 'catalogx_permission' ], - ]); - + public function register_rest_apis() { + + register_rest_route( + CatalogX()->rest_namespace, + '/settings', + array( + 'methods' => 'POST', + 'callback' => array( $this, 'set_settings' ), + 'permission_callback' => array( $this, 'catalogx_permission' ), + ) + ); + + // enable/disable the module. + register_rest_route( + CatalogX()->rest_namespace, + '/modules', + array( + array( + 'methods' => 'POST', + 'callback' => array( $this, 'set_modules' ), + 'permission_callback' => array( $this, 'catalogx_permission' ), + ), + array( + 'methods' => 'GET', + 'callback' => array( $this, 'get_modules' ), + 'permission_callback' => array( $this, 'catalogx_permission' ), + ), + ) + ); + + register_rest_route( + CatalogX()->rest_namespace, + '/tour', + array( + array( + 'methods' => 'GET', + 'callback' => array( $this, 'get_tour' ), + 'permission_callback' => array( $this, 'catalogx_permission' ), + ), + array( + 'methods' => 'POST', + 'callback' => array( $this, 'set_tour' ), + 'permission_callback' => array( $this, 'catalogx_permission' ), + ), + ) + ); + + register_rest_route( + CatalogX()->rest_namespace, + '/buttons', + array( + 'methods' => 'GET', + 'callback' => array( $this, 'get_buttons' ), + 'permission_callback' => array( $this, 'catalogx_permission' ), + ) + ); } /** - * get tour status + * Get tour status + * * @return \WP_Error|\WP_REST_Response */ public function get_tour() { - $status = CatalogX()->setting->get_option('catalogx_tour_active', false); - return ['active' => $status]; + $status = CatalogX()->setting->get_option( 'catalogx_tour_active', false ); + return array( 'active' => $status ); } - + /** - * set tour status - * @param mixed $request + * Set tour status + * + * active boolean required + * catalogx tour active or not + * + * @param \WP_REST_Request $request The REST request object. * @return \WP_Error|\WP_REST_Response */ - // active boolean required - // catalogx tour active or not - public function set_tour($request) { - update_option('catalogx_tour_active', $request->get_param( 'active' )); - return ['success' => true]; + public function set_tour( $request ) { + update_option( 'catalogx_tour_active', $request->get_param( 'active' ) ); + return array( 'success' => true ); } /** * Save global settings - * @param mixed $request + * + * setting array required + * all the settings of a particular id + * settingName string required + * Give the setting id + * + * @param \WP_REST_Request $request The REST request object. * @return \WP_Error|\WP_REST_Response */ - // setting array required - // all the settings of a particular id - // settingName string required - // Give the setting id public function set_settings( $request ) { - $all_details = []; - $get_settings_data = $request->get_param( 'setting' ); - $settingsname = $request->get_param( 'settingName' ); - $settingsname = str_replace( '-', '_', 'catalogx_' . $settingsname . '_settings' ); - // save the settings in database + $all_details = array(); + $get_settings_data = $request->get_param( 'setting' ); + $settingsname = $request->get_param( 'settingName' ); + $settingsname = str_replace( '-', '_', 'catalogx_' . $settingsname . '_settings' ); + + // save the settings in database. CatalogX()->setting->update_option( $settingsname, $get_settings_data ); do_action( 'catalogx_settings_after_save', $settingsname, $get_settings_data ); - $all_details[ 'error' ] = __( 'Settings Saved', 'catalogx' ); + $all_details['error'] = __( 'Settings Saved', 'catalogx' ); - //setup wizard settings - $action = $request->get_param('action'); + // setup wizard settings. + $action = $request->get_param( 'action' ); - if ($action == 'enquiry') { - $display_option = $request->get_param('displayOption'); - $restrict_user = $request->get_param('restrictUserEnquiry'); - CatalogX()->setting->update_setting('is_disable_popup', $display_option, 'catalogx_all_settings_settings'); - CatalogX()->setting->update_setting('enquiry_user_permission', $restrict_user, 'catalogx_all_settings_settings'); + if ( 'enquiry' === $action ) { + $display_option = $request->get_param( 'displayOption' ); + $restrict_user = $request->get_param( 'restrictUserEnquiry' ); + CatalogX()->setting->update_setting( 'is_disable_popup', $display_option, 'catalogx_all_settings_settings' ); + CatalogX()->setting->update_setting( 'enquiry_user_permission', $restrict_user, 'catalogx_all_settings_settings' ); } - - if ($action == 'quote') { - $restrict_user = $request->get_param('restrictUserQuote'); - CatalogX()->setting->update_setting('quote_user_permission', $restrict_user, 'catalogx_all_settings_settings'); + + if ( 'quote' === $action ) { + $restrict_user = $request->get_param( 'restrictUserQuote' ); + CatalogX()->setting->update_setting( 'quote_user_permission', $restrict_user, 'catalogx_all_settings_settings' ); } - return rest_ensure_response($all_details); + return rest_ensure_response( $all_details ); } /** * Manage module setting. Active or Deactive modules. - * @param mixed $request + * + * id string required + * Give the module id + * action string required + * Give the action that is activate or deactivate + * + * @param \WP_REST_Request $request The REST request object. * @return void */ - // id string required - // Give the module id - // action string required - // Give the action that is activate or deactivate public function set_modules( $request ) { - $moduleId = $request->get_param( 'id' ); - $action = $request->get_param( 'action' ); + $module_id = $request->get_param( 'id' ); + $action = $request->get_param( 'action' ); - // Setup wizard module - $modules = $request->get_param('modules'); - foreach ($modules as $module_id) { - CatalogX()->modules->activate_modules([$module_id]); + // Setup wizard module. + $modules = $request->get_param( 'modules' ); + foreach ( $modules as $module ) { + CatalogX()->modules->activate_modules( array( $module ) ); } - // Handle the actions + // Handle the actions. switch ( $action ) { case 'activate': - CatalogX()->modules->activate_modules([$moduleId]); + CatalogX()->modules->activate_modules( array( $module_id ) ); break; - + default: - CatalogX()->modules->deactivate_modules([$moduleId]); + CatalogX()->modules->deactivate_modules( array( $module_id ) ); break; } } - public function get_buttons($request) { - $product_id = $request->get_param('product_id'); - $button_type = $request->get_param('button_type'); + /** + * Get the list of active CatalogX modules. + * + * @return array + */ + public function get_modules() { + $modules_instance = new Modules(); + return $modules_instance->get_active_modules(); + } + + /** + * Get the enquiry or quote button markup. + * + * @param \WP_REST_Request $request The REST request object. + * @return \WP_REST_Response The HTML response. + */ + public function get_buttons( $request ) { + $product_id = $request->get_param( 'product_id' ); + $button_type = $request->get_param( 'button_type' ); - // Start output buffering + // Start output buffering. ob_start(); - if ($button_type == 'enquiry') { - EnquiryModule::init()->frontend->add_enquiry_button(intval($product_id)); + if ( 'enquiry' === $button_type ) { + EnquiryModule::init()->frontend->add_enquiry_button( intval( $product_id ) ); } - if ($button_type == 'quote') { - QuoteModule::init()->frontend->add_button_for_quote(intval($product_id)); + if ( 'quote' === $button_type ) { + QuoteModule::init()->frontend->add_button_for_quote( intval( $product_id ) ); } - do_action('catalogx_get_buttons', $button_type, $product_id); + do_action( 'catalogx_get_buttons', $button_type, $product_id ); - // Return the output - return rest_ensure_response(['html' => ob_get_clean()]); + // Return the output. + return rest_ensure_response( array( 'html' => ob_get_clean() ) ); } /** * Catalog rest api permission functions + * * @return bool */ public function catalogx_permission() { - return true; + return current_user_can( 'manage_options' ); } - -} \ No newline at end of file +} diff --git a/classes/Setting.php b/classes/Setting.php index fce1a19..1f1439c 100644 --- a/classes/Setting.php +++ b/classes/Setting.php @@ -1,177 +1,194 @@ load_settings(); } /** * Load all setting from option table - * @param mixed $fource + * + * @param bool $force Whether to force reload the settings even if already loaded. * @return void */ - private function load_settings( $fource = true ) { + private function load_settings( $force = true ) { - // If settings are loaded previously and not force to load - if ( ! $fource && $this->settings ) { + // If settings are loaded previously and not force to load. + if ( ! $force && $this->settings ) { return; } $setting_keys = $this->get_settings_keys(); - // Get all setting from option table - foreach( $setting_keys as $key) { - $this->settings[ $key ] = get_option( $key, [] ); + // Get all setting from option table. + foreach ( $setting_keys as $key ) { + $this->settings[ $key ] = get_option( $key, array() ); } } /** * Get all register setting key - * @return array + * + * @return array */ private function get_settings_keys() { - // Settings key are available + // Settings key are available. if ( $this->settings_keys ) { return $this->settings_keys; } /** * Filter for register settings key's + * * @var array setting keys */ - $this->settings_keys = apply_filters( 'catalogx_register_settings_keys', [ - 'catalogx_extra_settings', - 'catalogx_enquiry_catalog_customization_settings', - 'catalogx_all_settings_settings', - 'catalogx_tools_settings', - 'catalogx_pages_settings', - 'catalogx_enquiry_quote_exclusion_settings', - 'catalogx_enquiry_form_customization_settings', - 'catalogx_enquiry_email_temp_settings', - 'catalogx_wholesale_settings', - 'catalogx_wholesale_registration_settings' - ]); + $this->settings_keys = apply_filters( + 'catalogx_register_settings_keys', + array( + 'catalogx_extra_settings', + 'catalogx_enquiry_catalog_customization_settings', + 'catalogx_all_settings_settings', + 'catalogx_tools_settings', + 'catalogx_pages_settings', + 'catalogx_enquiry_quote_exclusion_settings', + 'catalogx_enquiry_form_customization_settings', + 'catalogx_enquiry_email_temp_settings', + 'catalogx_wholesale_settings', + 'catalogx_wholesale_registration_settings', + ) + ); return $this->settings_keys; } /** * Get the setting that was previously added. - * If setting is not present it return defalult value - * @param string $key setting key - * @param string $default setting value - * @param mixed $option_key option table's key + * If setting is not present it return defalult value + * + * @param string $key setting key. + * @param string $default_value setting value. + * @param mixed $option_key option table's key. * @return mixed */ - public function get_setting( $key, $default = '', $option_key = null ) { + public function get_setting( $key, $default_value = '', $option_key = null ) { - // If option key is not provided + // If option key is not provided. if ( ! $option_key ) { $option_key = $this->get_option_key( $key ); } - $setting = $this->settings[ $option_key ] ?? []; + $setting = $this->settings[ $option_key ] ?? array(); - return $setting[ $key ] ?? $default; + return $setting[ $key ] ?? $default_value; } /** * Update the setting that was already added. - * @param string $key setting key - * @param string $value setting value - * @param string $option_key option table's key + * + * @param string $key setting key. + * @param string $value setting value. + * @param string $option_key option table's key. * @return void */ public function update_setting( $key, $value, $option_key = null ) { - // If option key is not provided + // If option key is not provided. if ( ! $option_key ) { $option_key = $this->get_option_key( $key ); } - // Get the setting array from setting settings container - $setting = $this->settings[ $option_key ] ?? []; + // Get the setting array from setting settings container. + $setting = $this->settings[ $option_key ] ?? array(); - // Update setting in setting container - $setting[ $key ] = $value; + // Update setting in setting container. + $setting[ $key ] = $value; $this->settings[ $option_key ] = $setting; - // Update the setting in database - update_option( $option_key, $setting ); + // Update the setting in database. + update_option( $option_key, $setting ); } /** - * Get the option - * @param string $key - * @param mixed $default + * Get the option + * + * @param string $key The key for the setting to retrieve. * @return mixed value */ - public function get_option( $key, $default = [] ) { + public function get_option( $key ) { - // Check key exist in register settings keys - if ( in_array( $key, $this->get_settings_keys() ) ) { + // Check key exist in register settings keys. + if ( in_array( $key, $this->get_settings_keys(), true ) ) { return $this->settings[ $key ]; } - return get_option( $key, [] ); + return get_option( $key, array() ); } /** * Update the value in option table. - * If key does't exist it create it. - * @param string $key - * @param mixed $value + * If key doesn't exist it create it. + * + * @param string $key The option key to update. + * @param mixed $value The value to set for the given key. * @return void */ public function update_option( $key, $value ) { - - // Check key exist in register settings keys - if ( in_array( $key, $this->get_settings_keys() ) ) { - - // Update the container + + // Check key exist in register settings keys. + if ( in_array( $key, $this->get_settings_keys(), true ) ) { + + // Update the container. $this->settings[ $key ] = $value; } - // Update the option + // Update the option. update_option( $key, $value ); } /** * Find option key from setting container - * @param mixed $key setting key + * + * @param mixed $key setting key. * @return string */ private function get_option_key( $key ) { - foreach ( $this->settings as $option_key => $setting ) { + foreach ( $this->settings as $option_key => $setting ) { // Key exist in a particular setting. if ( is_array( $setting ) && array_key_exists( $key, $setting ) ) { return $option_key; @@ -180,4 +197,4 @@ private function get_option_key( $key ) { return 'catalogx_extra_settings'; } -} \ No newline at end of file +} diff --git a/classes/SetupWizard.php b/classes/SetupWizard.php index f5d3e21..9f099b4 100644 --- a/classes/SetupWizard.php +++ b/classes/SetupWizard.php @@ -1,55 +1,76 @@
id === 'dashboard_page_catalogx-setup' ) { - wp_enqueue_script('setup-wizard-script', CatalogX()->plugin_url . 'build/blocks/setupWizard/index.js', [ 'jquery', 'jquery-blockui', 'wp-element', 'wp-i18n', 'react-jsx-runtime' ], CatalogX()->version, true); + if ( 'dashboard_page_catalogx-setup' === $current_screen->id ) { + wp_enqueue_script( 'setup-wizard-script', CatalogX()->plugin_url . FrontendScripts::get_build_path_name() . 'js/block/setupWizard/index.js', array( 'jquery', 'jquery-blockui', 'wp-element', 'wp-i18n', 'react-jsx-runtime' ), CatalogX()->version, true ); wp_set_script_translations( 'setup-wizard-script', 'catalogx' ); - wp_enqueue_style('setup-wizard-style', CatalogX()->plugin_url . 'build/blocks/setupWizard/index.css'); + wp_enqueue_style( 'setup-wizard-style', CatalogX()->plugin_url . FrontendScripts::get_build_path_name() . 'styles/block/setupWizard/index.css', array(), CatalogX()->version ); wp_localize_script( - 'setup-wizard-script', 'appLocalizer', [ - 'apiurl' => untrailingslashit(get_rest_url()), - 'nonce' => wp_create_nonce( 'wp_rest' ), - 'restUrl' => CatalogX()->rest_namespace, - 'redirect_url' => admin_url() . 'admin.php?page=catalogx#&tab=modules', - ]); + 'setup-wizard-script', + 'appLocalizer', + array( + 'apiUrl' => untrailingslashit( get_rest_url() ), + 'nonce' => wp_create_nonce( 'wp_rest' ), + 'restUrl' => CatalogX()->rest_namespace, + 'redirect_url' => admin_url() . 'admin.php?page=catalogx#&tab=modules', + ) + ); } } - } diff --git a/classes/Shortcode.php b/classes/Shortcode.php index af2dfe7..ad7b612 100644 --- a/classes/Shortcode.php +++ b/classes/Shortcode.php @@ -1,32 +1,49 @@ modules->is_active('quote')) { + if ( CatalogX()->modules->is_active( 'quote' ) ) { FrontendScripts::load_scripts(); - FrontendScripts::localize_scripts('catalogx-quote-cart-script'); - FrontendScripts::enqueue_script('catalogx-quote-cart-script' ); - FrontendScripts::enqueue_style('catalogx-quote-cart-style'); + FrontendScripts::localize_scripts( 'catalogx-quote-cart-script' ); + FrontendScripts::enqueue_script( 'catalogx-quote-cart-script' ); + FrontendScripts::enqueue_style( 'catalogx-frontend-style' ); } } + /** + * Display the request quote container and enqueue necessary frontend scripts. + * + * @return string HTML output for the request quote section. + */ public function display_request_quote() { $this->frontend_scripts(); ob_start(); @@ -36,5 +53,4 @@ public function display_request_quote() { 'catalogx_enquiry', 'rule' => 'catalogx_rules', 'message' => 'catalogx_messages', - ]; + ); /** * Function to console and debug errors. + * + * @param mixed $data The data to be logged (string, array, object, etc.). + * @return void */ - public static function log( $str ) { - $file = apply_filters( 'catalogx_log_file_path', CatalogX()->plugin_path . 'log/catalogx.txt' ); + public static function log( $data ) { + if ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) { + return; + } + require_once ABSPATH . 'wp-admin/includes/file.php'; + WP_Filesystem(); - if ( file_exists( $file ) ) { - // Open the file to get existing content - $str = var_export( $str, true ); + global $wp_filesystem; - // Wp_remote_gate replacement required - $current = file_get_contents( $file ); + $log_file = CatalogX()->plugin_path . 'log/catalogx.log'; + $message = wp_json_encode( $data, JSON_PRETTY_PRINT ) . "\n---------------------------\n"; - if ( $current ) { - // Append a new content to the file - $current .= "$str" . "\r\n"; - $current .= "-------------------------------------\r\n"; - } else { - $current = "$str" . "\r\n"; - $current .= "-------------------------------------\r\n"; - } - - // Write the contents back to the file - file_put_contents( $file, $current ); - } + $existing = $wp_filesystem->exists( $log_file ) ? $wp_filesystem->get_contents( $log_file ) : ''; + $wp_filesystem->put_contents( $log_file, $existing . $message, FS_CHMOD_FILE ); } /** * Check is Catalog Pro is active or not. + * * @return bool */ public static function is_khali_dabba() { - if ( defined( 'CATALOGX_PRO_PLUGIN_TOKEN' ) ) { - return CatalogX_Pro()->license->is_active(); - } - - return false; + return apply_filters( 'kothay_dabba', false ); } /** * Get other templates ( e.g. product attributes ) passing attributes and including the file. * * @access public - * @param mixed $template_name - * @param array $args ( default: array() ) + * @param mixed $template_name The name of the template file to load. + * @param array $args ( default: array() ) . * @return void */ - public static function get_template( $template_name, $args = [] ) { - - if ( $args && is_array( $args ) ) { - extract( $args ); - } - - // Check if the template exists in the theme + public static function get_template( $template_name, $args = array() ) { + + // Check if the template exists in the theme. $theme_template = get_stylesheet_directory() . '/woocommerce-catalog-enquiry/' . $template_name; - - // Use the theme template if it exists, otherwise use the plugin template + + // Use the theme template if it exists, otherwise use the plugin template. $located = file_exists( $theme_template ) ? $theme_template : CatalogX()->plugin_path . 'templates/' . $template_name; - - // Load the template + + // Load the template. load_template( $located, false, $args ); } - + /** * Create atachment from array of fiels. - * @param mixed $files_array + * + * @param mixed $files_array Array representing the uploaded file. * @return int|\WP_Error */ - public static function create_attachment_from_files_array($files_array) { - require_once(ABSPATH . 'wp-admin/includes/file.php'); - require_once(ABSPATH . 'wp-admin/includes/image.php'); - require_once(ABSPATH . 'wp-admin/includes/media.php'); - - // Handle the file upload - $upload = wp_handle_upload($files_array, array('test_form' => false)); - - - // Prepare the attachment + public static function create_attachment_from_files_array( $files_array ) { + require_once ABSPATH . 'wp-admin/includes/file.php'; + require_once ABSPATH . 'wp-admin/includes/image.php'; + require_once ABSPATH . 'wp-admin/includes/media.php'; + + // Handle the file upload. + $upload = wp_handle_upload( $files_array, array( 'test_form' => false ) ); + + // Prepare the attachment. $file_path = $upload['file']; - $file_name = basename($file_path); - $file_type = wp_check_filetype($file_name, null); + $file_name = basename( $file_path ); + $file_type = wp_check_filetype( $file_name, null ); - // Create attachment post + // Create attachment post. $attachment = array( - 'guid' => $upload['url'], + 'guid' => $upload['url'], 'post_mime_type' => $file_type['type'], - 'post_title' => preg_replace('/\.[^.]+$/', '', $file_name), - 'post_content' => '', - 'post_status' => 'inherit' + 'post_title' => preg_replace( '/\.[^.]+$/', '', $file_name ), + 'post_content' => '', + 'post_status' => 'inherit', ); - // Insert attachment into the media library - $attachment_id = wp_insert_attachment($attachment, $file_path); + // Insert attachment into the media library. + $attachment_id = wp_insert_attachment( $attachment, $file_path ); - if (!is_wp_error($attachment_id)) { - // Generate metadata for the attachment, and update the attachment - $attachment_data = wp_generate_attachment_metadata($attachment_id, $file_path); - wp_update_attachment_metadata($attachment_id, $attachment_data); + if ( ! is_wp_error( $attachment_id ) ) { + // Generate metadata for the attachment, and update the attachment. + $attachment_data = wp_generate_attachment_metadata( $attachment_id, $file_path ); + wp_update_attachment_metadata( $attachment_id, $attachment_data ); - return $attachment_id; // Return the attachment ID + return $attachment_id; // Return the attachment ID. } return 0; @@ -131,67 +125,90 @@ public static function create_attachment_from_files_array($files_array) { /** * Check the plugin is active or not + * + * @param string $name Optional. Plugin name to check. Supported: 'notifima', 'multivendorx'. * @return bool */ - public static function is_active_plugin($name = '') { + public static function is_active_plugin( $name = '' ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; - if ($name == 'notifima') { - return is_plugin_active('woocommerce-product-stock-alert/product_stock_alert.php'); + if ( 'notifima' === $name ) { + return is_plugin_active( 'woocommerce-product-stock-alert/product_stock_alert.php' ); } - - if ($name == 'multivendorx') { - return is_plugin_active('dc-woocommerce-multi-vendor/dc_product_vendor.php'); + + if ( 'multivendorx' === $name ) { + return is_plugin_active( 'dc-woocommerce-multi-vendor/dc_product_vendor.php' ); } - + return false; } /** * WPML support for language translation - * @param mixed $context - * @param mixed $name - * @param mixed $default - * @return mixed + * + * @param string $context The translation context (used by WPML). + * @param string $name The name of the string to translate. + * @param string $default_value The default string to return if no translation is found. + * @return string The translated string. */ - public static function get_translated_string($context, $name, $default ) { + public static function get_translated_string( $context, $name, $default_value ) { if ( function_exists( 'icl_t' ) ) { - return icl_t( $context, $name, $default ); + return icl_t( $context, $name, $default_value ); } else { - return __( $default, $context ); + return __( $default_value, $context ); } } - public static function get_button_styles($button_settings, $hover = false) { - $button_css = ""; - $border_size = !empty($button_settings['button_border_size']) ? esc_html($button_settings['button_border_size']) . 'px' : '1px'; - - if ($hover) { - if ( isset( $button_settings[ 'button_background_color_onhover' ] ) ) - $button_css .= !empty( $button_settings[ 'button_background_color_onhover' ] ) ? 'background: ' . $button_settings[ 'button_background_color_onhover' ] . ' !important;' : ''; - if ( isset( $button_settings[ 'button_text_color_onhover' ] ) ) - $button_css .= !empty( $button_settings[ 'button_text_color_onhover' ] ) ? ' color: ' . $button_settings[ 'button_text_color_onhover' ] . ' !important;' : ''; - if ( isset( $button_settings[ 'button_border_color_onhover' ] ) ) - $button_css .= !empty( $button_settings[ 'button_border_color_onhover' ] ) ? 'border: ' . $border_size . ' solid' . $button_settings[ 'button_border_color_onhover' ] . ' !important;' : ''; + /** + * Generate inline CSS styles for button. + * + * Applies styles based on provided settings and whether the button is in a hover state. + * + * @param array $button_settings An associative array of button styling options. + * @param bool $hover Optional. Whether to apply hover styles. Default false. + * @return string CSS string to apply inline on the button. + */ + public static function get_button_styles( $button_settings, $hover = false ) { + $button_css = ''; + $border_size = ! empty( $button_settings['button_border_size'] ) ? esc_html( $button_settings['button_border_size'] ) . 'px' : '1px'; + + if ( $hover ) { + if ( isset( $button_settings['button_background_color_onhover'] ) ) { + $button_css .= ! empty( $button_settings['button_background_color_onhover'] ) ? 'background: ' . $button_settings['button_background_color_onhover'] . ' !important;' : ''; + } + if ( isset( $button_settings['button_text_color_onhover'] ) ) { + $button_css .= ! empty( $button_settings['button_text_color_onhover'] ) ? ' color: ' . $button_settings['button_text_color_onhover'] . ' !important;' : ''; + } + if ( isset( $button_settings['button_border_color_onhover'] ) ) { + $button_css .= ! empty( $button_settings['button_border_color_onhover'] ) ? 'border: ' . $border_size . ' solid' . $button_settings['button_border_color_onhover'] . ' !important;' : ''; + } } else { - if (!empty($button_settings['button_background_color'])) - $button_css .= "background: " . esc_html($button_settings['button_background_color']) . ";"; - if (!empty($button_settings['button_text_color'])) - $button_css .= "color: " . esc_html($button_settings['button_text_color']) . ";"; - if (!empty($button_settings['button_border_color'])) - $button_css .= "border: " . $border_size . " solid " . esc_html($button_settings['button_border_color']) . ";"; - if (!empty($button_settings['button_font_size'])) - $button_css .= "font-size: " . esc_html($button_settings['button_font_size']) . "px;"; - if (!empty($button_settings['button_border_radious'])) - $button_css .= "border-radius: " . esc_html($button_settings['button_border_radious']) . "px;"; - if (!empty($button_settings['button_font_width'])) - $button_css .= "font-weight: " . esc_html($button_settings['button_font_width']) . "px;"; - if (!empty($button_settings['button_padding'])) - $button_css .= "padding: " . esc_html($button_settings['button_padding']) . "px;"; - if (!empty($button_settings['button_margin'])) - $button_css .= "margin: " . esc_html($button_settings['button_margin']) . "px;"; + if ( ! empty( $button_settings['button_background_color'] ) ) { + $button_css .= 'background: ' . esc_html( $button_settings['button_background_color'] ) . ';'; + } + if ( ! empty( $button_settings['button_text_color'] ) ) { + $button_css .= 'color: ' . esc_html( $button_settings['button_text_color'] ) . ';'; + } + if ( ! empty( $button_settings['button_border_color'] ) ) { + $button_css .= 'border: ' . $border_size . ' solid ' . esc_html( $button_settings['button_border_color'] ) . ';'; + } + if ( ! empty( $button_settings['button_font_size'] ) ) { + $button_css .= 'font-size: ' . esc_html( $button_settings['button_font_size'] ) . 'px;'; + } + if ( ! empty( $button_settings['button_border_radious'] ) ) { + $button_css .= 'border-radius: ' . esc_html( $button_settings['button_border_radious'] ) . 'px;'; + } + if ( ! empty( $button_settings['button_font_width'] ) ) { + $button_css .= 'font-weight: ' . esc_html( $button_settings['button_font_width'] ) . 'px;'; + } + if ( ! empty( $button_settings['button_padding'] ) ) { + $button_css .= 'padding: ' . esc_html( $button_settings['button_padding'] ) . 'px;'; + } + if ( ! empty( $button_settings['button_margin'] ) ) { + $button_css .= 'margin: ' . esc_html( $button_settings['button_margin'] ) . 'px;'; + } } return $button_css; } -} \ No newline at end of file +} diff --git a/modules/Catalog/Admin.php b/modules/Catalog/Admin.php index 6752f20..f0d3f33 100644 --- a/modules/Catalog/Admin.php +++ b/modules/Catalog/Admin.php @@ -1,68 +1,78 @@ - __( 'Catalog', 'catalogx' ), 'target' => 'catalog_per_product_desc', - 'class' => [ 'show_if_simple', 'show_if_variable', 'show_if_grouped', 'show_if_external' ], + 'class' => array( 'show_if_simple', 'show_if_variable', 'show_if_grouped', 'show_if_external' ), 'priority' => 50, - ]; + ); return $tabs; } /** * Content of custom field in product tab. + * * @return void */ public function catalog_product_data_fields() { global $post; - // Retrieve existing value from the database + // Retrieve existing value from the database. $catalog_product_desc = get_post_meta( $post->ID, 'catalog_per_product_desc', true ); ?>
- 'catalog_per_product_desc', - 'label' => __( 'Additional Product Details', 'catalogx' ), - 'description' => __( 'Enter extra information about the product to display on the single product page. Useful for highlighting unique features, usage tips, or care instructions.', 'catalogx' ), - 'desc_tip' => true, - 'type' => 'text', - 'value' => $catalog_product_desc ?? '', - ] ); + 'catalog_per_product_desc', + 'label' => __( 'Additional Product Details', 'catalogx' ), + 'description' => __( 'Enter extra information about the product to display on the single product page. Useful for highlighting unique features, usage tips, or care instructions.', 'catalogx' ), + 'desc_tip' => true, + 'type' => 'text', + 'value' => $catalog_product_desc ?? '', + ) + ); ?>
@@ -71,12 +81,13 @@ public function catalog_product_data_fields() { /** * Save custom product data for wholesale product. - * @param mixed $post_id + * + * @param mixed $post_id The ID of the product being saved. * @return void */ public function save_catalog_product_data_fields( $post_id ) { - $catalog_product_desc = filter_input(INPUT_POST, 'catalog_per_product_desc', FILTER_SANITIZE_FULL_SPECIAL_CHARS) ?? ''; - + $catalog_product_desc = filter_input( INPUT_POST, 'catalog_per_product_desc', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ?? ''; + update_post_meta( $post_id, 'catalog_per_product_desc', wc_clean( $catalog_product_desc ) ); } } \ No newline at end of file diff --git a/modules/Catalog/Frontend.php b/modules/Catalog/Frontend.php index ef03529..adefdbc 100644 --- a/modules/Catalog/Frontend.php +++ b/modules/Catalog/Frontend.php @@ -1,89 +1,100 @@ -register_description_box(); + add_action( 'woocommerce_single_product_summary', array( $this, 'exclusion_for_single_product_page' ), 5 ); + // register description box. + $this->register_description_box(); } - + /** * Redirect cart and checkout page to home page + * * @return void */ public static function catalogx_redirect_page() { - // For exclusion + // For exclusion. foreach ( WC()->cart->get_cart() as $cart_item ) { $product_id = $cart_item['product_id']; - if ( ! Util::is_available_for_product( $product_id ) ) { + if ( ! Util::is_available_for_product( $product_id ) ) { return; } } - - // Get setting for sales enabled + + // Get setting for sales enabled. $sales_enabled = CatalogX()->setting->get_setting( 'enable_cart_checkout' ); - // Check sales enabled setting is enable or not - if ( !empty($sales_enabled) ) return; + // Check sales enabled setting is enable or not. + if ( ! empty( $sales_enabled ) ) { + return; + } - // Get cart and checkout page id - $cart_page_id = wc_get_page_id( 'cart' ); - $checkout_page_id = wc_get_page_id( 'checkout' ); + // Get cart and checkout page id. + $cart_page_id = wc_get_page_id( 'cart' ); + $checkout_page_id = wc_get_page_id( 'checkout' ); - // Redirect to redirect url if page is cart page or checkout page + // Redirect to redirect url if page is cart page or checkout page. if ( is_page( $cart_page_id ) || is_page( $checkout_page_id ) ) { - wp_redirect( home_url() ); + wp_safe_redirect( home_url() ); exit; } } /** - * Display single product page descrioption box + * Display single product page descrioption box + * * @return void */ public static function show_description_box() { global $post; - if ( ! Util::is_available_for_product( $post->ID ) ) { + if ( ! Util::is_available_for_product( $post->ID ) ) { return; } ?>
- ID, 'catalog_per_product_desc', true ); - $input_box = !empty($catalog_per_product_desc) ? $catalog_per_product_desc : CatalogX()->setting->get_setting( 'additional_input' ); - if ($input_box) { ?> + $input_box = ! empty( $catalog_per_product_desc ) ? $catalog_per_product_desc : CatalogX()->setting->get_setting( 'additional_input' ); + if ( $input_box ) { + ?>
- +
@@ -92,93 +103,99 @@ public static function show_description_box() { /** * Price exclusion for shop page - * @return void + * + * @param string $price The original price HTML. + * @param \WC_Product $product The WooCommerce product object. + * @return string The modified (or original) price HTML. */ - public function exclude_price_for_selected_product( $price, $product ) { $price_hide_product_page = CatalogX()->setting->get_setting( 'hide_product_price' ); - + if ( Util::is_available_for_product( $product->get_id() ) && $price_hide_product_page && is_shop() ) { return ''; } - + return $price; } /** * Shop page add to cart button exclusion for block - * @return void + * + * @param string $button The HTML for the add to cart button. + * @param \WC_Product $product The WooCommerce product object. + * @return string The modified (or original) button HTML. */ public function exclude_add_to_cart_button( $button, $product ) { if ( ! Util::is_available_for_product( $product->get_id() ) ) { return $button; } - + return empty( CatalogX()->setting->get_setting( 'enable_cart_checkout' ) ) ? '' : $button; - } /** * Single product page add to cart button exclusion + * * @return void */ - public function exclusion_for_single_product_page() { + public function exclusion_for_single_product_page() { global $post; if ( Util::is_available_for_product( $post->ID ) && is_product() ) { - if ( empty(CatalogX()->setting->get_setting( 'enable_cart_checkout' )) ) { - // remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 ); + if ( empty( CatalogX()->setting->get_setting( 'enable_cart_checkout' ) ) ) { remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 ); - // for block support - remove_action('woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30); + // for block support. + remove_action( 'woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30 ); } } } /** * Register description box for display in shop page + * * @return void */ public function register_description_box() { - - // Get shop page button settings - $position_settings = CatalogX()->setting->get_setting( 'shop_page_possition_setting', [] ); - // Priority of colide position + // Get shop page button settings. + $position_settings = CatalogX()->setting->get_setting( 'shop_page_possition_setting', array() ); + + // Priority of colide position. $possiton_priority = 1; - // Possiotion after a particular section - $possition_after = 'sku_category'; + // Possiotion after a particular section. + $possition_after = 'sku_category'; - // If possition settings exists + // If possition settings exists. if ( $position_settings ) { - // Get the colide possition priority - $possiton_priority = array_search( 'additional_input', array_keys( $position_settings ) ) + 1; + // Get the colide possition priority. + $possiton_priority = array_search( 'additional_input', array_keys( $position_settings ), true ) + 1; - // Get the possition after - $possition_after = $position_settings[ 'additional_input' ]; + // Get the possition after. + $possition_after = $position_settings['additional_input']; } - - // Display button group in a hooked based on possition setting + + // Display button group in a hooked based on possition setting. switch ( $possition_after ) { case 'sku_category': - add_action( 'woocommerce_product_meta_end', [ self::class, 'display_description_box' ], 99 + $possiton_priority ); + add_action( 'woocommerce_product_meta_end', array( self::class, 'display_description_box' ), 99 + $possiton_priority ); break; case 'add_to_cart': case 'product_description': - add_action( 'woocommerce_product_meta_start', [ self::class, 'display_description_box' ], 99 + $possiton_priority ); + add_action( 'woocommerce_product_meta_start', array( self::class, 'display_description_box' ), 99 + $possiton_priority ); break; case 'price_section': - add_action( 'woocommerce_single_product_summary', [ self::class, 'display_description_box' ], 10 + $possiton_priority ); + add_action( 'woocommerce_single_product_summary', array( self::class, 'display_description_box' ), 10 + $possiton_priority ); break; default: - add_action( 'woocommerce_single_product_summary', [ self::class, 'display_description_box' ], 6 + $possiton_priority ); + add_action( 'woocommerce_single_product_summary', array( self::class, 'display_description_box' ), 6 + $possiton_priority ); break; } } - /** + /** * Display descriopton box + * * @return void */ public static function display_description_box() { diff --git a/modules/Catalog/Module.php b/modules/Catalog/Module.php index 69fded2..66149e7 100644 --- a/modules/Catalog/Module.php +++ b/modules/Catalog/Module.php @@ -1,24 +1,31 @@ -init_classes(); do_action( 'load_premium_catalog_module' ); - } /** * Init helper classes + * * @return void */ public function init_classes() { - $this->container[ 'util' ] = new Util(); - $this->container[ 'fontend' ] = new Frontend(); - $this->container[ 'admin' ] = new Admin(); + $this->container['util'] = new Util(); + $this->container['fontend'] = new Frontend(); + $this->container['admin'] = new Admin(); } /** * Magic getter function to get the reference of class. - * Accept class name, If valid return reference, else Wp_Error. - * @param mixed $class + * Accept class name, If valid return reference, else Wp_Error. + * + * @param mixed $class Name of the class to retrieve from the container. * @return object | \WP_Error */ - public function __get( $class ) { + public function __get( $class ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.classFound if ( array_key_exists( $class, $this->container ) ) { return $this->container[ $class ]; } - return new \WP_Error( sprintf('Call to unknown class %s.', $class ) ); + return new \WP_Error( sprintf( 'Call to unknown class %s.', $class ) ); } /** @@ -63,7 +71,7 @@ public function __get( $class ) { * @param string $class The class name or key to store the instance. * @param object $value The instance of the class to store. */ - public function __set( $class, $value ) { + public function __set( $class, $value ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.classFound $this->container[ $class ] = $value; } @@ -71,11 +79,11 @@ public function __set( $class, $value ) { * Initializes Catalog class. * Checks for an existing instance * And if it doesn't find one, create it. - * @param mixed $file + * * @return object | null */ public static function init() { - if ( self::$instance === null ) { + if ( null === self::$instance ) { self::$instance = new self(); } diff --git a/modules/Catalog/Util.php b/modules/Catalog/Util.php index 3a077b2..ff54d45 100644 --- a/modules/Catalog/Util.php +++ b/modules/Catalog/Util.php @@ -1,50 +1,62 @@ -setting->get_option( 'catalogx_enquiry_quote_exclusion_settings', [] ); + // Get exclusion setting. + $catalog_exclusion_setting = CatalogX()->setting->get_option( 'catalogx_enquiry_quote_exclusion_settings', array() ); + + // Get userroll exclusion settings. + $userroles_exclusion_settings = isset( $catalog_exclusion_setting['catalog_exclusion_userroles_list'] ) ? $catalog_exclusion_setting['catalog_exclusion_userroles_list'] : array(); - // Get userroll exclusion settings - $userroles_exclusion_settings = isset($catalog_exclusion_setting['catalog_exclusion_userroles_list']) ? $catalog_exclusion_setting[ 'catalog_exclusion_userroles_list' ] : []; - - // Get excluded user roles - $exclude_user_roles = array_map( function( $userrole ) { - return $userrole[ 'key' ]; - }, $userroles_exclusion_settings ); + // Get excluded user roles. + $exclude_user_roles = array_map( + function ( $userrole ) { + return $userrole['key']; + }, + $userroles_exclusion_settings + ); - // Check current user's role is in exclude user roles + // Check current user's role is in exclude user roles. if ( array_intersect( $exclude_user_roles, $current_user->roles ) ) { return false; } - - // Get user exclusion settings - $userlist_exclusion_settings = isset($catalog_exclusion_setting['catalog_exclusion_user_list']) ? $catalog_exclusion_setting[ 'catalog_exclusion_user_list' ] : []; - // Get excluded user ids - $exclude_user_ids = array_map( function( $userid ) { - return $userid[ 'key' ]; - }, $userlist_exclusion_settings ); + // Get user exclusion settings. + $userlist_exclusion_settings = isset( $catalog_exclusion_setting['catalog_exclusion_user_list'] ) ? $catalog_exclusion_setting['catalog_exclusion_user_list'] : array(); - // Check current user's id is in exclude user id - if ( in_array( $current_user->ID, $exclude_user_ids ) ) { + // Get excluded user ids. + $exclude_user_ids = array_map( + function ( $userid ) { + return (int) $userid['key']; + }, + $userlist_exclusion_settings + ); + + // Check current user's id is in exclude user id. + if ( in_array( $current_user->ID, $exclude_user_ids, true ) ) { return false; } @@ -53,67 +65,86 @@ public static function is_available() { /** * Check catalog functionlity available for product + * + * @param int $product_id The ID of the product to check. * @return bool */ - public static function is_available_for_product($product_id) { - // Get exclusion setting - $catalog_exclusion_setting = CatalogX()->setting->get_option( 'catalogx_enquiry_quote_exclusion_settings', [] ); - - // Get product exclusion settings - $product_exclusion_settings = isset($catalog_exclusion_setting['catalog_exclusion_product_list']) ? $catalog_exclusion_setting['catalog_exclusion_product_list'] : []; - - // Get excluded products - $exclude_products = array_map( function( $product ) { - return $product[ 'key' ]; - }, $product_exclusion_settings ); - - - // Check current product id is in exclude products - if ( in_array( $product_id, $exclude_products ) ) { + public static function is_available_for_product( $product_id ) { + // Get exclusion setting. + $catalog_exclusion_setting = CatalogX()->setting->get_option( 'catalogx_enquiry_quote_exclusion_settings', array() ); + + // Get product exclusion settings. + $product_exclusion_settings = isset( $catalog_exclusion_setting['catalog_exclusion_product_list'] ) ? $catalog_exclusion_setting['catalog_exclusion_product_list'] : array(); + + // Get excluded products. + $exclude_products = array_map( + function ( $product ) { + return $product['key']; + }, + $product_exclusion_settings + ); + + // Check current product id is in exclude products. + if ( in_array( $product_id, $exclude_products, true ) ) { return false; } - // Get category exclusion settings - $category_exclusion_settings = isset($catalog_exclusion_setting['catalog_exclusion_category_list']) ? $catalog_exclusion_setting['catalog_exclusion_category_list'] : []; - - // Get excluded category - $exclude_categories = array_filter(array_map(function($category) use ($product_id) { - $term_list = wp_get_post_terms($product_id, 'product_cat', ['fields' => 'ids']); - return $category['key'] == $term_list[0] ? $product_id : null; - }, $category_exclusion_settings)); - - // Check current product id is in exclude categories - if ( in_array( $product_id, $exclude_categories ) ) { + // Get category exclusion settings. + $category_exclusion_settings = isset( $catalog_exclusion_setting['catalog_exclusion_category_list'] ) ? $catalog_exclusion_setting['catalog_exclusion_category_list'] : array(); + + // Get excluded category. + $exclude_categories = array_filter( + array_map( + function ( $category ) use ( $product_id ) { + $term_list = wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'ids' ) ); + return $category['key'] === $term_list[0] ? $product_id : null; + }, + $category_exclusion_settings + ) + ); + + // Check current product id is in exclude categories. + if ( in_array( $product_id, $exclude_categories, true ) ) { return false; } - // Get tag exclusion settings - $tag_exclusion_settings = isset($catalog_exclusion_setting['catalog_exclusion_tag_list']) ? $catalog_exclusion_setting['catalog_exclusion_tag_list'] : []; - - // Get excluded tag - $exclude_tags = array_filter(array_map( function( $tag ) use ($product_id) { - $tag_term_list = wp_get_post_terms($product_id,'product_tag',['fields'=>'ids']); - return $tag[ 'key' ] == (!empty($tag_term_list) && $tag_term_list[0]) ? $product_id : null; - }, $tag_exclusion_settings )); - - // Check current product id is in exclude tags - if ( in_array( $product_id, $exclude_tags ) ) { + // Get tag exclusion settings. + $tag_exclusion_settings = isset( $catalog_exclusion_setting['catalog_exclusion_tag_list'] ) ? $catalog_exclusion_setting['catalog_exclusion_tag_list'] : array(); + + // Get excluded tag. + $exclude_tags = array_filter( + array_map( + function ( $tag ) use ( $product_id ) { + $tag_term_list = wp_get_post_terms( $product_id, 'product_tag', array( 'fields' => 'ids' ) ); + return ( ! empty( $tag_term_list ) && $tag_term_list[0] ) == $tag['key'] ? $product_id : null; + }, + $tag_exclusion_settings + ) + ); + + // Check current product id is in exclude tags. + if ( in_array( $product_id, $exclude_tags, true ) ) { return false; } - // Get brand exclusion settings - $brand_exclusion_settings = isset($catalog_exclusion_setting['catalog_exclusion_brand_list']) ? $catalog_exclusion_setting['catalog_exclusion_brand_list'] : []; - // Get excluded brand - $exclude_brands = array_filter(array_map( function( $tag ) use ($product_id) { - $brand_term_list = wp_get_post_terms($product_id,'product_brand',['fields'=>'ids']); - return (!empty($brand_term_list) && in_array($tag['key'], $brand_term_list)) ? $product_id : null; - }, $brand_exclusion_settings )); - - // Check current product id is in exclude brands - if ( in_array( $product_id, $exclude_brands ) ) { + // Get brand exclusion settings. + $brand_exclusion_settings = isset( $catalog_exclusion_setting['catalog_exclusion_brand_list'] ) ? $catalog_exclusion_setting['catalog_exclusion_brand_list'] : array(); + // Get excluded brand. + $exclude_brands = array_filter( + array_map( + function ( $tag ) use ( $product_id ) { + $brand_term_list = wp_get_post_terms( $product_id, 'product_brand', array( 'fields' => 'ids' ) ); + return ( ! empty( $brand_term_list ) && in_array( $tag['key'], $brand_term_list, true ) ) ? $product_id : null; + }, + $brand_exclusion_settings + ) + ); + + // Check current product id is in exclude brands. + if ( in_array( $product_id, $exclude_brands, true ) ) { return false; } + return true; } - -} \ No newline at end of file +} diff --git a/modules/Enquiry/Ajax.php b/modules/Enquiry/Ajax.php index 3f2be67..ecddd4e 100644 --- a/modules/Enquiry/Ajax.php +++ b/modules/Enquiry/Ajax.php @@ -1,36 +1,45 @@ setting->get_setting( 'enquiry_user_permission', [] ); - if ( !empty($display_enquiry_button) && !is_user_logged_in()) { + $display_enquiry_button = CatalogX()->setting->get_setting( 'enquiry_user_permission', array() ); + if ( ! empty( $display_enquiry_button ) && ! is_user_logged_in() ) { return; } if ( empty( CatalogX()->setting->get_setting( 'enable_cart_checkout' ) ) ) { - add_action( 'woocommerce_after_shop_loop_item', [$this, 'add_button_in_shop_page'] ); + add_action( 'woocommerce_after_shop_loop_item', array( $this, 'add_button_in_shop_page' ) ); } - add_action( 'display_shop_page_button', [ $this, 'catalogx_add_enquiry_button' ] ); + add_action( 'display_shop_page_button', array( $this, 'catalogx_add_enquiry_button' ) ); - //Hook for exclusion - add_action( 'woocommerce_single_product_summary', [ $this, 'enquiry_button_exclusion' ], 5); + // Hook for exclusion. + add_action( 'woocommerce_single_product_summary', array( $this, 'enquiry_button_exclusion' ), 5 ); - add_action( 'wp_enqueue_scripts', [ $this, 'frontend_scripts' ] ); + add_action( 'wp_enqueue_scripts', array( $this, 'frontend_scripts' ) ); - // Enquiry button shortcode - add_shortcode( 'catalogx_enquiry_button', [ $this, 'catalogx_enquiry_button_shortcode' ] ); + // Enquiry button shortcode. + add_shortcode( 'catalogx_enquiry_button', array( $this, 'catalogx_enquiry_button_shortcode' ) ); } + /** + * Adds enquiry button on shop page via hook. + * + * @return void + */ public function catalogx_add_enquiry_button() { global $product; - if (empty(trim(CatalogX()->render_enquiry_btn_via))) { + if ( empty( trim( CatalogX()->render_enquiry_btn_via ) ) ) { CatalogX()->render_enquiry_btn_via = 'hook'; - $this->add_enquiry_button($product->get_id()); + $this->add_enquiry_button( $product->get_id() ); } } /** - * Add enquiry button + * Add enquiry button in single product page and shop page + * + * @param int|\WC_Product|null $product_obj Product object or ID. Falls back to global $product if null. * @return void */ - public function add_enquiry_button($productObj) { + public function add_enquiry_button( $product_obj ) { global $product; - $productObj = is_int($productObj) ? wc_get_product($productObj) : ($productObj ?: $product); + $product_obj = is_int( $product_obj ) ? wc_get_product( $product_obj ) : ( $product_obj ? $product_obj : $product ); - if ( empty( $productObj ) ) + if ( empty( $product_obj ) ) { return; + } - if ( CatalogX()->setting->get_setting( 'is_enable_multiple_product_enquiry' ) && Utill::is_khali_dabba() ) { + if ( apply_filters( 'catalogx_enable_multiple_product_enquiry', false ) ) { return; } - $button_settings = CatalogX()->setting->get_setting( 'enquiry_button', [] ); - $button_css = Utill::get_button_styles($button_settings); - $button_hover_css = Utill::get_button_styles($button_settings, true); - + $button_settings = CatalogX()->setting->get_setting( 'enquiry_button', array() ); + $button_css = Utill::get_button_styles( $button_settings ); + $button_hover_css = Utill::get_button_styles( $button_settings, true ); + if ( $button_hover_css ) { echo ''; } $additional_css_settings = CatalogX()->setting->get_setting( 'custom_css_product_page' ); - if (isset($additional_css_settings) && !empty($additional_css_settings)) { + if ( isset( $additional_css_settings ) && ! empty( $additional_css_settings ) ) { $button_css .= $additional_css_settings; } - - $button_settings[ 'button_text' ] = !empty( $button_settings[ 'button_text' ] ) ? $button_settings[ 'button_text' ] : \CatalogX\Utill::get_translated_string( 'catalogx', 'send_an_enquiry', 'Send an enquiry' ); - $button_position_settings = CatalogX()->setting->get_setting( 'shop_page_button_position_setting', [] ); - $position = array_search('enquiry_button', $button_position_settings); - $position = $position !== false ? $position : 0; + + $button_settings['button_text'] = ! empty( $button_settings['button_text'] ) ? $button_settings['button_text'] : \CatalogX\Utill::get_translated_string( 'catalogx', 'send_an_enquiry', 'Send an enquiry' ); + $button_position_settings = CatalogX()->setting->get_setting( 'shop_page_button_position_setting', array() ); + $position = array_search( 'enquiry_button', $button_position_settings, true ); + $position = false !== $position ? $position : 0; ?>
- setting->get_setting( 'is_enable_out_of_stock' ) ){ - if ( !$productObj->managing_stock() && !$productObj->is_in_stock()) { ?> -
- + setting->get_setting( 'is_enable_out_of_stock' ) ) { + if ( ! $product_obj->managing_stock() && ! $product_obj->is_in_stock() ) { + ?> +
+
-
- + } + } else { + ?> +
+
- - - - - + } + ?> + + + + +
-