From bc49ce5cad4cead2c5525f35b6f43ec83489f9a6 Mon Sep 17 00:00:00 2001 From: Marcin Pietrzak Date: Fri, 13 Dec 2024 17:36:37 +0100 Subject: [PATCH] simplifying the code --- ...ass-iworks-omnibus-integration-commons.php | 2 +- ...iworks-omnibus-integration-woocommerce.php | 106 ++++-------------- .../v4/class-iworks-omnibus-integration.php | 43 ++----- 3 files changed, 37 insertions(+), 114 deletions(-) diff --git a/includes/iworks/omnibus/integration/v4/class-iworks-omnibus-integration-commons.php b/includes/iworks/omnibus/integration/v4/class-iworks-omnibus-integration-commons.php index b453734..da4da0e 100644 --- a/includes/iworks/omnibus/integration/v4/class-iworks-omnibus-integration-commons.php +++ b/includes/iworks/omnibus/integration/v4/class-iworks-omnibus-integration-commons.php @@ -64,7 +64,7 @@ public function action_shutdown_maybe_delete_older_records() { */ $query = $wpdb->prepare( sprintf( - 'DELETE FROM %s WHERE created < ( CURRENT_DATE - INTERVAL %%d DAY )', + 'DELETE FROM %s WHERE price_sale_from < ( CURRENT_DATE - INTERVAL %%d DAY )', $wpdb->iworks_omnibus ), max( 31, intval( get_option( $this->get_name( 'days_delete' ) ) ) ) diff --git a/includes/iworks/omnibus/integration/v4/class-iworks-omnibus-integration-woocommerce.php b/includes/iworks/omnibus/integration/v4/class-iworks-omnibus-integration-woocommerce.php index 3615855..c6c344a 100644 --- a/includes/iworks/omnibus/integration/v4/class-iworks-omnibus-integration-woocommerce.php +++ b/includes/iworks/omnibus/integration/v4/class-iworks-omnibus-integration-woocommerce.php @@ -51,8 +51,7 @@ public function __construct() { */ add_action( 'iworks_omnibus_wc_lowest_price_message', array( $this, 'action_get_message' ) ); add_filter( 'iworks_omnibus_get_name', array( $this, 'get_name' ) ); - add_filter( 'iworks_omnibus_message_template', array( $this, 'filter_iworks_omnibus_message_template_for_admin_list' ) ); - add_filter( 'iworks_omnibus_message_template', array( $this, 'filter_iworks_omnibus_message_template_for_product' ), 10, 3 ); + add_filter( 'iworks_omnibus_message_template', array( $this, 'filter_iworks_omnibus_message_template_for_admin_list' ), 10, 6 ); /** * action to call save_price_history() * @@ -119,7 +118,7 @@ public function __construct() { * * @since 2.1.5 */ - if ( 'yes' === get_option( $this->get_name( 'cart' ), 'no' ) ) { + if ( $this->is_on( get_option( $this->get_name( 'cart' ), 'no' ) ) ) { add_filter( 'woocommerce_cart_item_price', array( $this, 'filter_woocommerce_cart_item_price' ), 10, 3 ); } /** @@ -234,10 +233,6 @@ public function action_admin_init() { add_action( 'admin_enqueue_scripts', array( $this, 'action_admin_enqueue_scripts_register' ) ); add_action( 'load-woocommerce_page_wc-settings', array( $this, 'enqueue_scripts' ) ); add_action( 'admin_head', array( $this, 'action_admin_head' ) ); - /** - * add extra data to price log - */ - add_filter( 'iworks_omnibus_add_price_log_data', array( $this, 'ilter_add_price_log_data' ), 10, 2 ); } public function enqueue_scripts() { @@ -317,7 +312,7 @@ private function should_it_show_up( $post_id ) { if ( isset( $_POST['action'] ) ) { if ( 'woocommerce_load_variations' === $_POST['action'] ) { check_ajax_referer( 'load-variations', 'security' ); - if ( 'yes' === get_option( $this->get_name( 'admin_edit' ), 'yes' ) ) { + if ( $this->is_on( get_option( $this->get_name( 'admin_edit' ), 'yes' ) ) ) { return apply_filters( 'iworks_omnibus_show', true ); } } @@ -325,12 +320,12 @@ private function should_it_show_up( $post_id ) { } elseif ( function_exists( 'get_current_screen' ) ) { $screen = get_current_screen(); if ( 'product' === $screen->id ) { - if ( 'yes' === get_option( $this->get_name( 'admin_edit' ), 'yes' ) ) { + if ( $this->is_on( get_option( $this->get_name( 'admin_edit' ), 'yes' ) )) { return apply_filters( 'iworks_omnibus_show', true ); } } if ( 'edit-product' === $screen->id ) { - if ( 'yes' === get_option( $this->get_name( 'admin_list' ), 'no' ) ) { + if ($this->is_on( get_option( $this->get_name( 'admin_list' ), 'no' ) )) { return apply_filters( 'iworks_omnibus_show', true ); } } @@ -340,9 +335,9 @@ private function should_it_show_up( $post_id ) { /** * front-end short term good */ - if ( 'yes' === get_option( $this->get_name( 'admin_short' ), 'no' ) ) { - if ( 'yes' === get_post_meta( $post_id, $this->get_name( 'is_short' ), true ) ) { - if ( 'no' === get_option( $this->get_name( 'short_message' ), 'no' ) ) { + if ( $this->is_on( get_option( $this->get_name( 'admin_short' ), 'no' ) ) ) { + if ( $this->is_on( get_post_meta( $post_id, $this->get_name( 'is_short' ), 'yes') )) { + if ( !$this->is_on( get_option( $this->get_name( 'short_message' ), 'no' ) ) ) { return apply_filters( 'iworks_omnibus_show', false ); } } @@ -350,7 +345,7 @@ private function should_it_show_up( $post_id ) { /** * front-end on sale */ - if ( 'yes' === get_option( $this->get_name( 'on_sale' ), 'yes' ) ) { + if ( $this->is_on( get_option( $this->get_name( 'on_sale' ), 'yes' ) )) { $product = wc_get_product( $post_id ); if ( ! is_object( $product ) @@ -379,10 +374,11 @@ private function should_it_show_up( $post_id ) { */ $product = wc_get_product( $post_id ); switch ( $product->get_type() ) { + case 'grouped'; case 'variable'; return apply_filters( 'iworks_omnibus_show', false ); case 'variation': - if ( 'no' === get_option( $this->get_name( 'variation' ), 'yes' ) ) { + if ( !$this->is_on( get_option( $this->get_name( 'variation' ), 'yes' ) ) ) { return apply_filters( 'iworks_omnibus_show', false ); } return apply_filters( 'iworks_omnibus_show', true ); @@ -415,7 +411,7 @@ private function should_it_show_up( $post_id ) { * any loop */ if ( in_the_loop() ) { - if ( 'yes' === get_option( $this->get_name( 'loop' ), 'no' ) ) { + if ( $this->is_on( get_option( $this->get_name( 'loop' ), 'no' ) ) ) { return apply_filters( 'iworks_omnibus_show', true ); } return apply_filters( 'iworks_omnibus_show', false ); @@ -423,7 +419,7 @@ private function should_it_show_up( $post_id ) { /** * at least add filter */ - $show = 'yes' === get_option( $this->get_name( 'default' ), 'no' ); + $show = $this->is_on( get_option( $this->get_name( 'default' ), 'no' ) ); return apply_filters( 'iworks_omnibus_show', $show ); } @@ -716,12 +712,12 @@ public function filter_add_link_omnibus_configuration( $actions, $plugin_file, $ * @since 2.1.5 */ public function filter_woocommerce_cart_item_price( $price_html, $cart_item, $cart_item_key ) { - if ( 'yes' === get_option( $this->get_name( 'on_sale' ), 'yes' ) ) { + if ( $this->is_on( get_option( $this->get_name( 'on_sale' ), 'yes' ) )) { if ( ! $cart_item['data']->is_on_sale() ) { return $price_html; } } - return $this->add_message_helper( $price_html, $product ); + return $this->add_message_helper( $price_html, wc_get_product($cart_item['product_id'] ) ); } /** @@ -755,76 +751,22 @@ public function filter_woocommerce_get_settings_pages( $settings ) { * add data on admin list * */ - public function filter_iworks_omnibus_message_template_for_admin_list( $template ) { + public function filter_iworks_omnibus_message_template_for_admin_list( $message, $price_html, $price_regular, $price_sale, $price_lowest, $format_price_callback) { if ( ! is_admin() ) { - return $template; - } - $screen = get_current_screen(); - if ( empty( $screen ) ) { - return $template; - } - if ( ! is_a( $screen, 'WP_Screen' ) ) { - return $template; - } - if ( 'product' !== $screen->post_type ) { - return $template; + return $message; } if ( 'no' === get_option( $this->get_name( 'admin_list_short' ), 'no' ) ) { - return $template; + } else { + $message = __( 'OD: {price}', 'omnibus' ); } - return __( 'OD: {price}', 'omnibus' ); + $message = preg_replace( '/{price}/', wc_price( $price_sale ), $message ); + return $this->message_wrapper( $message ); } /** - * change & replace message + * is short-term product * - * @since 2.3.0 */ - public function filter_iworks_omnibus_message_template_for_product( $template, $price, $price_lowest ) { - if ( is_admin() ) { - return $template; - } - /** - * short term product - */ - $post_id = get_the_ID(); - if ( isset( $price_lowest['variation_id'] ) ) { - $post_id = $price_lowest['variation_id']; - } - if ( 'yes' === get_post_meta( $post_id, $this->get_name( 'is_short' ), true ) ) { - switch ( get_option( $this->get_name( 'short_message' ), 'no' ) ) { - case 'no': - return ''; - case 'inform': - if ( 'yes' == get_option( $this->get_name( 'message_settings' ), 'no' ) ) { - $v = get_option( $this->get_name( 'message_short' ), false ); - if ( ! empty( $v ) ) { - return $v; - } - } - return __( 'This is short term product.', 'omnibus' ); - } - } - /** - * no price - */ - if ( - empty( $price_lowest ) - || ( - isset( $price_lowest['type'] ) - && 'autosaved' === $price_lowest['type'] - ) - ) { - switch ( get_option( $this->get_name( 'missing' ), 'current' ) ) { - case 'no': - return ''; - case 'inform': - return $this->get_message_price_is_not_available(); - } - } - return $template; - } - public function woocommerce_wp_checkbox_short( $post_id, $configuration = array(), $loop = null ) { if ( 'not applicable' === get_option( $this->get_name( 'is_short' ), 'no' ) ) { return; @@ -859,7 +801,7 @@ public function woocommerce_wp_checkbox_short( $post_id, $configuration = array( */ private function update_post_meta_short( $post_id, $meta_value ) { $meta_key = $this->get_name( 'is_short' ); - if ( 'yes' === $meta_value ) { + if ( $this->is_on( $meta_value )) { if ( ! update_post_meta( $post_id, $meta_key, 'yes' ) ) { add_post_meta( $post_id, $meta_key, 'yes', true ); } @@ -935,7 +877,7 @@ public function action_iworks_omnibus_wc_save_price_history( $post_id, $price ) * @since 4.0.0 */ public function register_deactivation_hook() { - if ( 'yes' === get_option( $this->get_name( 'delete' ) ) ) { + if ( $this->is_on( get_option( $this->get_name( 'delete' ), 'no' ) ) ) { $this->delete_all(); } } diff --git a/includes/iworks/omnibus/integration/v4/class-iworks-omnibus-integration.php b/includes/iworks/omnibus/integration/v4/class-iworks-omnibus-integration.php index 6821a83..749b267 100644 --- a/includes/iworks/omnibus/integration/v4/class-iworks-omnibus-integration.php +++ b/includes/iworks/omnibus/integration/v4/class-iworks-omnibus-integration.php @@ -79,6 +79,11 @@ private function add_log( $data ) { ); } + /** + * helper to delete futore log when sale has be chenged + * + * @since 4.0.0 + */ private function delete_future_logs( $post_id ) { global $wpdb; $wpdb->query( @@ -215,22 +220,6 @@ protected function is_on( $value ) { return false; } - /** - * message: price is not available - * - * @since 2.0.2 - */ - protected function get_message_price_is_not_available() { - if ( 'yes' == get_option( $this->get_name( 'message_settings' ), 'no' ) ) { - $v = get_option( $this->get_name( 'message_no_data' ), false ); - if ( ! empty( $v ) ) { - return $v; - } - } - return __( 'The previous price is not available.', 'omnibus' ); - } - - public function filter_get_log_array( $log, $id ) { return apply_filters( 'iworks/omnibus/logger/v4/get/log/array', array(), $id ); } @@ -281,8 +270,6 @@ protected function add_message( $price_html, $price_regular, $price_sale, $price $format_price_callback, $this->get_message_text( $missing_price_messsage_status, $price_regular, $price_sale, $format_price_callback ) ); - default: - l( get_option( $this->get_name( 'missing' ) ) ); } return $price_html; } @@ -308,15 +295,9 @@ private function add_message_runner( $price_html, $price_regular, $price_sale, $ if ( empty( $message ) ) { /* translators: do not translate placeholders in braces */ $message = __( '{price} Lowest price from {days} days before the discount.', 'omnibus' ); - if ( - 'custom' === get_option( $this->get_name( 'message_settings' ), 'no' ) - || 'yes' === get_option( $this->get_name( 'message_settings' ), 'no' ) - ) { - $message = get_option( - $this->get_name( 'message' ), - /* translators: %2$s: rich html price */ - __( 'Previous lowest price was %2$s.', 'omnibus' ) - ); + $message_setting = get_option( $this->get_name( 'message_settings' ), 'no' ); + if ( 'custom' === $message_setting || $this->is_on( $message_setting )) { + $message = get_option( $this->get_name( 'message' ), $message); } } /** @@ -324,7 +305,7 @@ private function add_message_runner( $price_html, $price_regular, $price_sale, $ * * @since 2.3.0 */ - $message = apply_filters( 'iworks_omnibus_message_template', $message, $price_html, $price_sale, $price_lowest ); + $message = apply_filters( 'iworks_omnibus_message_template', $message, $price_html, $price_regular, $price_sale, $price_lowest, $format_price_callback ); if ( empty( $message ) ) { return $price_html; } @@ -403,14 +384,14 @@ protected function get_message_text( $type, $price_regular = null, $price_sale = case 'no_data': case 'inform': $message = esc_html__( 'There is no recorded price from {days} days before the discount.', 'omnibus' ); - if ( 'yes' === get_option( $this->get_name( 'message_settings' ) ) ) { + if ( $this->is_on( get_option( $this->get_name( 'message_settings' ) ) ) ) { $message = get_option( $this->get_name( 'message_no_data' ) ); } break; case 'current': case 'regular': $message = __( '{price} Lowest price from {days} days before the discount.', 'omnibus' ); - if ( 'yes' === get_option( $this->get_name( 'message_settings' ) ) ) { + if ( $this->is_on( get_option( $this->get_name( 'message_settings' ) ) ) ) { $message = get_option( $this->get_name( 'message' ) ); } $price_to_show = 'regular' === $type ? $price_regular : $price_sale; @@ -432,7 +413,7 @@ protected function get_message_text( $type, $price_regular = null, $price_sale = * * @since 4.0.0 */ - private function message_wrapper( $text ) { + protected function message_wrapper( $text ) { return apply_filters( 'iworks/omnibus/message/wrapper', sprintf(