plugin_slug; } /** * Return an instance of this class. * * @return object A single instance of this class. * @since 1.0 * */ public static function get_instance() { // If the single instance hasn't been set, set it now. if (null == self::$instance) { self::$instance = new self; } return self::$instance; } /** * Fired when the plugin is activated. * * @param boolean $network_wide True if WPMU superadmin uses * "Network Activate" action, false if * WPMU is disabled or plugin is * activated on an individual blog. * @since 1.0 * */ public static function activate($network_wide) { if (function_exists('is_multisite') && is_multisite()) { if ($network_wide) { // Get all blog ids $blog_ids = self::get_blog_ids(); foreach ($blog_ids as $blog_id) { switch_to_blog($blog_id); self::single_activate(); } restore_current_blog(); } else { self::single_activate(); } } else { self::single_activate(); } } /** * Fired when the plugin is deactivated. * * @param boolean $network_wide True if WPMU superadmin uses * "Network Deactivate" action, false if * WPMU is disabled or plugin is * deactivated on an individual blog. * @since 1.0 * */ public static function deactivate($network_wide) { if (function_exists('is_multisite') && is_multisite()) { if ($network_wide) { // Get all blog ids $blog_ids = self::get_blog_ids(); foreach ($blog_ids as $blog_id) { switch_to_blog($blog_id); self::single_deactivate(); } restore_current_blog(); } else { self::single_deactivate(); } } else { self::single_deactivate(); } } /** * Fired when a new site is activated with a WPMU environment. * * @param int $blog_id ID of the new blog. * @since 1.0 * */ public function activate_new_site($blog_id) { if (1 !== did_action('wpmu_new_blog')) { return; } switch_to_blog($blog_id); self::single_activate(); restore_current_blog(); } /** * Get all blog ids of blogs in the current network that are: * - not archived * - not spam * - not deleted * * @return array|false The blog ids, false if no matches. * @since 1.0 * */ private static function get_blog_ids() { global $wpdb; // get an array of blog ids // Multisite blogs table - direct query is necessary for network-wide activation. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching return $wpdb->get_col( $wpdb->prepare( "SELECT blog_id FROM {$wpdb->blogs} WHERE archived = %s AND spam = %s AND deleted = %s", '0', '0', '0' ) ); } /** * Fired for each blog when the plugin is activated. * * @since 1.0 */ private static function single_activate() { global $wpdb; $wpdb->hide_errors(); $collate = ''; if ($wpdb->has_cap('collation')) { if (!empty($wpdb->charset)) { $collate .= "DEFAULT CHARACTER SET $wpdb->charset"; } if (!empty($wpdb->collate)) { $collate .= " COLLATE $wpdb->collate"; } } require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); $table = " CREATE TABLE IF NOT EXISTS {$wpdb->prefix}stock_alert ( `ID` bigint(255) NOT NULL AUTO_INCREMENT, `date` varchar(100) COLLATE utf8_czech_ci NOT NULL, `name` longtext COLLATE utf8_czech_ci NOT NULL, `surname` longtext COLLATE utf8_czech_ci NOT NULL, `email` varchar(100) COLLATE utf8_czech_ci NOT NULL, `productid` varchar(10) COLLATE utf8_czech_ci NOT NULL, `send` int(2) COLLATE utf8_czech_ci NOT NULL, `senddate` varchar(100) COLLATE utf8_czech_ci NOT NULL, PRIMARY KEY (`ID`), KEY `productid` (`productid`), KEY `email` (`email`), KEY `date` (`date`) ) $collate; "; dbDelta($table); } /** * Fired for each blog when the plugin is deactivated. * * @since 1.0 */ private static function single_deactivate() { } /** * Load the plugin text domain for translation. * * @since 1.0 */ public function load_plugin_textdomain() { $domain = $this->plugin_slug; // WordPress core filter hook - part of WordPress API. // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound $locale = apply_filters('plugin_locale', get_locale(), $domain); load_textdomain($domain, AFFINITEPSADIR . 'languages/' . $domain . '-' . $locale . '.mo'); } /** * Register and enqueue public-facing style sheet. * * @since 1.0 */ public function enqueue_styles() { wp_enqueue_style( $this->plugin_slug . '-popup', AFFINITEPSAURL . 'public/assets/css/popup.min.css', array(), self::VERSION ); wp_enqueue_style( $this->plugin_slug . '-plugin-style', AFFINITEPSAURL . 'public/assets/css/public.min.css', array(), self::VERSION ); } /** * Register and enqueues public-facing JavaScript files. * * @since 1.0 */ public function enqueue_scripts() { if ( !is_checkout() ) { wp_enqueue_script( $this->plugin_slug . '-plugin-script', AFFINITEPSAURL . 'public/assets/js/public.min.js', array(), self::VERSION, true ); wp_localize_script($this->plugin_slug . '-plugin-script', 'wcp_localize', array( 'ajaxurl' => admin_url() . 'admin-ajax.php', 'homeurl' => home_url(), 'wc_ajax_url' => WC_AJAX::get_endpoint("%%endpoint%%"), 'undefined_name' => esc_html__('Please fill your name', 'affinite-back-in-stock'), 'undefined_secondname' => esc_html__('Please fill your second name', 'affinite-back-in-stock'), 'undefined_email' => esc_html__('Please fill your e-mail', 'affinite-back-in-stock'), 'not_valid_email' => esc_html__('Please enter a valid e-mail', 'affinite-back-in-stock') )); } } /** * Force WooCommerce to load email template from plugin * * @since 1.0.0 */ public function toret_locate_template($template, $template_name, $template_path) { if ($template_name == 'stock-alert-admin-email.php') { $template = AFFINITEPSADIR . 'includes/emails/stock-alert-admin-email.php'; } elseif ($template_name == 'stock-alert-admin-email-plain.php') { $template = AFFINITEPSADIR . 'includes/emails/stock-alert-admin-email-plain.php'; } if ($template_name == 'stock-alert-customer-email.php') { $template = AFFINITEPSADIR . 'includes/emails/stock-alert-customer-email.php'; } elseif ($template_name == 'stock-alert-customer-email-plain.php') { $template = AFFINITEPSADIR . 'includes/emails/stock-alert-customer-email-plain.php'; } if ($template_name == 'stock-alert-customer-save-email.php') { $template = AFFINITEPSADIR . 'includes/emails/stock-alert-customer-save-email.php'; } elseif ($template_name == 'stock-alert-customer-save-email-plain.php') { $template = AFFINITEPSADIR . 'includes/emails/stock-alert-customer-save-email-plain.php'; } return $template; } /** * Render stock alert button * * @since 1.0 */ public function stock_alert_button() { $option = get_option('affinite_stock_alert'); if ( !empty( $option['product-button-text'] ) ) { $button_text = $option['product-button-text']; } else { $button_text = esc_html__( 'Notify me when this product is available', 'affinite-back-in-stock' ); } global $product; if ( ! $product || ! is_a( $product, 'WC_Product' ) ) { return; } if ('variable' == $product->get_type()) { $available_variations = $product->get_available_variations(); if ( empty( $available_variations ) ) { ?> is_in_stock() ) { ?>
' . esc_html( $duplicite_email_subtitle ) . '
'; $html .= '' . esc_html( $duplicite_email_button_text ) . '
'; $reponse = array( 'status' => 'duplicate', 'html' => $html ); wp_send_json( $reponse ); } else { $data = array(); $data['date'] = time(); $data['name'] = $name; $data['surname'] = $surname; $data['email'] = $email; $data['productid'] = $productid; //Not send $data['send'] = 1; $data['senddate'] = 'no'; global $wpdb; // Custom plugin table - direct query is necessary for data insertion. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery $wpdb->insert($wpdb->prefix . 'stock_alert', $data); // Custom plugin table - direct query is necessary. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching $number = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( ID ) FROM " . $wpdb->prefix . "stock_alert WHERE productid = %d AND send = '1'", array( $productid ) ) ); $option = get_option('affinite_stock_alert'); if( !empty( $option['product_count'] ) && $number > $option['product_count'] ){ //Send email to admin!!! WC()->mailer(); if ( file_exists( AFFINITEPSADIR . 'includes/class-stock-alert-admin-email.php' ) ) { require_once(AFFINITEPSADIR . 'includes/class-stock-alert-admin-email.php'); if ( class_exists( 'Affinite_Alert_Admin_Email' ) ) { $send = new Affinite_Alert_Admin_Email(); $mail = $send->trigger($productid); } } } //Send email to customer WC()->mailer(); if ( file_exists( AFFINITEPSADIR . 'includes/class-stock-alert-customer-save-email.php' ) ) { require_once(AFFINITEPSADIR . 'includes/class-stock-alert-customer-save-email.php'); if ( class_exists( 'Affinite_Alert_Customer_Save_Email' ) ) { $send = new Affinite_Alert_Customer_Save_Email(); $mail = $send->trigger($productid, $email); } } $html = '' . esc_html( $success_subtitle ) . '
'; $html .= '' . esc_html( $success_button_text ) . '
'; $reponse = array( 'status' => 'success', 'html' => $html ); wp_send_json( $reponse ); } } } /** * Send alert * * @since 1.0 */ public function send_alert(WC_Product $product){ if ($this->is_product_in_stock($product)) { if ('variation' == $product->get_type()) { $product_id = $product->get_parent_id(); } else { $product_id = $product->get_id(); } global $wpdb; // Custom plugin table - direct query is necessary. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching $data = $wpdb->get_results($wpdb->prepare( "SELECT * FROM " . $wpdb->prefix . "stock_alert WHERE productid = %d AND send = 1", array( $product_id ) )); if (!empty($data)) { WC()->mailer(); if ( file_exists( AFFINITEPSADIR . 'includes/class-stock-alert-customer-email.php' ) ) { require_once(AFFINITEPSADIR . 'includes/class-stock-alert-customer-email.php'); if ( class_exists( 'Affinite_Alert_Customer_Email' ) ) { $send = new Affinite_Alert_Customer_Email(); $mail = $send->trigger($product_id); } } } } } /** * Check if product is in stock * * @since 1.0 */ private function is_product_in_stock($product){ $in_stock = false; if ($product !== null && $product instanceof WC_Product) { if ($product->is_type('variable')) { $variantions = $product->get_children(); foreach ($variantions as $variantion) { $var_product = wc_get_product($variantion); $stock_quantity = $var_product->get_stock_quantity(); if ($stock_quantity > 0) { $in_stock = true; continue; } } return $in_stock; } else { return $product->get_stock_quantity() > 0; } } return $in_stock; } }//End class