-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass-permalink-structure.php
More file actions
301 lines (268 loc) · 9.24 KB
/
Copy pathclass-permalink-structure.php
File metadata and controls
301 lines (268 loc) · 9.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
/**
* Add tasks for permalink structure.
*
* @package Progress_Planner
*/
namespace Progress_Planner\Suggested_Tasks\Providers;
/**
* Add tasks for permalink structure.
*/
class Permalink_Structure extends Tasks_Interactive {
/**
* Whether the task is an onboarding task.
*
* @var bool
*/
protected const IS_ONBOARDING_TASK = true;
/**
* The provider ID.
*
* @var string
*/
protected const PROVIDER_ID = 'core-permalink-structure';
/**
* The popover ID.
*
* @var string
*/
const POPOVER_ID = 'core-permalink-structure';
/**
* The external link URL.
*
* @var string
*/
protected const EXTERNAL_LINK_URL = 'https://prpl.fyi/change-default-permalink-structure';
/**
* The task priority.
*
* @var int
*/
protected $priority = 3;
/**
* Initialize the task.
*
* @return void
*/
public function init() {
\add_action( 'wp_ajax_prpl_interactive_task_submit_core-permalink-structure', [ $this, 'handle_interactive_task_specific_submit' ] );
\add_action( 'init', [ $this, 'maybe_flush_rewrite_rules' ] );
}
/**
* Maybe flush rewrite rules.
*
* Checks for a transient flag and flushes rewrite rules if needed.
* This is more performant than flushing immediately during the AJAX request.
*
* @return void
*/
public function maybe_flush_rewrite_rules() {
if ( \get_transient( 'prpl_flush_rewrite_rules' ) ) {
\flush_rewrite_rules();
\delete_transient( 'prpl_flush_rewrite_rules' );
}
}
/**
* Get the task URL.
*
* @return string
*/
protected function get_url() {
return \admin_url( 'options-permalink.php' );
}
/**
* Get the link setting.
*
* @return array
*/
public function get_link_setting() {
$icon_el = 'label[for="permalink-input-month-name"], label[for="permalink-input-post-name"]';
// If the task is completed, we want to add icon element only to the selected option (not both).
if ( $this->is_task_completed() ) {
$permalink_structure = \get_option( 'permalink_structure' );
if ( '/%year%/%monthnum%/%postname%/' === $permalink_structure || '/index.php/%year%/%monthnum%/%postname%/' === $permalink_structure ) {
$icon_el = 'label[for="permalink-input-month-name"]';
}
if ( '/%postname%/' === $permalink_structure || '/index.php/%postname%/' === $permalink_structure ) {
$icon_el = 'label[for="permalink-input-post-name"]';
}
}
return [
'hook' => 'options-permalink.php',
'iconEl' => $icon_el,
];
}
/**
* Get the title.
*
* @return string
*/
protected function get_title() {
return \esc_html__( 'Set permalink structure', 'progress-planner' );
}
/**
* Check if the task condition is satisfied.
* (bool) true means that the task condition is satisfied, meaning that we don't need to add the task or task was completed.
*
* @return bool
*/
public function should_add_task() {
$permalink_structure = \get_option( 'permalink_structure' );
return '/%year%/%monthnum%/%day%/%postname%/' === $permalink_structure || '/index.php/%year%/%monthnum%/%day%/%postname%/' === $permalink_structure;
}
/**
* Get the popover instructions.
*
* @return void
*/
public function print_popover_instructions() {
echo '<p>';
\esc_html_e( 'By default, WordPress uses date-based URLs (e.g., /2025/01/21/post-name/) which can make your content seem outdated. SEO-friendly URLs help search engines and visitors better understand your content.', 'progress-planner' );
echo '</p>';
}
/**
* Print the popover input field for the form.
*
* @return void
*/
public function print_popover_form_contents() {
$permalink_structure = \get_option( 'permalink_structure' );
$prefix = \got_url_rewrite() ? '' : '/index.php';
$url_base = \home_url( $prefix );
$index_php_prefix = \got_url_rewrite() ? '' : '/index.php';
// Default structure values from WP core.
$structures = [
[
'id' => 'day-name',
'value' => $index_php_prefix . '/%year%/%monthnum%/%day%/%postname%/',
'label' => \__( 'Day and name', 'progress-planner' ),
'code' => $url_base . '/' . \gmdate( 'Y/m/d' ) . '/sample-post/',
],
[
'id' => 'month-name',
'value' => $index_php_prefix . '/%year%/%monthnum%/%postname%/',
'label' => \__( 'Month and name', 'progress-planner' ),
'code' => $url_base . '/' . \gmdate( 'Y/m' ) . '/sample-post/',
],
[
'id' => 'numeric',
'value' => $index_php_prefix . '/archives/%post_id%',
'label' => \__( 'Numeric', 'progress-planner' ),
'code' => $url_base . '/archives/123',
],
[
'id' => 'post-name',
'value' => $index_php_prefix . '/%postname%/',
'label' => \__( 'Post name', 'progress-planner' ),
'code' => $url_base . '/sample-post/',
],
];
$default_structure_values = \wp_list_pluck( $structures, 'value' );
$is_custom = ! \in_array( $permalink_structure, $default_structure_values, true );
?>
<div class="radios">
<fieldset class="prpl-structure-selection">
<?php foreach ( $structures as $structure ) : ?>
<div class="prpl-radio-wrapper">
<label class="prpl-custom-radio">
<input
id="prpl-permalink-input-<?php echo \esc_attr( $structure['id'] ); ?>"
name="prpl_permalink_structure"
type="radio"
value="<?php echo \esc_attr( $structure['value'] ); ?>"
<?php \checked( $structure['value'], $permalink_structure ); ?>
/>
<span class="prpl-custom-control"></span>
<div>
<span><?php echo \esc_html( $structure['label'] ); ?></span>
<code style="display: block;"><?php echo \esc_html( $structure['code'] ); ?></code>
</div>
</label>
</div>
<?php endforeach; ?>
<?php /* Custom permalink structure. */ ?>
<div class="prpl-radio-wrapper">
<label class="prpl-custom-radio">
<input
id="prpl_permalink_structure_custom_radio"
name="prpl_permalink_structure"
type="radio"
value="custom"
<?php \checked( $is_custom ); ?>
/>
<span class="prpl-custom-control"></span> <span>
<?php
\printf(
/* translators: %s: Screen reader text "enter a custom date format in the following field". */
\esc_html__( 'Custom: %s', 'progress-planner' ),
/* translators: Hidden accessibility text. */
'<span class="screen-reader-text">' . \esc_html__( 'enter a custom permalink structure in the following field', 'progress-planner' ) . '</span>'
);
?>
</span>
</label>
</div>
<label for="prpl_custom_permalink_structure" class="screen-reader-text">
<?php
/* translators: Hidden accessibility text. */
\esc_html_e( 'Custom permalink structure:', 'progress-planner' );
?>
</label>
<div style="display: flex; gap: 0.5rem; align-items: center;">
<code style="display: flex; align-items: center; height: 1.25rem;"><?php echo \esc_html( $url_base ); ?></code>
<input type="text" name="prpl_custom_permalink_structure" id="prpl_custom_permalink_structure" value="<?php echo \esc_attr( $permalink_structure ); ?>" class="small-text" />
</div>
</fieldset>
</div>
<?php
$this->print_submit_button( \__( 'Set permalink structure', 'progress-planner' ) );
}
/**
* Handle the interactive task submit.
*
* This is only for interactive tasks that change core permalink settings.
* The $_POST data is expected to be:
* - value: (mixed) The value to update the setting to.
* - nonce: (string) The nonce.
*
* @return void
*/
public function handle_interactive_task_specific_submit() {
// Check if the user has the necessary capabilities.
if ( ! \current_user_can( 'manage_options' ) ) {
\wp_send_json_error( [ 'message' => \esc_html__( 'You do not have permission to update settings.', 'progress-planner' ) ] );
}
// Check the nonce.
if ( ! \check_ajax_referer( 'progress_planner', 'nonce', false ) ) {
\wp_send_json_error( [ 'message' => \esc_html__( 'Invalid nonce.', 'progress-planner' ) ] );
}
if ( ! isset( $_POST['value'] ) ) {
\wp_send_json_error( [ 'message' => \esc_html__( 'Missing value.', 'progress-planner' ) ] );
}
$permalink_structure = trim( \sanitize_text_field( \wp_unslash( $_POST['value'] ) ) );
if ( empty( $permalink_structure ) ) {
\wp_send_json_error( [ 'message' => \esc_html__( 'Invalid permalink structure.', 'progress-planner' ) ] );
}
// Update the permalink structure.
\update_option( 'permalink_structure', $permalink_structure );
// Set a transient to flush rewrite rules on the next page load.
// This is more performant than flushing immediately during the AJAX request.
\set_transient( 'prpl_flush_rewrite_rules', 1, HOUR_IN_SECONDS );
\wp_send_json_success( [ 'message' => \esc_html__( 'Permalink structure updated.', 'progress-planner' ) ] );
}
/**
* Add task actions specific to this task.
*
* @param array $data The task data.
* @param array $actions The existing actions.
*
* @return array
*/
public function add_task_actions( $data = [], $actions = [] ) {
$actions[] = [
'priority' => 10,
'html' => '<a href="#" class="prpl-tooltip-action-text" role="button" onclick="document.getElementById(\'prpl-popover-' . \esc_attr( static::POPOVER_ID ) . '\')?.showPopover()">' . \esc_html__( 'Select permalink structure', 'progress-planner' ) . '</a>',
];
return $actions;
}
}