| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Admin loader for the simple toolbar feature. |
| 5 |
* |
| 6 |
* Adds the Toolbar Style row (Simple / Full) to the user profile, |
| 7 |
* persists it on save, and reorders the row into place under the |
| 8 |
* core "Toolbar" row at print-footer time. The render decision |
| 9 |
* itself lives in `Frontend::style()` / `Frontend::shouldRender()`. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Extendify\Toolbar; |
| 13 |
|
| 14 |
defined('ABSPATH') || die('No direct access.'); |
| 15 |
|
| 16 |
class Admin |
| 17 |
{ |
| 18 |
public function __construct() |
| 19 |
{ |
| 20 |
\add_action('personal_options', [$this, 'renderProfileRow']); |
| 21 |
\add_action('personal_options_update', [$this, 'saveProfileRow']); |
| 22 |
\add_action('edit_user_profile_update', [$this, 'saveProfileRow']); |
| 23 |
\add_action('admin_print_footer_scripts-profile.php', [$this, 'reorderRow']); |
| 24 |
\add_action('admin_print_footer_scripts-user-edit.php', [$this, 'reorderRow']); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Render the Toolbar Style fieldset on the user profile page. |
| 29 |
* |
| 30 |
* @param \WP_User $user |
| 31 |
* @return void |
| 32 |
*/ |
| 33 |
public function renderProfileRow($user) |
| 34 |
{ |
| 35 |
$style = Frontend::style($user->ID); |
| 36 |
// phpcs:disable Generic.Files.LineLength.TooLong -- inline HTML form markup |
| 37 |
?> |
| 38 |
<tr class="extendify-toolbar-style-wrap"> |
| 39 |
<th scope="row"><?php \esc_html_e('Toolbar Style', 'extendify-local'); ?></th> |
| 40 |
<td> |
| 41 |
<fieldset> |
| 42 |
<legend class="screen-reader-text"><?php \esc_html_e('Toolbar Style', 'extendify-local'); ?></legend> |
| 43 |
<p> |
| 44 |
<label> |
| 45 |
<input type="radio" name="<?php echo \esc_attr(Frontend::STYLE_META); ?>" value="simple" <?php \checked($style, 'simple'); ?> /> |
| 46 |
<?php echo \esc_html_x('Simple', 'toolbar style', 'extendify-local'); ?> |
| 47 |
</label> |
| 48 |
</p> |
| 49 |
<p> |
| 50 |
<label> |
| 51 |
<input type="radio" name="<?php echo \esc_attr(Frontend::STYLE_META); ?>" value="full" <?php \checked($style, 'full'); ?> /> |
| 52 |
<?php echo \esc_html_x('Full', 'toolbar style', 'extendify-local'); ?> |
| 53 |
</label> |
| 54 |
</p> |
| 55 |
</fieldset> |
| 56 |
</td> |
| 57 |
</tr> |
| 58 |
<?php |
| 59 |
// phpcs:enable Generic.Files.LineLength.TooLong |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Persist the Toolbar Style choice on profile save. |
| 64 |
* |
| 65 |
* @param int $userId |
| 66 |
* @return void |
| 67 |
*/ |
| 68 |
public function saveProfileRow($userId) |
| 69 |
{ |
| 70 |
if (!\current_user_can('edit_user', $userId)) { |
| 71 |
return; |
| 72 |
} |
| 73 |
// phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 74 |
$raw = isset($_POST[Frontend::STYLE_META]) |
| 75 |
// phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.ValidatedSanitizedInput.MissingUnslash |
| 76 |
? \sanitize_text_field(\wp_unslash($_POST[Frontend::STYLE_META])) |
| 77 |
: 'simple'; |
| 78 |
$val = $raw === 'full' ? 'full' : 'simple'; |
| 79 |
\update_user_meta($userId, Frontend::STYLE_META, $val); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Move our row to sit immediately after the core "Toolbar" row. |
| 84 |
* |
| 85 |
* The core profile page hardcodes the Toolbar and Language rows |
| 86 |
* in user-edit.php and only fires `personal_options` AFTER both — |
| 87 |
* so PHP priority alone can't position our row between them. We |
| 88 |
* do it client-side at print time. |
| 89 |
* |
| 90 |
* @return void |
| 91 |
*/ |
| 92 |
public function reorderRow() |
| 93 |
{ |
| 94 |
?> |
| 95 |
<script> |
| 96 |
(function () { |
| 97 |
var ours = document.querySelector('tr.extendify-toolbar-style-wrap'); |
| 98 |
var anchor = document.querySelector('tr.user-admin-bar-front-wrap'); |
| 99 |
if (ours && anchor && anchor.parentNode) { |
| 100 |
anchor.parentNode.insertBefore(ours, anchor.nextSibling); |
| 101 |
} |
| 102 |
})(); |
| 103 |
</script> |
| 104 |
<?php |
| 105 |
} |
| 106 |
} |
| 107 |
|