Skip to content

Commit

Permalink
= 4.2.0 =
Browse files Browse the repository at this point in the history
~ Modified: load template addon.
~ Modified: learn_press_template_path()
  • Loading branch information
tungnxt89 committed Dec 17, 2022
1 parent c0eac02 commit 4006152
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 72 deletions.
46 changes: 28 additions & 18 deletions inc/Helper/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,21 @@ public function get_admin_template( string $file_name = '', array $args = array(
* @param string $file_name
* @param array $args
*
* @return void
* @return void|string
* @since 1.0.0
* @version 1.0.0
*/
public function get_frontend_template( string $file_name = '', array $args = array() ) {
$default_path = LP_PLUGIN_PATH . "templates/{$file_name}";
$folder_name_rewrite = apply_filters( 'learn_press_template_path', LP_PLUGIN_FOLDER_NAME );

$from_theme_path = get_template_directory() . DIRECTORY_SEPARATOR . $folder_name_rewrite . DIRECTORY_SEPARATOR . $file_name;
$path_load = file_exists( $from_theme_path ) ? $from_theme_path : $default_path;

$template = $this->get_template( $path_load, $args );

$folder_name_rewrite = learn_press_template_path();
$from_theme_path = sprintf(
'%s/%s/%s',
get_template_directory(),
$folder_name_rewrite,
$file_name
);
$path_load = file_exists( $from_theme_path ) ? $from_theme_path : $default_path;
$template = $this->get_template( $path_load, $args );
if ( ! $this->include ) {
return $template;
}
Expand All @@ -90,18 +92,26 @@ public function get_frontend_template( string $file_name = '', array $args = arr
* @version 1.0.0
*/
public function get_template( string $path_file, array $args = array() ) {
extract( $args );
if ( file_exists( $path_file ) ) {
if ( $this->include ) {
include $path_file;
try {
extract( $args );

// Check path file not extension, will add extension .php
if ( ! preg_match( '/\.php$/', $path_file ) ) {
$path_file .= '.php';
}

if ( realpath( $path_file ) ) {
if ( $this->include ) {
include $path_file;
} else {
return $path_file;
}
} else {
return $path_file;
printf( esc_html__( 'Path file %s not exists', 'realpress' ), $path_file );
echo '<br>';
}
} else {
printf( esc_html__( 'Path %s not exists.', 'realpress' ), $path_file );
?>
<br>
<?php
} catch ( \Throwable $e ) {
error_log( $e->getMessage() );
}
}
}
Expand Down
104 changes: 61 additions & 43 deletions inc/abstracts/abstract-addon.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use LearnPress\Helpers\Template;

/**
* Class LP_Addon
*/
Expand All @@ -9,61 +11,67 @@ class LP_Addon {
*
* @var string
*/
public $version = null;

public $version = 0;
/**
* Learnpress require addon version
* Case:
* LearnPress require addon version
*
* @var array
*/
private $lp_require_addon_version = '4.0.0';

/**
* Required version for current version of addon.
*
* @var string
*/
public $require_version = null;

public $require_version = 0;
/**
* Path to addon.
* Path to root file of addon.
*
* @var string
*/
public $plugin_file = null;

public $plugin_file = '';
/**
* Base addon.
* @var string folder path root of addon.
*/
public $plugin_folder_path = '';
/**
* Root folder name addon.
*
* @var string
*/
public $plugin_base = null;

public $plugin_folder_name = '';
/**
* Addon textdomain name.
* Plugin base addon.
*
* @var string contain root folder path and root file of addon
*/
public $plugin_base = '';
/**
* Base name addon.
*
* @var string root file name php of addon
*/
public $plugin_base_name = '';
/**
* Addon text-domain name.
*
* @var string
*/
public $text_domain = '';

/**
* @var null
*/
protected $_valid = null;

/**
* Singleton instance of the addon.
*
* @var array
*/
public static $instances = array();

/**
* @var array
*/
protected static $_admin_notices = array();

/**
* @var string
*/
Expand All @@ -75,9 +83,12 @@ class LP_Addon {
* LP_Addon constructor.
*/
public function __construct() {
$this->plugin_base = plugin_basename( $this->plugin_file );
$this->plugin_base_name = basename( $this->plugin_file );
$this->plugin_folder_path = dirname( $this->plugin_file );
$this->plugin_folder_name = str_replace( array( '/', $this->plugin_base_name ), '', $this->plugin_base );
$this->_define_constants();
$this->_includes();

remove_action( 'plugins_loaded', array( 'LP_Addon_Announcements', 'instance' ) );
add_action( 'init', array( $this, 'init' ) );
}
Expand Down Expand Up @@ -117,7 +128,7 @@ public function init() {
$this->load_text_domain();

add_filter(
"plugin_action_links_{$this->get_plugin_slug()}",
"plugin_action_links_$this->plugin_base",
array(
$this,
'_plugin_links',
Expand Down Expand Up @@ -160,16 +171,11 @@ protected function _enqueue_assets() {
* Get plugin slug from plugin file.
*
* @return bool|string
* @deprecated 4.2.0
*/
public function get_plugin_slug() {
if ( empty( $this->plugin_file ) ) {
return false;
}

$dir = dirname( $this->plugin_file );
$basename = basename( $dir );

return $basename . '/' . basename( $this->plugin_file );
_deprecated_function( __METHOD__, '4.2.0' );
return $this->plugin_base;
}

/**
Expand Down Expand Up @@ -315,12 +321,12 @@ public function load_text_domain() {
$domain_files = array();

if ( is_admin() ) {
$domain_files[] = WP_LANG_DIR . "/{$plugin_folder}/{$plugin_folder}-admin-{$locale}.mo";
$domain_files[] = WP_LANG_DIR . "/plugins/{$plugin_folder}-admin-{$locale}.mo";
$domain_files[] = WP_LANG_DIR . "/$plugin_folder/$plugin_folder-admin-$locale.mo";
$domain_files[] = WP_LANG_DIR . "/plugins/$plugin_folder-admin-$locale.mo";
}

$domain_files[] = WP_CONTENT_DIR . "/plugins/{$plugin_folder}/languages/{$plugin_folder}-{$locale}.mo";
$domain_files[] = WP_LANG_DIR . "/{$plugin_folder}/{$plugin_folder}-{$locale}.mo";
$domain_files[] = WP_CONTENT_DIR . "/plugins/$plugin_folder/languages/$plugin_folder-$locale.mo";
$domain_files[] = WP_LANG_DIR . "/$plugin_folder/$plugin_folder-$locale.mo";

foreach ( $domain_files as $file ) {
if ( ! file_exists( $file ) ) {
Expand Down Expand Up @@ -352,7 +358,7 @@ public static function load( string $instance = '', string $path = '', string $p
}

if ( $plugin_folder ) {
$path = "{$plugin_folder}/$path";
$path = "$plugin_folder/$path";
}

if ( ! file_exists( $path ) ) {
Expand Down Expand Up @@ -401,32 +407,42 @@ public function get_plugin_url( $sub = '/' ) {
* Get template path.
*
* @return string
* @deprecated 4.2.0
*/
public function get_template_path() {
_deprecated_function( __FUNCTION__, '4.2.0', 'LP_Addon::get_template' );
if ( empty( $this->_template_path ) ) {
$this->_template_path = learn_press_template_path() . '/addons/' . preg_replace(
'!^learnpress-!',
'',
dirname( $this->get_plugin_slug() )
$this->plugin_folder_name
);
}

return $this->_template_path;
}

/**
* Get content template of addon in theme or inside itself.
* Get content template of addon.
*
* @param string $template_name
* @param array $args
*/
public function get_template( $template_name, $args = array() ) {
learn_press_get_template(
$template_name,
$args,
$this->get_template_path(),
dirname( $this->plugin_file ) . '/templates/'
* @param mixed $args
* @since 3.0.0
* @version 1.0.1
*/
public function get_template( string $template_name = '', $args = [] ) {
$default_path = $this->plugin_folder_path . "/templates/$template_name";
$folder_name_rewrite = learn_press_template_path();
$from_theme_path = sprintf(
'%s/%s/%s/%s/%s',
get_template_directory(),
$folder_name_rewrite,
'addons',
str_replace( 'learnpress-', '', $this->plugin_folder_name ),
$template_name
);
$path_load = file_exists( $from_theme_path ) ? $from_theme_path : $default_path;
Template::instance()->get_template( $path_load, $args );
}

/**
Expand All @@ -435,8 +451,10 @@ public function get_template( $template_name, $args = array() ) {
* @param string $template_name
*
* @return string
* @deprecated 4.2.0
*/
public function locate_template( $template_name ) {
_deprecated_function( __FUNCTION__, '4.2.0', 'LP_Addon::get_template' );
return learn_press_locate_template(
$template_name,
$this->get_template_path(),
Expand Down
6 changes: 3 additions & 3 deletions inc/class-lp-page-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -603,13 +603,13 @@ private function get_page_templates( $default_template ) {
if ( learn_press_is_course_taxonomy() ) {
$object = get_queried_object();
$templates[] = 'taxonomy-' . $object->taxonomy . '-' . $object->slug . '.php';
$templates[] = learn_press_template_path( true ) . 'taxonomy-' . $object->taxonomy . '-' . $object->slug . '.php';
$templates[] = learn_press_template_path() . '/taxonomy-' . $object->taxonomy . '-' . $object->slug . '.php';
$templates[] = 'taxonomy-' . $object->taxonomy . '.php';
$templates[] = learn_press_template_path( true ) . 'taxonomy-' . $object->taxonomy . '.php';
$templates[] = learn_press_template_path() . '/taxonomy-' . $object->taxonomy . '.php';
}

$templates[] = $default_template;
$templates[] = learn_press_template_path( true ) . $default_template;
$templates[] = learn_press_template_path() . $default_template;

return array_unique( $templates );
}
Expand Down
15 changes: 10 additions & 5 deletions inc/lp-template-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -909,14 +909,19 @@ function learn_press_locate_template( $template_name, $template_path = '', $defa
}

/**
* Returns the name of folder contains template files in theme
*
* @param bool
* Returns the name of folder contains override template files in theme
*
* @return string
* @since 3.0.0
* @version 1.0.1
*/
function learn_press_template_path( $slash = false ) {
return apply_filters( 'learn_press_template_path', 'learnpress', $slash ) . ( $slash ? '/' : '' );
function learn_press_template_path(): string {
$lp_folder_name_override = apply_filters( 'learn_press_template_path', LP_PLUGIN_FOLDER_NAME );
if ( ! is_string( $lp_folder_name_override ) ) {
$lp_folder_name_override = LP_PLUGIN_FOLDER_NAME;
}

return $lp_folder_name_override;
}

/**
Expand Down
8 changes: 5 additions & 3 deletions inc/rest-api/v1/frontend/class-lp-rest-courses-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -603,15 +603,17 @@ public function delete_item( $request ) {
}

/**
* Rest API for Continue in single course.
* Rest API for get item continue in single course.
*
* @param WP_REST_Request $request
*
* @author minhpd
* @editor tungnx
* @since 4.1.4
* @version 1.0.2
* @return LP_REST_Response
*/
public function continue_course( WP_REST_Request $request ) {
public function continue_course( WP_REST_Request $request ): LP_REST_Response {
$params = $request->get_params();
$response = new LP_REST_Response();
$response->data = '';
Expand Down Expand Up @@ -666,7 +668,7 @@ public function continue_course( WP_REST_Request $request ) {
$response->message = $e->getMessage();
}

wp_send_json( $response );
return $response;
}

}
1 change: 1 addition & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<exclude-pattern type="relative">vendor</exclude-pattern>
<exclude-pattern type="relative">node_modules</exclude-pattern>
<exclude-pattern type="relative">tests</exclude-pattern>
<exclude-pattern type="relative">assets</exclude-pattern>

<rule ref="WordPress-Core">
<exclude name="WordPress.PHP.YodaConditions.NotYoda"/>
Expand Down

0 comments on commit 4006152

Please sign in to comment.