Skip to content

Commit

Permalink
simplifying the code
Browse files Browse the repository at this point in the history
  • Loading branch information
iworks committed Dec 13, 2024
1 parent 22d18f6 commit bc49ce5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) ) ) )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
*
Expand Down Expand Up @@ -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 );
}
/**
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -317,20 +312,20 @@ 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 );
}
}
}
} 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 );
}
}
Expand All @@ -340,17 +335,17 @@ 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 );
}
}
}
/**
* 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 )
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -415,15 +411,15 @@ 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 );
}
/**
* 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 );
}

Expand Down Expand Up @@ -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'] ) );
}

/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
}
Expand Down Expand Up @@ -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();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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 );
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -308,23 +295,17 @@ 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);
}
}
/**
* mesage template filter
*
* @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;
}
Expand Down Expand Up @@ -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;
Expand All @@ -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(
Expand Down

0 comments on commit bc49ce5

Please sign in to comment.