diff --git a/includes/iworks/class-iworks-omnibus.php b/includes/iworks/class-iworks-omnibus.php index a35d89f..32c2965 100644 --- a/includes/iworks/class-iworks-omnibus.php +++ b/includes/iworks/class-iworks-omnibus.php @@ -107,7 +107,8 @@ public function __construct() { } public function action_plugins_loaded() { - $dir = dirname( __FILE__ ) . '/omnibus'; + $dir = dirname( __FILE__ ) . '/omnibus'; + $v4_directory = $this->is_migrated_v4() ? '/v4' : ''; /** * WooCommerce * @@ -117,7 +118,6 @@ public function action_plugins_loaded() { defined( 'WC_PLUGIN_FILE' ) && defined( 'WC_VERSION' ) ) { - $v4_directory = $this->is_migrated_v4() ? '/v4' : ''; /** * Check minimal WooCommerce version to run. * @@ -152,7 +152,7 @@ public function action_plugins_loaded() { * @since 1.0.1 */ if ( defined( 'LP_PLUGIN_FILE' ) ) { - include_once $dir . '/integration/class-iworks-omnibus-integration-learnpress.php'; + include_once $dir . '/integration' . $v4_directory . '/class-iworks-omnibus-integration-learnpress.php'; $this->objects['learnpress'] = new iworks_omnibus_integration_learnpress(); } /** 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 da4da0e..8848034 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 @@ -36,6 +36,20 @@ public function __construct() { add_action( 'shutdown', array( $this, 'action_shutdown_maybe_delete_older_records' ) ); } + /** + * helper to decide show it or no + */ + protected function should_it_show_up( $post_id ) { + } + + /** + * helper to add logs + * + * @since 4.0.0 + */ + protected function maybe_add_price_log( $element ) { + } + /** * Delete older records * diff --git a/includes/iworks/omnibus/integration/v4/class-iworks-omnibus-integration-debug-bar.php b/includes/iworks/omnibus/integration/v4/class-iworks-omnibus-integration-debug-bar.php index f3de4bd..62c71a3 100644 --- a/includes/iworks/omnibus/integration/v4/class-iworks-omnibus-integration-debug-bar.php +++ b/includes/iworks/omnibus/integration/v4/class-iworks-omnibus-integration-debug-bar.php @@ -31,6 +31,20 @@ public function __construct() { add_filter( 'debug_bar_panels', array( $this, 'filter_debug_bar_panels' ) ); } + /** + * helper to decide show it or no + */ + protected function should_it_show_up( $post_id ) { + } + + /** + * helper to add logs + * + * @since 4.0.0 + */ + protected function maybe_add_price_log( $element ) { + } + public function filter_debug_bar_panels( $panels ) { if ( is_singular( 'product' ) ) { include_once __DIR__ . '/class-iworks-omnibus-integration-debug-bar-panel.php'; diff --git a/includes/iworks/omnibus/integration/v4/class-iworks-omnibus-integration-learnpress-settings.php b/includes/iworks/omnibus/integration/v4/class-iworks-omnibus-integration-learnpress-settings.php new file mode 100644 index 0000000..c5799f1 --- /dev/null +++ b/includes/iworks/omnibus/integration/v4/class-iworks-omnibus-integration-learnpress-settings.php @@ -0,0 +1,38 @@ +id = 'omnibus'; + $this->text = esc_html__( 'Omnibus', 'omnibus' ); + + parent::__construct(); + } + + /** + * Return fields for settings page. + * + * @param string $section + * @param string $tab + * + * @return mixed + */ + public function get_settings( $section = '', $tab = '' ) { + return apply_filters( 'iworks/omnibus/learn-press/settings', array() ); + } + +} + +return new iworks_omnibus_integration_learnpress_settings(); diff --git a/includes/iworks/omnibus/integration/v4/class-iworks-omnibus-integration-learnpress.php b/includes/iworks/omnibus/integration/v4/class-iworks-omnibus-integration-learnpress.php new file mode 100644 index 0000000..d4d9478 --- /dev/null +++ b/includes/iworks/omnibus/integration/v4/class-iworks-omnibus-integration-learnpress.php @@ -0,0 +1,347 @@ + 'learn-press-settings', + 'tab' => 'omnibus', + ), + admin_url( 'admin.php' ) + ); + $actions['omnibus'] = sprintf( + '%s', + esc_url( $settings_page_url ), + __( 'Omnibus', 'omnibus' ) + ); + return $actions; + } + + /** + * LearnPress: add confirmation fields + * + * @since 2.1.0 + */ + public function filter_learnpress_courses_settings_fields( $settings ) { + return array_merge( + $settings, + $this->get_settings() + ); + } + + private function get_settings() { + return array( + $this->settings_title(), + array( + 'title' => __( 'Only on sale', 'omnibus' ), + 'id' => $this->get_name( 'on_sale', '' ), + 'default' => 'yes', + 'type' => 'checkbox', + 'desc' => __( 'Display only for the course on sale.', 'omnibus' ), + ), + /** + * Show on + */ + array( + 'title' => __( 'Show on', 'omnibus' ), + 'desc' => __( 'Course single', 'omnibus' ), + 'id' => $this->get_name( 'product', '' ), + 'default' => 'yes', + 'type' => 'checkbox', + 'checkboxgroup' => 'start', + 'desc_tip' => __( 'Show or hide on a single course page.', 'omnibus' ), + ), + array( + 'id' => $this->get_name( 'default', '' ), + 'default' => 'no', + 'type' => 'checkbox', + 'desc' => __( 'Display anywhere else', 'omnibus' ), + 'desc_tip' => __( 'Display anywhere else that doesn\'t fit any of the above.', 'omnibus' ), + 'checkboxgroup' => 'end', + ), + /** + * admin + */ + array( + 'title' => __( 'Show on admin on', 'omnibus' ), + 'desc' => __( 'Courses list', 'omnibus' ), + 'id' => $this->get_name( 'admin_list', '' ), + 'default' => 'yes', + 'type' => 'checkbox', + 'checkboxgroup' => 'start', + ), + array( + 'desc' => __( 'Course edit', 'omnibus' ), + 'id' => $this->get_name( 'admin_edit', '' ), + 'default' => 'yes', + 'type' => 'checkbox', + 'checkboxgroup' => 'end', + ), + $this->settings_days(), + /** + * messages + */ + $this->settings_message_settings(), + $this->settings_message(), + array( + 'type' => 'sectionend', + ), + ); + } + + /** + * Save LearnPress Course + * + * @since 4.0.0 + */ + public function action_updated_postmeta_maybe_add_price_log( $meta_id, $object_id, $meta_key, $_meta_value ) { + if ( '_edit_lock' !== $meta_key ) { + return; + } + if ( 'lp_course' !== get_post_type( $object_id ) ) { + return; + } + $this->maybe_add_price_log( $object_id ); + } + + /** + * helper to add logs + * + * @since 4.0.0 + */ + protected function maybe_add_price_log( $id ) { + if ( 'publish' !== get_post_status( $id ) ) { + return; + } + /** + * price regular + */ + $regular_price = get_post_meta( $id, '_lp_price', true ); // For LP version < 1.4.1.2 + if ( metadata_exists( 'post', $id, '_lp_regular_price' ) ) { + $regular_price = get_post_meta( $id, '_lp_regular_price', true ); + } + /** + * price sale from + */ + $price_sale_from = get_post_meta( $id, '_lp_sale_start', true ); + if ( empty( $price_sale_from ) ) { + $price_sale_from = 'now'; + } else { + $price_sale_from = date( $this->mysql_data_format, strtotime( $price_sale_from ) ); + } + $data = array( + 'post_id' => $id, + 'product_origin' => 'learnpress', + 'product_type' => get_post_type( $id ), + 'price_regular' => $regular_price, + 'price_sale' => get_post_meta( $id, '_lp_sale_price', true ), + 'price_sale_from' => $price_sale_from, + 'currency' => learn_press_get_currency(), + 'user_id' => get_current_user_id(), + ); + $this->maybe_add_last_saved_prices( $data ); + } + + /** + * LearnPress: show prices in admin + * + * @since 1.0.1 + */ + public function filter_learnpress_admin_show_omnibus( $configuration ) { + $post_id = get_the_ID(); + if ( ! $this->should_it_show_up( $post_id ) ) { + return $configuration; + } + if ( 'no' === get_option( $this->get_name( 'admin_edit' ) ) ) { + return $configuration; + } + global $post_id; + $course = learn_press_get_course( $post_id ); + $price_lowest = $this->get_lowest_price_by_post_id( $course->get_id(), $course->get_sale_price() ); + $configuration[ $this->meta_name . 'price' ] = new LP_Meta_Box_Text_Field( + esc_html__( 'Omnibus Price', 'omnibus' ), + sprintf( + /* translators: %d: nuber of days */ + esc_html__( 'The lowest price in %d days.', 'omnibus' ), + $this->get_days() + ), + is_array( $price_lowest ) && isset( $price_lowest['price'] ) ? $price_lowest['price'] : esc_html__( 'no data available', 'omnibus' ), + array( + 'type_input' => 'text', + 'custom_attributes' => array( + 'readonly' => 'readonly', + ), + ) + ); + $configuration[ $this->meta_name . 'date' ] = new LP_Meta_Box_Text_Field( + esc_html__( 'Omnibus Date', 'omnibus' ), + sprintf( + /* translators: %d: nuber of days */ + esc_html__( 'The date when lowest price in %d days occurred.', 'omnibus' ), + $this->get_days() + ), + is_array( $price_lowest ) && isset( $price_lowest['timestamp'] ) ? date_i18n( get_option( 'date_format' ), $price_lowest['timestamp'] ) : esc_html__( 'no data available', 'omnibus' ), + array( + 'type_input' => 'text', + 'custom_attributes' => array( + 'readonly' => 'readonly', + ), + ) + ); + return $configuration; + } + + /** + * LearnPress: add Omnibus price information + * + * @since 1.0.1 + */ + public function filter_learn_press_course_price_html( $price_html, $has_sale_price, $post_id ) { + if ( ! $this->should_it_show_up( $post_id ) ) { + return $price_html; + } + $course = learn_press_get_course( $post_id ); + return $this->add_message_helper( $price_html, $course ); + } + + /** + * helper function to parent->add_message() + * + * @since 4.0.0 + */ + private function add_message_helper( $price_html, $course, $message = null ) { + $price_lowest = $this->get_lowest_price_by_post_id( $course->get_id(), $course->get_sale_price() ); + return $this->add_message( + $price_html, + $course->get_regular_price(), + $course->get_sale_price(), + $price_lowest, + 'learn_press_format_price', + $message + ); + } + + public function get_name( $name = '', $add_prefix = 'learn_press_' ) { + if ( empty( $name ) ) { + return $add_prefix . parent::get_name( 'lp' ); + } + return $add_prefix . parent::get_name( 'lp_' . $name ); + } + + /** + * helper to decide show it or no + */ + protected function should_it_show_up( $post_id ) { + if ( 'yes' === get_option( $this->get_name( 'on_sale' ), 'yes' ) ) { + $course = learn_press_get_course( $post_id ); + if ( ! $course->has_sale_price() ) { + return apply_filters( 'iworks_omnibus_show', false ); + } + } + /** + * for admin + */ + if ( is_admin() ) { + $screen = get_current_screen(); + if ( 'lp_course' === $screen->id ) { + if ( 'no' === get_option( $this->get_name( 'admin_edit' ), 'yes' ) ) { + return apply_filters( 'iworks_omnibus_show', false ); + } + } + if ( 'edit-lp_course' === $screen->id ) { + if ( 'no' === get_option( $this->get_name( 'admin_list' ), 'yes' ) ) { + return apply_filters( 'iworks_omnibus_show', false ); + } + } + return apply_filters( 'iworks_omnibus_show', true ); + } + /** + * front-end + */ + if ( + is_single() + && is_main_query() + && ( learn_press_is_course() ) + ) { + if ( 'no' === get_option( $this->get_name( 'single' ), 'yes' ) ) { + return apply_filters( 'iworks_omnibus_show', false ); + } + return apply_filters( 'iworks_omnibus_show', true ); + } + /** + * at least add filter + */ + $show = 'yes' === get_option( $this->get_name( 'default' ), 'no' ); + return apply_filters( 'iworks_omnibus_show', $show ); + } + +} + 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 c6c344a..fb7b7ff 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 @@ -188,8 +188,6 @@ public function action_save_product( $product ) { $this->maybe_add_price_log( $variant ); } break; - default: - l( $product->get_type() ); } } @@ -198,9 +196,8 @@ public function action_save_product( $product ) { * * @since 4.0.0 * - * */ - public function maybe_add_price_log( $product ) { + protected function maybe_add_price_log( $product ) { if ( 'publish' !== get_post_status( $product ) ) { return; } @@ -303,7 +300,7 @@ public function action_woocommerce_variation_options_pricing( $loop, $variation_ /** * helper to decide show it or no */ - private function should_it_show_up( $post_id ) { + protected function should_it_show_up( $post_id ) { /** * for admin */ @@ -320,12 +317,12 @@ private function should_it_show_up( $post_id ) { } elseif ( function_exists( 'get_current_screen' ) ) { $screen = get_current_screen(); if ( 'product' === $screen->id ) { - if ( $this->is_on( 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 ($this->is_on( 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 ); } } @@ -336,8 +333,8 @@ private function should_it_show_up( $post_id ) { * front-end short term good */ 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' ) ) ) { + 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 ); } } @@ -345,7 +342,7 @@ private function should_it_show_up( $post_id ) { /** * front-end on sale */ - if ( $this->is_on( 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 ) @@ -375,13 +372,19 @@ 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 'variable': + if ( 2 > count( $product->get_available_variations() ) ) { + if ( $this->is_on( get_option( $this->get_name( 'variation' ), 'yes' ) ) ) { + return apply_filters( 'iworks_omnibus_show', true ); + } + } return apply_filters( 'iworks_omnibus_show', false ); case 'variation': - if ( !$this->is_on( get_option( $this->get_name( 'variation' ), 'yes' ) ) ) { - return apply_filters( 'iworks_omnibus_show', false ); + if ( $this->is_on( get_option( $this->get_name( 'variation' ), 'yes' ) ) ) { + return apply_filters( 'iworks_omnibus_show', true ); } - return apply_filters( 'iworks_omnibus_show', true ); + return apply_filters( 'iworks_omnibus_show', false ); } } if ( 'yes' === get_option( $this->get_name( 'single' ), 'yes' ) ) { @@ -712,12 +715,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 ( $this->is_on( 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, wc_get_product($cart_item['product_id'] ) ); + return $this->add_message_helper( $price_html, wc_get_product( $cart_item['product_id'] ) ); } /** @@ -751,7 +754,7 @@ public function filter_woocommerce_get_settings_pages( $settings ) { * add data on admin list * */ - public function filter_iworks_omnibus_message_template_for_admin_list( $message, $price_html, $price_regular, $price_sale, $price_lowest, $format_price_callback) { + 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 $message; } @@ -801,7 +804,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 ( $this->is_on( $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 ); } 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 749b267..4e55145 100644 --- a/includes/iworks/omnibus/integration/v4/class-iworks-omnibus-integration.php +++ b/includes/iworks/omnibus/integration/v4/class-iworks-omnibus-integration.php @@ -41,17 +41,35 @@ abstract class iworks_omnibus_integration { protected string $mysql_data_format = 'Y-m-d H:i:s'; + /** + * helper to decide show it or no + * + * @since 4.0.0 + */ + abstract protected function should_it_show_up( $post_id ); + + /** + * helper to add logs + * + * @since 4.0.0 + */ + abstract protected function maybe_add_price_log( $element ); + public function get_name( $name = '' ) { if ( empty( $name ) ) { return $this->meta_name; } - return sanitize_title( + $name = sanitize_title( sprintf( '%s_%s', $this->meta_name, $name ) ); + return apply_filters( + 'iworks/omnibus/option/name/' . $name, + $name + ); } /** @@ -294,10 +312,10 @@ 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' ); + $message = __( '{price} Lowest price from {days} days before the discount.', '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); + if ( 'custom' === $message_setting || $this->is_on( $message_setting ) ) { + $message = get_option( $this->get_name( 'message' ), $message ); } } /** @@ -322,33 +340,11 @@ private function add_message_runner( $price_html, $price_regular, $price_sale, $ if ( is_callable( $format_price_callback ) ) { $price_to_show = $format_price_callback( $price_to_show ); } - /** - * add attributes - */ - $attributes = array( - 'data-iwo-version' => $this->version, - ); - foreach ( $price_lowest as $key => $value ) { - $attributes[ sprintf( 'data-iwo-%s', $key ) ] = esc_attr( $value ); - } - $attribute_data_string = ''; - foreach ( $attributes as $attribute_name => $attribute_value ) { - $attribute_data_string .= sprintf( - ' %s="%s"', - esc_html( $attribute_name ), - esc_attr( $attribute_value ) - ); - } - $price_html .= apply_filters( - 'iworks_omnibus_message', + $price_html .= $this->message_wrapper( sprintf( - '
%s
', - $attribute_data_string, - sprintf( - $message, - $this->get_days(), - $price_to_show - ) + $message, + $this->get_days(), + $price_to_show ) ); /** @@ -417,10 +413,10 @@ protected function message_wrapper( $text ) { return apply_filters( 'iworks/omnibus/message/wrapper', sprintf( - '<%3$s class="iworks-omnibus" data-iwo-version="%2$s">%1$s%3$s>', + '