Skip to content

Commit

Permalink
= 4.1.6.9 =
Browse files Browse the repository at this point in the history
~ Fixed: case drag item to another section (sortable)
~ Fixed: create first section, order will be 1 not 2
  • Loading branch information
tungnxt89 committed Jul 28, 2022
1 parent d89dac5 commit 1b91da0
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 95 deletions.
2 changes: 1 addition & 1 deletion config/settings/course.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
array(
'title' => esc_html__( 'Section Per Page', 'learnpress' ),
'id' => 'section_per_page',
'default' => 2,
'default' => 1,
'type' => 'number',
'desc' => esc_html__( 'Number of sections displayed per page ( Enter -1 for display all sections).', 'learnpress' ),
),
Expand Down
17 changes: 10 additions & 7 deletions inc/admin/editor/class-lp-admin-editor-course.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,22 +130,26 @@ public function sort_sections( $args = array() ) {
* @return mixed
*/
public function update_section( $args = array() ) {
$section = ! empty( $args['section'] ) ? $args['section'] : false;
$section = $args['section'] ?? false;
$section = json_decode( wp_unslash( $section ), true );

if ( ! $section ) {
return false;
}

$update = array(
if ( ! isset( $section['course_id'] ) && ! isset( $section['id'] ) ) {
return false;
}

$data = array(
'section_id' => $section['id'],
'section_name' => $section['title'],
'section_description' => $section['description'],
'section_order' => $section['order'],
'section_course_id' => $section['course_id'],
);

$this->result = $this->section_curd->update( $update );
$this->result = $this->section_curd->update( $data );

return true;
}
Expand Down Expand Up @@ -173,8 +177,8 @@ public function remove_section( $args = array() ) {
* @return mixed
*/
public function new_section( $args = array() ) {
$section_name = ! empty( $args['section_name'] ) ? $args['section_name'] : false;
$temp_id = isset( $args['temp_id'] ) ? $args['temp_id'] : 0;
$section_name = $args['section_name'] ?? '';
$temp_id = $args['temp_id'] ?? 0;

$args = array(
'section_course_id' => $this->course->get_id(),
Expand Down Expand Up @@ -310,8 +314,7 @@ public function update_section_items( $args = array() ) {
return false;
}

//$this->result = $this->section_curd->update_section_items( $section_id, $items );
$this->result = $this->section_curd->update_section_items_order( $section_id, $items );
$this->result = $this->section_curd->update_section_items( $section_id, $items );

//$this->section_curd->update_final_item();

Expand Down
2 changes: 2 additions & 0 deletions inc/background-process/class-lp-background-single-course.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ protected function save_extra_info() {
// Clean cache
$key_cache_sections_items = "$course_id/sections_items";
$lp_course_cache->clear( $key_cache_sections_items );
$key_cache_sections = "$course_id/sections";
$lp_course_cache->clear( $key_cache_sections );
$sections_items = $lp_course->get_sections_and_items_course_from_db_and_sort();
$extra_info->sections_items = $sections_items;

Expand Down
2 changes: 0 additions & 2 deletions inc/background-process/class-lp-background-single-order.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ class LP_Background_Single_Order extends WP_Async_Request {

/**
* Get params via $_POST and handle
* @in_array
* @see LP_Course_Post_Type::save
*/
protected function handle() {

Expand Down
5 changes: 3 additions & 2 deletions inc/course/class-lp-course.php
Original file line number Diff line number Diff line change
Expand Up @@ -615,10 +615,11 @@ public function get_full_sections_and_items_course(): array {
public function get_sections_and_items_course_from_db_and_sort(): array {
$sections_items = [];
$course_id = $this->get_id();
$lp_course_db = LP_Course_DB::getInstance();

try {
$sections_results = LP_Course_DB::getInstance()->get_sections( $course_id );
$sections_items_results = LP_Course_DB::getInstance()->get_full_sections_and_items_course( $course_id );
$sections_results = $lp_course_db->get_sections( $course_id );
$sections_items_results = $lp_course_db->get_full_sections_and_items_course( $course_id );
$count_items = count( $sections_items_results );
$index_items_last = $count_items - 1;
$section_current = 0;
Expand Down
96 changes: 22 additions & 74 deletions inc/curds/class-lp-section-curd.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,30 @@ public function __construct( $course_id ) {
* @since 3.0.0
*/
public function create( &$args ) {

global $wpdb;
$section = [];

$section = $this->parse( $args );
$section = stripslashes_deep( $section );
$section['section_order'] = $this->get_last_number_order( $section['section_course_id'] ) + 1;
$insert_data = array(
'section_course_id' => $this->course_id,
'section_name' => $section['section_name'],
'section_order' => $section['section_order'],
'section_description' => $section['section_description'],
);

$wpdb->insert(
$wpdb->learnpress_sections,
$insert_data,
array( '%d', '%s', '%d', '%s' )
);
$section['section_id'] = $wpdb->insert_id;
try {
$section = $this->parse( $args );
$section = stripslashes_deep( $section );
$last_section_order_number = LP_Section_DB::getInstance()->get_last_number_order( $section['section_course_id'] );
$section['section_order'] = $last_section_order_number + 1;
$insert_data = array(
'section_course_id' => $this->course_id,
'section_name' => $section['section_name'],
'section_order' => $section['section_order'],
'section_description' => $section['section_description'],
);

// allow hook
do_action( 'learn-press/after-create-section', $this->course_id, $section );
$wpdb->insert(
$wpdb->learnpress_sections,
$insert_data,
array( '%d', '%s', '%d', '%s' )
);
$section['section_id'] = $wpdb->insert_id;
} catch ( Throwable $e ) {
error_log( $e->getMessage() );
}

return $section;
}
Expand Down Expand Up @@ -226,6 +228,7 @@ private function parse( $args ) {
* @param int $section_id
*
* @return array
* @depecated 4.1.6.9 Remove when release Addon Frontend Editor 4.0.1
*/
public function read_items( $section_id ) {
global $wpdb;
Expand All @@ -248,20 +251,6 @@ public function read_items( $section_id ) {
return $wpdb->get_col( $query );
}

/**
* @param $course_id
*
* @return int
*/
private function get_last_number_order( $course_id ) {
global $wpdb;

$query = $wpdb->prepare( "SELECT MAX(s.section_order) FROM {$wpdb->prefix}learnpress_sections AS s WHERE s.section_course_id = %d", $course_id );
$result = intval( $wpdb->get_var( $query ) );

return ( $result > 0 ) ? $result : 1;
}

/**
* Update sort sections.
*
Expand Down Expand Up @@ -597,10 +586,8 @@ public function remove_section_item( $section_id, $item_id ) {
*
* @return array
* @since 3.0.0
* @depecated 4.1.6.9
*/
public function update_section_items( $section_id, $items ) {
_deprecated_function( __FUNCTION__, '4.1.6.9' );
$current_items = $this->get_section_items( $section_id );

global $wpdb;
Expand Down Expand Up @@ -648,48 +635,9 @@ public function update_section_items( $section_id, $items ) {
}
}

// LP_Object_Cache::set( 'course-' . $this->course_id . '-' . $section_id, $items, 'learn-press/course-section-items' );

return $items;
}

/**
* Update section items order in course
*
* @param $section_id
* @param $items
*
* @return bool
* @editor tungnx
* @modify 4.1.6.9
* @version 4.0.1
*/
public function update_section_items_order( $section_id, $items ): bool {
global $wpdb;
$flag = false;

try {
foreach ( $items as $index => $item ) {
$order = $index + 1;

$flag = $wpdb->update(
$wpdb->learnpress_section_items,
array( 'item_order' => $order ),
array(
'section_id' => $section_id,
'item_id' => $item['id'],
)
);
}

$flag = true;
} catch ( Throwable $e ) {
error_log( $e->getMessage() );
}

return $flag;
}

/**
* Check item exist.
*
Expand Down
25 changes: 25 additions & 0 deletions inc/databases/class-lp-section-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,5 +307,30 @@ public function get_section_id_by_item_id( $item_id ) {

return false;
}

/**
* Get last section order of course
*
* @param int $course_id
*
* @return int
* @throws Exception
* @since 4.1.6.9
* @version 1.0.0
*/
public function get_last_number_order( int $course_id = 0 ): int {
$query = $this->wpdb->prepare(
"SELECT MAX(section_order)
FROM $this->tb_lp_sections
WHERE section_course_id = %d",
$course_id
);

$number_order = intval( $this->wpdb->get_var( $query ) );

$this->check_execute_has_error();

return $number_order;
}
}

9 changes: 0 additions & 9 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,15 @@

<rule ref="WordPress-Core">
<exclude name="WordPress.PHP.YodaConditions.NotYoda"/>
<exclude name="Squiz.Commenting.InlineComment.InvalidEndChar"/>
<exclude name="WordPress.DB.PreparedSQL.NotPrepared"/>
<exclude name="WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar"/>
<exclude name="WordPress.NamingConventions.ValidHookName.UseUnderscores"/>
<exclude name="Squiz.Commenting.FunctionComment.ParamCommentFullStop"/>
<exclude name="Squiz.Commenting.FunctionComment.MissingParamTag"/>
<exclude name="Squiz.Commenting.FileComment.SpacingAfterComment"/>
<exclude name="WordPress.WP.I18n.MissingTranslatorsComment"/>
<exclude name="Squiz.Commenting.FunctionComment.MissingParamName"/>
<exclude name="Squiz.Commenting.VariableComment.EmptyVar"/>
<exclude name="Generic.Commenting.DocComment.MissingShort"/>
<exclude name="Squiz.Commenting.FunctionComment.MissingParamComment"/>
<exclude name="WordPress.Files.FileName.NotHyphenatedLowercase"/>
<exclude name="WordPress.Files.FileName.InvalidClassFileName"/>
<exclude name="WordPress.PHP.StrictComparisons.LooseComparison"/>
<exclude name="WordPress.PHP.StrictInArray.MissingTrueStrict"/>
<exclude name="WordPress.PHP.RestrictedPHPFunctions.date_date"/>
<exclude name="Squiz.PHP.DisallowMultipleAssignments.Found"/>
<exclude name="Generic.Files.LineEndings.InvalidEOLChar"/>
<exclude name="WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase"/>
<exclude name="WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid"/>
Expand Down

0 comments on commit 1b91da0

Please sign in to comment.