Skip to content

Commit 47ff5cd

Browse files
authored
Merge pull request #697 from ProgressPlanner/ari/reduce-code-duplication-with-traits
Reduce code duplication using traits
2 parents 89da8bc + 3d01dcf commit 47ff5cd

24 files changed

+2152
-136
lines changed

classes/admin/widgets/class-widget.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@
77

88
namespace Progress_Planner\Admin\Widgets;
99

10+
use Progress_Planner\Utils\Traits\Input_Sanitizer;
11+
1012
/**
1113
* Widgets class.
1214
*
1315
* All widgets should extend this class.
1416
*/
1517
abstract class Widget {
1618

19+
use Input_Sanitizer;
20+
1721
/**
1822
* The widget width.
1923
*
@@ -58,11 +62,7 @@ public function get_id() {
5862
* @return string
5963
*/
6064
public function get_range() {
61-
// phpcs:ignore WordPress.Security.NonceVerification
62-
return isset( $_GET['range'] )
63-
// phpcs:ignore WordPress.Security.NonceVerification
64-
? \sanitize_text_field( \wp_unslash( $_GET['range'] ) )
65-
: '-6 months';
65+
return $this->get_sanitized_get( 'range', '-6 months' );
6666
}
6767

6868
/**
@@ -71,11 +71,7 @@ public function get_range() {
7171
* @return string
7272
*/
7373
public function get_frequency() {
74-
// phpcs:ignore WordPress.Security.NonceVerification
75-
return isset( $_GET['frequency'] )
76-
// phpcs:ignore WordPress.Security.NonceVerification
77-
? \sanitize_text_field( \wp_unslash( $_GET['frequency'] ) )
78-
: 'monthly';
74+
return $this->get_sanitized_get( 'frequency', 'monthly' );
7975
}
8076

8177
/**

classes/suggested-tasks/providers/integrations/aioseo/class-archive-author.php

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@
88
namespace Progress_Planner\Suggested_Tasks\Providers\Integrations\AIOSEO;
99

1010
use Progress_Planner\Suggested_Tasks\Data_Collector\Post_Author;
11+
use Progress_Planner\Suggested_Tasks\Providers\Traits\Task_Action_Builder;
12+
use Progress_Planner\Suggested_Tasks\Providers\Traits\Ajax_Security_AIOSEO;
1113

1214
/**
1315
* Add task for All in One SEO: disable the author archive.
1416
*/
1517
class Archive_Author extends AIOSEO_Interactive_Provider {
1618

19+
use Task_Action_Builder;
20+
use Ajax_Security_AIOSEO;
21+
1722
/**
1823
* The minimum number of posts with a post format to add the task.
1924
*
@@ -148,19 +153,13 @@ public function print_popover_form_contents() {
148153
* @return void
149154
*/
150155
public function handle_interactive_task_specific_submit() {
151-
if ( ! \function_exists( 'aioseo' ) ) {
152-
\wp_send_json_error( [ 'message' => \esc_html__( 'AIOSEO is not active.', 'progress-planner' ) ] );
153-
}
156+
$this->verify_aioseo_active_or_fail();
157+
$this->verify_nonce_or_fail();
154158

155-
// Check the nonce.
156-
if ( ! \check_ajax_referer( 'progress_planner', 'nonce', false ) ) {
157-
\wp_send_json_error( [ 'message' => \esc_html__( 'Invalid nonce.', 'progress-planner' ) ] );
158-
}
159-
160-
\aioseo()->options->searchAppearance->archives->author->show = false;
159+
\aioseo()->options->searchAppearance->archives->author->show = false; // @phpstan-ignore-line
161160

162161
// Update the option.
163-
\aioseo()->options->save();
162+
\aioseo()->options->save(); // @phpstan-ignore-line
164163

165164
\wp_send_json_success( [ 'message' => \esc_html__( 'Setting updated.', 'progress-planner' ) ] );
166165
}
@@ -174,11 +173,6 @@ public function handle_interactive_task_specific_submit() {
174173
* @return array
175174
*/
176175
public function add_task_actions( $data = [], $actions = [] ) {
177-
$actions[] = [
178-
'priority' => 10,
179-
'html' => '<a href="#" class="prpl-tooltip-action-text" onclick="document.getElementById(\'prpl-popover-' . \esc_attr( static::POPOVER_ID ) . '\')?.showPopover();return false;">' . \esc_html__( 'Noindex', 'progress-planner' ) . '</a>',
180-
];
181-
182-
return $actions;
176+
return $this->add_popover_action( $actions, \__( 'Noindex', 'progress-planner' ) );
183177
}
184178
}

classes/suggested-tasks/providers/integrations/aioseo/class-archive-date.php

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@
77

88
namespace Progress_Planner\Suggested_Tasks\Providers\Integrations\AIOSEO;
99

10+
use Progress_Planner\Suggested_Tasks\Providers\Traits\Task_Action_Builder;
11+
use Progress_Planner\Suggested_Tasks\Providers\Traits\Ajax_Security_AIOSEO;
12+
1013
/**
1114
* Add task for All in One SEO: disable the date archive.
1215
*/
1316
class Archive_Date extends AIOSEO_Interactive_Provider {
1417

18+
use Task_Action_Builder;
19+
use Ajax_Security_AIOSEO;
20+
1521
/**
1622
* The provider ID.
1723
*
@@ -134,19 +140,13 @@ public function print_popover_form_contents() {
134140
* @return void
135141
*/
136142
public function handle_interactive_task_specific_submit() {
137-
if ( ! \function_exists( 'aioseo' ) ) {
138-
\wp_send_json_error( [ 'message' => \esc_html__( 'AIOSEO is not active.', 'progress-planner' ) ] );
139-
}
143+
$this->verify_aioseo_active_or_fail();
144+
$this->verify_nonce_or_fail();
140145

141-
// Check the nonce.
142-
if ( ! \check_ajax_referer( 'progress_planner', 'nonce', false ) ) {
143-
\wp_send_json_error( [ 'message' => \esc_html__( 'Invalid nonce.', 'progress-planner' ) ] );
144-
}
145-
146-
\aioseo()->options->searchAppearance->archives->date->show = false;
146+
\aioseo()->options->searchAppearance->archives->date->show = false; // @phpstan-ignore-line
147147

148148
// Update the option.
149-
\aioseo()->options->save();
149+
\aioseo()->options->save(); // @phpstan-ignore-line
150150

151151
\wp_send_json_success( [ 'message' => \esc_html__( 'Setting updated.', 'progress-planner' ) ] );
152152
}
@@ -160,11 +160,6 @@ public function handle_interactive_task_specific_submit() {
160160
* @return array
161161
*/
162162
public function add_task_actions( $data = [], $actions = [] ) {
163-
$actions[] = [
164-
'priority' => 10,
165-
'html' => '<a href="#" class="prpl-tooltip-action-text" onclick="document.getElementById(\'prpl-popover-' . \esc_attr( static::POPOVER_ID ) . '\')?.showPopover();return false;">' . \esc_html__( 'Noindex', 'progress-planner' ) . '</a>',
166-
];
167-
168-
return $actions;
163+
return $this->add_popover_action( $actions, \__( 'Noindex', 'progress-planner' ) );
169164
}
170165
}

classes/suggested-tasks/providers/integrations/aioseo/class-crawl-settings-feed-authors.php

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@
88
namespace Progress_Planner\Suggested_Tasks\Providers\Integrations\AIOSEO;
99

1010
use Progress_Planner\Suggested_Tasks\Data_Collector\Post_Author;
11+
use Progress_Planner\Suggested_Tasks\Providers\Traits\Task_Action_Builder;
12+
use Progress_Planner\Suggested_Tasks\Providers\Traits\Ajax_Security_AIOSEO;
1113

1214
/**
1315
* Add task for All in One SEO: disable author RSS feeds.
1416
*/
1517
class Crawl_Settings_Feed_Authors extends AIOSEO_Interactive_Provider {
1618

19+
use Task_Action_Builder;
20+
use Ajax_Security_AIOSEO;
21+
1722
/**
1823
* The minimum number of posts with a post format to add the task.
1924
*
@@ -145,19 +150,13 @@ public function print_popover_form_contents() {
145150
* @return void
146151
*/
147152
public function handle_interactive_task_specific_submit() {
148-
if ( ! \function_exists( 'aioseo' ) ) {
149-
\wp_send_json_error( [ 'message' => \esc_html__( 'AIOSEO is not active.', 'progress-planner' ) ] );
150-
}
153+
$this->verify_aioseo_active_or_fail();
154+
$this->verify_nonce_or_fail();
151155

152-
// Check the nonce.
153-
if ( ! \check_ajax_referer( 'progress_planner', 'nonce', false ) ) {
154-
\wp_send_json_error( [ 'message' => \esc_html__( 'Invalid nonce.', 'progress-planner' ) ] );
155-
}
156-
157-
\aioseo()->options->searchAppearance->advanced->crawlCleanup->feeds->authors = false;
156+
\aioseo()->options->searchAppearance->advanced->crawlCleanup->feeds->authors = false; // @phpstan-ignore-line
158157

159158
// Update the option.
160-
\aioseo()->options->save();
159+
\aioseo()->options->save(); // @phpstan-ignore-line
161160

162161
\wp_send_json_success( [ 'message' => \esc_html__( 'Setting updated.', 'progress-planner' ) ] );
163162
}
@@ -171,11 +170,6 @@ public function handle_interactive_task_specific_submit() {
171170
* @return array
172171
*/
173172
public function add_task_actions( $data = [], $actions = [] ) {
174-
$actions[] = [
175-
'priority' => 10,
176-
'html' => '<a href="#" class="prpl-tooltip-action-text" role="button" onclick="document.getElementById(\'prpl-popover-' . \esc_attr( static::POPOVER_ID ) . '\')?.showPopover();return false;">' . \esc_html__( 'Disable', 'progress-planner' ) . '</a>',
177-
];
178-
179-
return $actions;
173+
return $this->add_popover_action( $actions, \__( 'Disable', 'progress-planner' ) );
180174
}
181175
}

classes/suggested-tasks/providers/integrations/aioseo/class-crawl-settings-feed-comments.php

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@
77

88
namespace Progress_Planner\Suggested_Tasks\Providers\Integrations\AIOSEO;
99

10+
use Progress_Planner\Suggested_Tasks\Providers\Traits\Task_Action_Builder;
11+
use Progress_Planner\Suggested_Tasks\Providers\Traits\Ajax_Security_AIOSEO;
12+
1013
/**
1114
* Add task for All in One SEO: disable global comment RSS feeds.
1215
*/
1316
class Crawl_Settings_Feed_Comments extends AIOSEO_Interactive_Provider {
1417

18+
use Task_Action_Builder;
19+
use Ajax_Security_AIOSEO;
20+
1521
/**
1622
* The provider ID.
1723
*
@@ -113,14 +119,8 @@ public function print_popover_form_contents() {
113119
* @return void
114120
*/
115121
public function handle_interactive_task_specific_submit() {
116-
if ( ! \function_exists( 'aioseo' ) ) {
117-
\wp_send_json_error( [ 'message' => \esc_html__( 'AIOSEO is not active.', 'progress-planner' ) ] );
118-
}
119-
120-
// Check the nonce.
121-
if ( ! \check_ajax_referer( 'progress_planner', 'nonce', false ) ) {
122-
\wp_send_json_error( [ 'message' => \esc_html__( 'Invalid nonce.', 'progress-planner' ) ] );
123-
}
122+
$this->verify_aioseo_active_or_fail();
123+
$this->verify_nonce_or_fail();
124124

125125
// Global comment feed.
126126
if ( \aioseo()->options->searchAppearance->advanced->crawlCleanup->feeds->globalComments ) { // @phpstan-ignore-line
@@ -147,11 +147,6 @@ public function handle_interactive_task_specific_submit() {
147147
* @return array
148148
*/
149149
public function add_task_actions( $data = [], $actions = [] ) {
150-
$actions[] = [
151-
'priority' => 10,
152-
'html' => '<a href="#" class="prpl-tooltip-action-text" role="button" onclick="document.getElementById(\'prpl-popover-' . \esc_attr( static::POPOVER_ID ) . '\')?.showPopover();return false;">' . \esc_html__( 'Disable', 'progress-planner' ) . '</a>',
153-
];
154-
155-
return $actions;
150+
return $this->add_popover_action( $actions, \__( 'Disable', 'progress-planner' ) );
156151
}
157152
}

classes/suggested-tasks/providers/integrations/aioseo/class-media-pages.php

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@
77

88
namespace Progress_Planner\Suggested_Tasks\Providers\Integrations\AIOSEO;
99

10+
use Progress_Planner\Suggested_Tasks\Providers\Traits\Task_Action_Builder;
11+
use Progress_Planner\Suggested_Tasks\Providers\Traits\Ajax_Security_AIOSEO;
12+
1013
/**
1114
* Add task for All in One SEO: redirect media/attachment pages.
1215
*/
1316
class Media_Pages extends AIOSEO_Interactive_Provider {
1417

18+
use Task_Action_Builder;
19+
use Ajax_Security_AIOSEO;
20+
1521
/**
1622
* The provider ID.
1723
*
@@ -120,19 +126,13 @@ public function print_popover_form_contents() {
120126
* @return void
121127
*/
122128
public function handle_interactive_task_specific_submit() {
123-
if ( ! \function_exists( 'aioseo' ) ) {
124-
\wp_send_json_error( [ 'message' => \esc_html__( 'AIOSEO is not active.', 'progress-planner' ) ] );
125-
}
129+
$this->verify_aioseo_active_or_fail();
130+
$this->verify_nonce_or_fail();
126131

127-
// Check the nonce.
128-
if ( ! \check_ajax_referer( 'progress_planner', 'nonce', false ) ) {
129-
\wp_send_json_error( [ 'message' => \esc_html__( 'Invalid nonce.', 'progress-planner' ) ] );
130-
}
131-
132-
\aioseo()->dynamicOptions->searchAppearance->postTypes->attachment->redirectAttachmentUrls = 'attachment';
132+
\aioseo()->dynamicOptions->searchAppearance->postTypes->attachment->redirectAttachmentUrls = 'attachment'; // @phpstan-ignore-line
133133

134134
// Update the option.
135-
\aioseo()->options->save();
135+
\aioseo()->options->save(); // @phpstan-ignore-line
136136

137137
\wp_send_json_success( [ 'message' => \esc_html__( 'Setting updated.', 'progress-planner' ) ] );
138138
}
@@ -146,11 +146,6 @@ public function handle_interactive_task_specific_submit() {
146146
* @return array
147147
*/
148148
public function add_task_actions( $data = [], $actions = [] ) {
149-
$actions[] = [
150-
'priority' => 10,
151-
'html' => '<a href="#" class="prpl-tooltip-action-text" onclick="document.getElementById(\'prpl-popover-' . \esc_attr( static::POPOVER_ID ) . '\')?.showPopover();return false;">' . \esc_html__( 'Redirect', 'progress-planner' ) . '</a>',
152-
];
153-
154-
return $actions;
149+
return $this->add_popover_action( $actions, \__( 'Redirect', 'progress-planner' ) );
155150
}
156151
}

classes/suggested-tasks/providers/integrations/yoast/class-archive-author.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
88
namespace Progress_Planner\Suggested_Tasks\Providers\Integrations\Yoast;
99

1010
use Progress_Planner\Suggested_Tasks\Data_Collector\Post_Author;
11+
use Progress_Planner\Suggested_Tasks\Providers\Traits\Task_Action_Builder;
1112

1213
/**
1314
* Add task for Yoast SEO: disable the author archive.
1415
*/
1516
class Archive_Author extends Yoast_Interactive_Provider {
1617

18+
use Task_Action_Builder;
19+
1720
/**
1821
* The minimum number of posts with a post format to add the task.
1922
*
@@ -137,11 +140,6 @@ public function print_popover_form_contents() {
137140
* @return array
138141
*/
139142
public function add_task_actions( $data = [], $actions = [] ) {
140-
$actions[] = [
141-
'priority' => 10,
142-
'html' => '<a class="prpl-tooltip-action-text" href="#" class="prpl-tooltip-action-text" role="button" onclick="document.getElementById(\'prpl-popover-' . \esc_attr( static::POPOVER_ID ) . '\')?.showPopover()">' . \esc_html__( 'Disable', 'progress-planner' ) . '</a>',
143-
];
144-
145-
return $actions;
143+
return $this->add_popover_action( $actions, \__( 'Disable', 'progress-planner' ) );
146144
}
147145
}

classes/suggested-tasks/providers/integrations/yoast/class-archive-date.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@
77

88
namespace Progress_Planner\Suggested_Tasks\Providers\Integrations\Yoast;
99

10+
use Progress_Planner\Suggested_Tasks\Providers\Traits\Task_Action_Builder;
11+
1012
/**
1113
* Add task for Yoast SEO: disable the date archive.
1214
*/
1315
class Archive_Date extends Yoast_Interactive_Provider {
1416

17+
use Task_Action_Builder;
18+
1519
/**
1620
* The provider ID.
1721
*
@@ -124,11 +128,6 @@ public function print_popover_form_contents() {
124128
* @return array
125129
*/
126130
public function add_task_actions( $data = [], $actions = [] ) {
127-
$actions[] = [
128-
'priority' => 10,
129-
'html' => '<a class="prpl-tooltip-action-text" href="#" class="prpl-tooltip-action-text" role="button" onclick="document.getElementById(\'prpl-popover-' . \esc_attr( static::POPOVER_ID ) . '\')?.showPopover()">' . \esc_html__( 'Disable', 'progress-planner' ) . '</a>',
130-
];
131-
132-
return $actions;
131+
return $this->add_popover_action( $actions, \__( 'Disable', 'progress-planner' ) );
133132
}
134133
}

classes/suggested-tasks/providers/integrations/yoast/class-archive-format.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
88
namespace Progress_Planner\Suggested_Tasks\Providers\Integrations\Yoast;
99

1010
use Progress_Planner\Suggested_Tasks\Data_Collector\Archive_Format as Archive_Format_Data_Collector;
11+
use Progress_Planner\Suggested_Tasks\Providers\Traits\Task_Action_Builder;
1112

1213
/**
1314
* Add task for Yoast SEO: disable the format archives.
1415
*/
1516
class Archive_Format extends Yoast_Interactive_Provider {
1617

18+
use Task_Action_Builder;
19+
1720
/**
1821
* The provider ID.
1922
*
@@ -137,11 +140,6 @@ public function print_popover_form_contents() {
137140
* @return array
138141
*/
139142
public function add_task_actions( $data = [], $actions = [] ) {
140-
$actions[] = [
141-
'priority' => 10,
142-
'html' => '<a class="prpl-tooltip-action-text" href="#" class="prpl-tooltip-action-text" role="button" onclick="document.getElementById(\'prpl-popover-' . \esc_attr( static::POPOVER_ID ) . '\')?.showPopover()">' . \esc_html__( 'Disable', 'progress-planner' ) . '</a>',
143-
];
144-
145-
return $actions;
143+
return $this->add_popover_action( $actions, \__( 'Disable', 'progress-planner' ) );
146144
}
147145
}

0 commit comments

Comments
 (0)