| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace TheFrosty\CustomLogin; |
| 6 |
|
| 7 |
use TheFrosty\CustomLogin\Settings\Api\Factory; |
| 8 |
use TheFrosty\WpUtilities\Plugin\PluginInterface; |
| 9 |
use function add_action; |
| 10 |
use function function_exists; |
| 11 |
use function get_editable_roles; |
| 12 |
use function is_admin; |
| 13 |
use function is_array; |
| 14 |
use function is_string; |
| 15 |
use function preg_match; |
| 16 |
use function sanitize_key; |
| 17 |
use function sprintf; |
| 18 |
use function str_contains; |
| 19 |
use function wp_doing_ajax; |
| 20 |
use const WEEK_IN_SECONDS; |
| 21 |
|
| 22 |
const CUSTOM_LOGIN_FUNCTIONS = true; |
| 23 |
|
| 24 |
/** |
| 25 |
* Are we on the Custom Login settings page? |
| 26 |
* phpcs:disable SlevomatCodingStandard.Variables.DisallowSuperGlobalVariable.DisallowedSuperGlobalVariable |
| 27 |
* @param PluginInterface $plugin |
| 28 |
* @return bool |
| 29 |
*/ |
| 30 |
function isSettingsPage(PluginInterface $plugin): bool |
| 31 |
{ |
| 32 |
global $pagenow; |
| 33 |
return $pagenow === 'options-general.php' && |
| 34 |
isset($_GET['page']) && |
| 35 |
strpos($plugin->getSlug(), $_GET['page']) !== false; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Return all editable role capabilities. |
| 40 |
* @return array<string, string> |
| 41 |
*/ |
| 42 |
function getWpRoles(): array |
| 43 |
{ |
| 44 |
$key = sprintf('%1$s%2$s', Factory::PREFIX, sanitize_key(__FUNCTION__)); |
| 45 |
$roles = get_transient($key); |
| 46 |
if (empty($roles) && is_admin() && !wp_doing_ajax()) { |
| 47 |
add_action('shutdown', static function () use ($key, &$roles): void { |
| 48 |
$roles = _getEditableRoles(); |
| 49 |
set_transient($key, $roles, WEEK_IN_SECONDS); |
| 50 |
}); |
| 51 |
} |
| 52 |
|
| 53 |
return !is_array($roles) ? ['manage_options' => 'manage_options'] : $roles; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Browser prefixes. |
| 58 |
* @param string $property |
| 59 |
* @param string $value |
| 60 |
* @return string |
| 61 |
* @since 1.1 (1/8/13) |
| 62 |
*/ |
| 63 |
function prefixIt(string $property, string $value): string |
| 64 |
{ |
| 65 |
$output = "\n\t"; |
| 66 |
foreach (['-webkit-', '-moz-', ''] as $prefix) { |
| 67 |
$output .= trailingSemicolonIt(sprintf('%1$s%2$s', $prefix, $property), $value); |
| 68 |
} |
| 69 |
|
| 70 |
return $output; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Add a Trailing Semicolon. |
| 75 |
* @param string $property |
| 76 |
* @param string $value |
| 77 |
* @return string |
| 78 |
* @since 1.1 (1/8/13) |
| 79 |
* @updated 1.1.1 (1/9/13) Remove esc_attr since it's encoding single quotes in image urls with quotes. |
| 80 |
*/ |
| 81 |
function trailingSemicolonIt(string $property, string $value): string |
| 82 |
{ |
| 83 |
return sprintf("%s: %s;\n\t", $property, rtrim($value, ';')); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Open a new CSS rule. |
| 88 |
* @param string $value |
| 89 |
* @return string |
| 90 |
* @since 2.0 |
| 91 |
*/ |
| 92 |
function openCssRule(string $value): string |
| 93 |
{ |
| 94 |
return sprintf("%s {\n\t", rtrim($value, '{')); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Return all editable role capabilities. |
| 99 |
* @link http://codex.wordpress.org/Function_Reference/get_editable_roles |
| 100 |
* @access private |
| 101 |
* @return array<string, string> |
| 102 |
*/ |
| 103 |
function _getEditableRoles(): array |
| 104 |
{ |
| 105 |
$roles = []; |
| 106 |
$get_editable_roles = !function_exists('get_editable_roles') ? null : get_editable_roles(); |
| 107 |
if (empty($get_editable_roles)) { |
| 108 |
return ['manage_options' => 'manage_options']; |
| 109 |
} |
| 110 |
foreach ($get_editable_roles as $role) { |
| 111 |
/* |
| 112 |
* Avoid "Invalid argument supplied for foreach()". |
| 113 |
* @link https://wordpress.org/support/topic/invalid-argument-supplied-for-foreach-error-line-in-wp-dashboard?replies=2#post-6427631 |
| 114 |
*/ |
| 115 |
if (!isset($role['capabilities']) || !is_array($role['capabilities'])) { |
| 116 |
continue; |
| 117 |
} |
| 118 |
foreach ($role['capabilities'] as $capability => $array) { |
| 119 |
// Remove the (deprecated) capabilities from the array. |
| 120 |
if (is_string($capability) && preg_match('/^level_/', $capability)) { |
| 121 |
continue; |
| 122 |
} |
| 123 |
$roles[$capability] = $capability; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
return $roles; |
| 128 |
} |
| 129 |
|