Skip to content

Commit

Permalink
= 4.1.6.5 =
Browse files Browse the repository at this point in the history
~ Fixed: error page checkout with 'WP theme 2022'
~ Check cart null
  • Loading branch information
tungnxt89 committed May 13, 2022
1 parent f99f4ba commit c1644a7
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 30 deletions.
5 changes: 5 additions & 0 deletions block-templates/page-lp_checkout.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!-- wp:template-part {"slug":"header"} /-->
<!-- wp:group {"layout":{"inherit":true}} -->
<div class="wp-block-group"><!-- wp:learnpress/template {"template":"pages/checkout"} /--></div>
<!-- /wp:group -->
<!-- wp:template-part {"slug":"footer"} /-->
87 changes: 73 additions & 14 deletions inc/block-template/class-block-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,29 @@ class LP_Block_Template_Controller {

public function __construct() {
add_action( 'template_redirect', array( $this, 'render_block_template' ) );
// Detected file block template html.
add_filter( 'get_block_templates', array( $this, 'add_block_templates' ), 10, 3 );
add_action( 'enqueue_block_editor_assets', array( $this, 'block_editor' ) );
add_action( 'init', array( $this, 'register_block_template_post_type' ) );
add_filter( 'pre_get_block_file_template', array( $this, 'maybe_return_blocks_template' ), 10, 3 );
add_filter( 'page_template_hierarchy', array( $this, 'page_template_hierarchy' ), 10, 3 );
}

/**
* Fixed error on the function 'resolve_block_template' when call usort()
*
* apply_filters( "{$type}_template_hierarchy", $templates )
*
* @param array $templates
*
* @return array|string[]
*/
public function page_template_hierarchy( array $templates = array() ): array {
if ( LP_PAGE_CHECKOUT === LP_Page_Controller::page_current() ) {
$templates = array( 'page-lp_checkout' );
}

return $templates;
}

public function block_editor() {
Expand Down Expand Up @@ -53,10 +72,7 @@ public function maybe_return_blocks_template( $template, $id, $template_type ) {
// 'get_block_template' was introduced in WP 5.9. We need to support
// 'gutenberg_get_block_template' for previous versions of WP with
// Gutenberg enabled.
if (
! function_exists( 'gutenberg_get_block_template' ) &&
! function_exists( 'get_block_template' )
) {
if ( ! function_exists( 'gutenberg_get_block_template' ) && ! function_exists( 'get_block_template' ) ) {
return $template;
}
$template_name_parts = explode( '//', $id );
Expand Down Expand Up @@ -129,7 +145,7 @@ public function get_single_block_template( $template, $id, $template_type ) {
* @return false|string
*/
public function render_content_block_template( $attributes ) {
$templates = array( 'archive-course', 'single-course', 'content-single-item' );
$templates = array( 'archive-course', 'single-course', 'content-single-item', 'pages/checkout' );
$current_page = LP_Page_Controller::page_current();

if ( in_array( $attributes['template'], $templates, true ) ) {
Expand Down Expand Up @@ -158,16 +174,29 @@ public function render_block_template() {
add_filter( 'learnpress_has_block_template', '__return_true', 10, 0 );
} elseif ( ( is_post_type_archive( LP_COURSE_CPT ) || ( ! empty( learn_press_get_page_id( 'courses' ) ) && is_page( learn_press_get_page_id( 'courses' ) ) ) ) && ! LP_Block_Template_Utils::instance()->theme_has_template( 'archive-lp_course' ) && $this->block_template_is_available( 'archive-lp_course' ) ) {
add_filter( 'learnpress_has_block_template', '__return_true', 10, 0 );
} elseif ( LP_PAGE_CHECKOUT === LP_Page_Controller::page_current() ) {
add_filter( 'learnpress_has_block_template', '__return_true', 10, 0 );
}
}

/**
* Detected if a block template is available
*
* @param $query_result
* @param $query
* @param $template_type
*
* @return array|mixed
*/
public function add_block_templates( $query_result, $query, $template_type ) {
if ( ! LP_Block_Template_Utils::instance()->supports_block_templates() ) {
return $query_result;
}

$post_type = isset( $query['post_type'] ) ? $query['post_type'] : '';
$slugs = isset( $query['slug__in'] ) ? $query['slug__in'] : array();
$post_type = $query['post_type'] ?? '';
$slugs = $query['slug__in'] ?? array();

// Get file block template html.
$template_files = $this->get_block_templates( $slugs, $template_type );

// @todo: Add apply_filters to _gutenberg_get_template_files() in Gutenberg to prevent duplication of logic.
Expand Down Expand Up @@ -201,8 +230,7 @@ function( $query_result_template ) use ( $template_file ) {
if ( 'custom' !== $template_file->source ) {
$template = LP_Block_Template_Utils::instance()->gutenberg_build_template_result_from_file( $template_file, $template_type );
} else {
$template_file->title = ! LP_Block_Template_Utils::instance()->convert_slug_to_title( $template_file->slug );
$query_result[] = $template_file;
$query_result[] = $template_file;
continue;
}

Expand All @@ -225,20 +253,43 @@ function( $query_result_template ) use ( $template_file ) {
return $query_result;
}

public function get_block_templates( $slugs = array(), $template_type = 'wp_template' ) {
/**
* Get content block
*
* @param array $slugs
* @param string $template_type
*
* @return mixed
*/
public function get_block_templates( array $slugs = array(), string $template_type = 'wp_template' ) {
static $templates = null;

if ( ! is_null( $templates ) ) {
return $templates;
}

// Fixed page Checkout
if ( LP_PAGE_CHECKOUT === LP_Page_Controller::page_current() ) {
$slugs[] = 'page-lp_checkout';
}

// Get content template form DB on table posts, with post_type = $template_type.
$templates_from_db = $this->get_block_templates_from_db( $slugs, $template_type );

// Get content from file default.
$templates_from_lp = $this->get_block_templates_from_learnpress( $slugs, $templates_from_db, $template_type );
$templates = array_merge( $templates_from_db, $templates_from_lp );

return $templates;
}

/**
* @param array $slugs
* @param $already_found_templates
* @param string $template_type
*
* @return array
*/
public function get_block_templates_from_learnpress( $slugs, $already_found_templates, $template_type = 'wp_template' ) {
global $wp;

Expand All @@ -261,15 +312,20 @@ public function get_block_templates_from_learnpress( $slugs, $already_found_temp

$template_slug = $template_slug === 'content-single-lp_course-item' ? 'single-lp_course' : $template_slug;

// This template does not have a slug we're looking for. Skip it.
if ( is_array( $slugs ) && count( $slugs ) > 0 && ! in_array( $template_slug, $slugs, true ) ) {
if ( LP_PAGE_CHECKOUT === LP_Page_Controller::page_current() ) {
if ( str_contains( $template_file, 'page-lp_checkout' ) ) {
$template_slug = $slugs[0];
$templates[] = LP_Block_Template_Utils::instance()->create_new_block_template_object( $template_file, $template_type, $template_slug );
continue;
}
} elseif ( is_array( $slugs ) && count( $slugs ) > 0 && ! in_array( $template_slug, $slugs, true ) ) {
// This template does not have a slug we're looking for. Skip it.
continue;
}

// If the theme already has a template, or the template is already in the list (i.e. it came from the
// database) then we should not overwrite it with the one from the filesystem.
if (
LP_Block_Template_Utils::instance()->theme_has_template( $template_slug ) ||
if ( LP_Block_Template_Utils::instance()->theme_has_template( $template_slug ) ||
count(
array_filter(
$already_found_templates,
Expand Down Expand Up @@ -298,6 +354,9 @@ function ( $template ) use ( $template_slug ) {

// At this point the template only exists in the Blocks filesystem and has not been saved in the DB,
// or superseded by the theme.
/*if ( ! isset( $templates[ $template_slug ] ) ) {
$templates[ $template_slug ] = LP_Block_Template_Utils::instance()->create_new_block_template_object( $template_file, $template_type, $template_slug );
}*/
$templates[] = LP_Block_Template_Utils::instance()->create_new_block_template_object( $template_file, $template_type, $template_slug );
}

Expand Down
8 changes: 4 additions & 4 deletions inc/class-lp-debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ public static function rollbackTransaction() {
/**
* Show value of variable
*
* @param $variable
* @param $file_path
* @param $line
* @param mixed $variable
* @param string $file_path
* @param string $line
*/
public static function var_dump( $variable, $file_path, $line ) {
public static function var_dump( $variable, string $file_path = '', string $line = '' ) {
echo '<pre>' . print_r( $variable, true ) . '</pre>';
echo 'FILE:' . esc_html( $file_path ) . '<br> LINE:' . esc_html( $line );
}
Expand Down
2 changes: 0 additions & 2 deletions inc/class-lp-install.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ public function on_activate() {
update_option( LP_KEY_DB_VERSION, LearnPress::instance()->db_version );
}

update_option( 'learnpress_version', LearnPress::instance()->version );

// Create pages default.
self::create_pages();

Expand Down
2 changes: 1 addition & 1 deletion inc/class-lp-page-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ private function get_page_template() {
}
} elseif ( ( is_post_type_archive( LP_COURSE_CPT ) || ( ! empty( learn_press_get_page_id( 'courses' ) ) && is_page( learn_press_get_page_id( 'courses' ) ) ) ) && ! self::has_block_template( 'archive-course' ) ) {
$page_template = 'archive-course.php';
} elseif ( learn_press_is_checkout() ) {
} elseif ( learn_press_is_checkout() && ! self::has_block_template( 'page-lp_checkout' ) ) {
$page_template = 'pages/checkout.php';
}

Expand Down
5 changes: 3 additions & 2 deletions inc/curds/class-lp-course-curd.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,12 @@ public function load( &$course ) {
* @return array
*/
public function read_course_items( $course_id, $publish_only = true, $section_ids = array() ) {
static $results = null;
// Tungnx: Comment because make not work with WPML.
/*static $results = null;
if ( ! is_null( $results ) ) {
return $results;
}
}*/

global $wpdb;
$where = '';
Expand Down
3 changes: 3 additions & 0 deletions inc/lp-template-hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,9 @@
add_action( 'learn-press/user-profile', LP()->template( 'profile' )->func( 'register_form' ), 15 );

/** BEGIN: Checkout page */
/**
* @see LP_Template_Checkout::review_order()
*/
add_action( 'learn-press/before-checkout-form', LP()->template( 'checkout' )->func( 'review_order' ), 10 );
add_action( 'learn-press/after-checkout-form', LP()->template( 'checkout' )->func( 'account_logged_in' ), 20 );
add_action( 'learn-press/after-checkout-form', LP()->template( 'checkout' )->func( 'account_register' ), 30 );
Expand Down
2 changes: 1 addition & 1 deletion inc/shortcodes/class-lp-shortcode-checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function output() {
$checkout_cart = learn_press_get_checkout_cart();

// Check cart has contents
if ( $checkout_cart->is_empty() ) {
if ( $checkout_cart && $checkout_cart->is_empty() ) {
learn_press_get_template( 'checkout/empty-cart.php' );
} else {
learn_press_get_template( 'checkout/form.php' );
Expand Down
3 changes: 2 additions & 1 deletion inc/templates/class-lp-template-checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public function __construct() {
}

public function review_order() {
learn_press_get_template( 'checkout/review-order' );
$cart = learn_press_get_checkout_cart();
learn_press_get_template( 'checkout/review-order', compact( 'cart' ) );
}

public function payment() {
Expand Down
2 changes: 2 additions & 0 deletions learnpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ protected function __construct() {
}
self::$_instance = $this;

update_option( 'learnpress_version', $this->version );

// Define constant .
$this->plugin_defines();

Expand Down
2 changes: 1 addition & 1 deletion templates/checkout/payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
do_action( 'learn-press/before-payment-methods' );

// Show payments if cart total > 0 and have at least one payment method.
if ( LP()->cart->needs_payment() && $available_gateways ) {
if ( LP()->cart && LP()->cart->needs_payment() && $available_gateways ) {
?>
<h4>
<?php esc_html_e( 'Payment', 'learnpress' ); ?>
Expand Down
8 changes: 6 additions & 2 deletions templates/checkout/review-order.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
*
* @author ThimPress
* @package Learnpress/Templates
* @version 4.0.0
* @version 4.0.1
*/

defined( 'ABSPATH' ) || exit();

$cart = learn_press_get_checkout_cart();
// $cart = learn_press_get_checkout_cart();

if ( ! isset( $cart ) ) {
return;
}
?>

<div id="checkout-order" class="lp-checkout-block right">
Expand Down
8 changes: 6 additions & 2 deletions templates/pages/checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

defined( 'ABSPATH' ) or die;

get_header();
if ( empty( $is_block_theme ) ) {
get_header();
}

do_action( 'learn-press/before-main-content' );
do_action( 'learnpress/template/pages/checkout/before-content' );
Expand Down Expand Up @@ -44,4 +46,6 @@
do_action( 'learnpress/template/pages/checkout/after-content' );
do_action( 'learn-press/after-main-content' );

get_footer();
if ( empty( $is_block_theme ) ) {
get_footer();
}

0 comments on commit c1644a7

Please sign in to comment.