| @@ -8,8 +8,9 @@ | ||
| 8 | 8 | namespace LassoLite\Models; |
| 9 | 9 | |
| 10 | 10 | use LassoLite\Classes\Cache_Per_Process; |
| 11 | 11 | use LassoLite\Classes\Helper as Lasso_Helper; |
| 12 | +use LassoLite\Classes\Setting_Enum as Lasso_Setting_Enum; | |
| 12 | 13 | use LassoLite\Classes\Update_DB; |
| 13 | 14 | |
| 14 | 15 | /** |
| 15 | 16 | * Model |
| @@ -507,8 +508,17 @@ | ||
| 507 | 508 | return self::query( $sql ); |
| 508 | 509 | } |
| 509 | 510 | |
| 510 | 511 | /** |
| 512 | + * Format keyword for searching | |
| 513 | + * | |
| 514 | + * @param string $keyword Keyword. | |
| 515 | + */ | |
| 516 | + public static function esc_like( $keyword ) { | |
| 517 | + return self::get_wpdb()->esc_like( $keyword ); | |
| 518 | + } | |
| 519 | + | |
| 520 | + /** | |
| 511 | 521 | * Get property name by method |
| 512 | 522 | * |
| 513 | 523 | * @param string $method Method. |
| 514 | 524 | */ |
| @@ -572,12 +582,15 @@ | ||
| 572 | 582 | * @return mixed Property value. |
| 573 | 583 | */ |
| 574 | 584 | public function __get( $name ) { |
| 575 | 585 | $method = strtolower( $name ); |
| 576 | - $property = $this->get_property_name( $method ); | |
| 586 | + $property = $method; | |
| 577 | 587 | |
| 578 | - if ( ! $property ) { | |
| 579 | - $property = $method; | |
| 588 | + if ( 0 === strpos( $method, 'get_' ) || 0 === strpos( $method, 'set_' ) ) { | |
| 589 | + $derived = $this->get_property_name( $method ); | |
| 590 | + if ( $derived ) { | |
| 591 | + $property = $derived; | |
| 592 | + } | |
| 580 | 593 | } |
| 581 | 594 | |
| 582 | 595 | if ( ! in_array( $property, $this->columns, true ) ) { |
| 583 | 596 | return; |
| @@ -886,9 +899,9 @@ | ||
| 886 | 899 | Update_DB::create_tables(); |
| 887 | 900 | } |
| 888 | 901 | |
| 889 | 902 | // ? Add force write log for lasso_debug, to see what happen when Lasso call query. |
| 890 | - trigger_error( $error, E_USER_NOTICE ); // phpcs:ignore | |
| 903 | + trigger_error( '[lasso-lite-sql] ' . $error, E_USER_NOTICE ); // phpcs:ignore | |
| 891 | 904 | } |
| 892 | 905 | } |
| 893 | 906 | |
| 894 | 907 | /** |
| @@ -896,23 +909,30 @@ | ||
| 896 | 909 | * |
| 897 | 910 | * @return bool |
| 898 | 911 | */ |
| 899 | 912 | private static function should_recreate_tables() { |
| 900 | - // ? Define global $$lasso_recreate_table_time if not existed. | |
| 901 | - if ( ! array_key_exists( 'lasso_recreate_table_time', $GLOBALS ) ) { | |
| 902 | - global $lasso_recreate_table_time; | |
| 903 | - $lasso_recreate_table_time = 0; | |
| 904 | - } else { | |
| 905 | - global $lasso_recreate_table_time; | |
| 906 | - } | |
| 913 | + $lasso_recreate_table_time = Lasso_Helper::get_option( Lasso_Setting_Enum::RECREATE_TABLE_TIME, 0 ); | |
| 914 | + $lasso_recreate_table_time = intval( $lasso_recreate_table_time ); | |
| 907 | 915 | |
| 908 | - // ? Increase number of times we call Lasso_Activator::create_lasso_table(). | |
| 916 | + // ? Increase number of times we call Update_DB::create_tables(). | |
| 909 | 917 | ++$lasso_recreate_table_time; |
| 918 | + if ( $lasso_recreate_table_time > self::LASSO_RECREATE_TABLES_LIMIT_TIMES ) { | |
| 919 | + $now = time(); | |
| 920 | + $next_run = Lasso_Helper::get_option( Lasso_Setting_Enum::NEXT_TIME_RECREATE_TABLE, 0 ); | |
| 921 | + $next_run = intval( $next_run ); | |
| 922 | + if ( 0 === $next_run ) { | |
| 923 | + Lasso_Helper::update_option( Lasso_Setting_Enum::NEXT_TIME_RECREATE_TABLE, strtotime( '+1 hour' ) ); | |
| 924 | + } | |
| 910 | 925 | |
| 911 | - if ( $lasso_recreate_table_time > self::LASSO_RECREATE_TABLES_LIMIT_TIMES ) { | |
| 926 | + if ( $next_run && $now > $next_run ) { | |
| 927 | + Lasso_Helper::update_option( Lasso_Setting_Enum::RECREATE_TABLE_TIME, 1 ); | |
| 928 | + Lasso_Helper::update_option( Lasso_Setting_Enum::NEXT_TIME_RECREATE_TABLE, 0 ); | |
| 929 | + return true; | |
| 930 | + } | |
| 931 | + | |
| 912 | 932 | return false; |
| 913 | 933 | } |
| 914 | - | |
| 934 | + Lasso_Helper::update_option( Lasso_Setting_Enum::RECREATE_TABLE_TIME, $lasso_recreate_table_time ); | |
| 915 | 935 | return true; |
| 916 | 936 | } |
| 917 | 937 | |
| 918 | 938 | /** |
| @@ -940,9 +960,9 @@ | ||
| 940 | 960 | $reference_charset_table = $reference_charset_table ? $reference_charset_table : self::get_wp_table_name( 'posts' ); |
| 941 | 961 | $reference_charset_status = $this->get_table_charset( $reference_charset_table, true ); |
| 942 | 962 | $current_table_status = $this->get_table_charset( $table, false ); |
| 943 | 963 | |
| 944 | - if ( ! $reference_charset_status[1] !== $current_table_status[1] ) { | |
| 964 | + if ( $reference_charset_status[1] !== $current_table_status[1] ) { | |
| 945 | 965 | $result = $this->update_table_collation( $table, $reference_charset_status[0], $reference_charset_status[1] ); |
| 946 | 966 | } else { |
| 947 | 967 | $result = true; |
| 948 | 968 | } |
| @@ -953,15 +973,9 @@ | ||
| 953 | 973 | $check_msg_txt = $check->Msg_text ?? ''; // phpcs:ignore |
| 954 | 974 | if ( 'OK' === $check_msg_txt ) { |
| 955 | 975 | $result = 'The table is okay, it does not need to be repaired.'; |
| 956 | 976 | } else { |
| 957 | - // @codeCoverageIgnoreStart | |
| 958 | - $repair = self::get_row( "REPAIR TABLE $table" ); | |
| 959 | - $repair_msg_txt = $repair->Msg_text ?? ''; // phpcs:ignore | |
| 960 | - $result = 'OK' === $repair_msg_txt | |
| 961 | - ? "Successfully repaired the $table table." | |
| 962 | - : "Failed to repair the $table table. Error: $repair->Msg_text"; | |
| 963 | - // @codeCoverageIgnoreEnd | |
| 977 | + self::log_error( $check_msg_txt ); | |
| 964 | 978 | } |
| 965 | 979 | } |
| 966 | 980 | |
| 967 | 981 | return array( $table, $result ); |