Skip to content

Commit a5e225f

Browse files
committed
Refactor Settings class methods to static and improve validation
1 parent b4f18f7 commit a5e225f

File tree

3 files changed

+362
-397
lines changed

3 files changed

+362
-397
lines changed

includes/admin/class-metabox.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,10 @@ public static function call_meta_box() {
107107
wp_nonce_field( 'crp_meta_box', 'crp_meta_box_nonce' );
108108

109109
// Get the thumbnail settings. The name of the meta key is defined in thumb_meta parameter of the CRP Settings array.
110-
$thumb_meta = get_post_meta( $post->ID, crp_get_option( 'thumb_meta' ), true );
110+
$thumb_meta = crp_get_meta( $post->ID, crp_get_option( 'thumb_meta' ) );
111111
$value = ( $thumb_meta ) ? $thumb_meta : '';
112112

113113
// Get related posts specific meta.
114-
// Using crp_get_meta() for backward compatibility with pre-4.2.0 crp_post_meta array storage.
115114
$disable_here = crp_get_meta( $post->ID, 'disable_here' );
116115
$exclude_this_post = crp_get_meta( $post->ID, 'exclude_this_post' );
117116
$keyword = crp_get_meta( $post->ID, 'keyword' );

includes/admin/class-settings.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,7 @@ public static function get_registered_settings() {
204204
foreach ( $sections as $section => $value ) {
205205
$method_name = 'settings_' . $section;
206206
if ( method_exists( __CLASS__, $method_name ) ) {
207-
$instance = new self();
208-
$settings[ $section ] = $instance->$method_name();
207+
$settings[ $section ] = self::$method_name();
209208
}
210209
}
211210

@@ -226,7 +225,7 @@ public static function get_registered_settings() {
226225
*
227226
* @return array General settings array
228227
*/
229-
public function settings_general() {
228+
public static function settings_general() {
230229
$settings = array(
231230
'list_general_header' => array(
232231
'id' => 'list_general_header',
@@ -368,7 +367,7 @@ public function settings_general() {
368367
*
369368
* @return array Output settings array
370369
*/
371-
public function settings_output() {
370+
public static function settings_output() {
372371
$settings = array(
373372
'title' => array(
374373
'id' => 'title',
@@ -548,7 +547,7 @@ public function settings_output() {
548547
*
549548
* @return array List Tuning settings array
550549
*/
551-
public function settings_list() {
550+
public static function settings_list() {
552551
$settings = array(
553552
'list_general_header' => array(
554553
'id' => 'list_general_header',
@@ -570,7 +569,7 @@ public function settings_list() {
570569
'desc' => esc_html__( 'Maximum number of posts that will be displayed in the list. This option is used if you do not specify the number of posts in the widget or shortcodes', 'contextual-related-posts' ),
571570
'type' => 'number',
572571
'default' => '6',
573-
'min' => '0',
572+
'min' => '1',
574573
'size' => 'small',
575574
),
576575
'daily_range' => array(
@@ -864,7 +863,7 @@ public function settings_list() {
864863
*
865864
* @return array Thumbnail settings array
866865
*/
867-
public function settings_thumbnail() {
866+
public static function settings_thumbnail() {
868867
$settings = array(
869868
'post_thumb_op' => array(
870869
'id' => 'post_thumb_op',
@@ -981,7 +980,7 @@ public function settings_thumbnail() {
981980
*
982981
* @return array Styles settings array
983982
*/
984-
public function settings_styles() {
983+
public static function settings_styles() {
985984
$settings = array(
986985
'crp_styles' => array(
987986
'id' => 'crp_styles',
@@ -1019,7 +1018,7 @@ public function settings_styles() {
10191018
*
10201019
* @return array Feed settings array
10211020
*/
1022-
public function settings_feed() {
1021+
public static function settings_feed() {
10231022
$settings = array(
10241023
'feed_options_desc' => array(
10251024
'id' => 'feed_options_desc',
@@ -1093,7 +1092,7 @@ public function settings_feed() {
10931092
*
10941093
* @return array WooCommerce settings array
10951094
*/
1096-
public function settings_woocommerce() {
1095+
public static function settings_woocommerce() {
10971096
$settings = array(
10981097
'wc_header' => array(
10991098
'id' => 'wc_header',
@@ -1280,7 +1279,7 @@ public function settings_woocommerce() {
12801279
*
12811280
* @return array Performance settings array
12821281
*/
1283-
public function settings_performance() {
1282+
public static function settings_performance() {
12841283
$custom_tables_desc = sprintf(
12851284
/* translators: 1: Opening a tag, 2: Closing a tag */
12861285
esc_html__( 'Efficient Content Storage and Indexing (ECSI) creates a dedicated database table optimized for related content queries. This enhances performance, particularly on sites with a large number of posts or high traffic. To create the ECSI tables, visit the %1$sTools tab%2$s.', 'contextual-related-posts' ),
@@ -1644,6 +1643,11 @@ public function change_settings_on_save( $settings ) {
16441643
list( $settings['thumb_width'], $settings['thumb_height'] ) = \WebberZone\Contextual_Related_Posts\Frontend\Media_Handler::get_thumb_size( $settings['thumb_size'] );
16451644
}
16461645

1646+
// Force limit to 6 if left blank to prevent array_slice errors.
1647+
if ( empty( $settings['limit'] ) ) {
1648+
$settings['limit'] = '6';
1649+
}
1650+
16471651
if ( isset( $_POST['crp_save_clear_cache'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
16481652
$count = Cache::delete();
16491653
add_settings_error(

0 commit comments

Comments
 (0)