| 1 |
<?php // phpcs:ignore Squiz.Commenting.FileComment.Missing -- File doc comment exists |
| 2 |
/** |
| 3 |
* Analytify Settings Helpers Trait |
| 4 |
* |
| 5 |
* This trait contains utility and helper functions for the Analytify settings functionality. |
| 6 |
* It was created to provide common helper methods that support the main settings operations, |
| 7 |
* keeping the code DRY and well-organized. |
| 8 |
* |
| 9 |
* PURPOSE: |
| 10 |
* - Provides utility functions for settings operations |
| 11 |
* - Handles common settings-related tasks |
| 12 |
* - Offers helper methods for data processing |
| 13 |
* |
| 14 |
* @package WP_Analytify |
| 15 |
* @subpackage Settings |
| 16 |
* @since 8.0.0 |
| 17 |
*/ |
| 18 |
|
| 19 |
trait Analytify_Settings_Helpers { |
| 20 |
|
| 21 |
/** |
| 22 |
* Get current post types. |
| 23 |
* |
| 24 |
* @return array |
| 25 |
*/ |
| 26 |
public function get_current_post_types() { |
| 27 |
$post_types_list = array(); |
| 28 |
$args = array( 'public' => true ); |
| 29 |
$post_types = get_post_types( $args ); |
| 30 |
foreach ( $post_types as $post_type ) { |
| 31 |
$post_types_list[ $post_type ] = $post_type; |
| 32 |
} |
| 33 |
return $post_types_list; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Get current user roles. |
| 38 |
* |
| 39 |
* @return array |
| 40 |
*/ |
| 41 |
public function get_current_roles() { |
| 42 |
$roles = array(); |
| 43 |
if ( get_editable_roles() > 0 ) { |
| 44 |
foreach ( get_editable_roles() as $role => $name ) { |
| 45 |
$roles[ $role ] = $name['name']; |
| 46 |
} |
| 47 |
} else { |
| 48 |
$roles['empty'] = 'no roles found'; |
| 49 |
} |
| 50 |
return $roles; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Get field description. |
| 55 |
* |
| 56 |
* @param mixed $args The field arguments. |
| 57 |
* @return string |
| 58 |
*/ |
| 59 |
public function get_field_description( $args ) { |
| 60 |
if ( isset( $args['desc'] ) && ! empty( $args['desc'] ) ) { |
| 61 |
$desc = sprintf( '<p class="description">%s</p>', wp_kses_post( $args['desc'] ) ); |
| 62 |
} else { |
| 63 |
$desc = ''; |
| 64 |
} |
| 65 |
return $desc; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Sanitize options. |
| 70 |
* |
| 71 |
* @param mixed $options The options to sanitize. |
| 72 |
* @return mixed |
| 73 |
*/ |
| 74 |
public function sanitize_options( $options ) { |
| 75 |
if ( ! is_array( $options ) ) { |
| 76 |
return $options; |
| 77 |
} |
| 78 |
|
| 79 |
$this->analytify_settings_merge_oauth_email_for_sanitize( $options ); |
| 80 |
|
| 81 |
foreach ( $options as $option_slug => $option_value ) { |
| 82 |
$sanitize_callback = $this->get_sanitize_callback( $option_slug ); |
| 83 |
if ( $sanitize_callback ) { |
| 84 |
$options[ $option_slug ] = call_user_func( $sanitize_callback, $option_value ); |
| 85 |
continue; |
| 86 |
} |
| 87 |
} |
| 88 |
return $options; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Preserve plugin-managed Google OAuth email when saving the authentication option. |
| 93 |
* |
| 94 |
* The settings form only registers `manual_ua_code`; the email key is not user-editable. |
| 95 |
* Any posted value for that key is removed, then a valid value is copied from the DB row. |
| 96 |
* Corrupt non-array DB values are ignored (no indexing notices). |
| 97 |
* |
| 98 |
* @since 9.1.0 |
| 99 |
* @param array<string, mixed> $options Options being sanitized (modified by reference). |
| 100 |
* @return void |
| 101 |
*/ |
| 102 |
private function analytify_settings_merge_oauth_email_for_sanitize( array &$options ) { |
| 103 |
if ( ! array_key_exists( 'manual_ua_code', $options ) ) { |
| 104 |
return; |
| 105 |
} |
| 106 |
if ( ! defined( 'ANALYTIFY_AUTHENTICATION_OPTION_NAME' ) || ! defined( 'ANALYTIFY_GOOGLE_OAUTH_EMAIL_KEY' ) ) { |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
$email_key = ANALYTIFY_GOOGLE_OAUTH_EMAIL_KEY; |
| 111 |
unset( $options[ $email_key ] ); |
| 112 |
|
| 113 |
$prev_auth = get_option( ANALYTIFY_AUTHENTICATION_OPTION_NAME, array() ); |
| 114 |
if ( ! is_array( $prev_auth ) || ! isset( $prev_auth[ $email_key ] ) || ! is_string( $prev_auth[ $email_key ] ) ) { |
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
$kept = sanitize_email( $prev_auth[ $email_key ] ); |
| 119 |
if ( is_email( $kept ) ) { |
| 120 |
$options[ $email_key ] = $kept; |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
$logger = function_exists( 'analytify_get_logger' ) ? analytify_get_logger() : null; |
| 125 |
if ( $logger && method_exists( $logger, 'debug' ) ) { |
| 126 |
$logger->debug( |
| 127 |
'Analytify: omitted invalid stored Google OAuth email while saving authentication options.', |
| 128 |
array( 'source' => 'analytify_settings_merge_oauth_email_for_sanitize' ) |
| 129 |
); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Get sanitize callback. |
| 135 |
* |
| 136 |
* @param mixed $slug The option slug. |
| 137 |
* @return mixed |
| 138 |
*/ |
| 139 |
public function get_sanitize_callback( $slug = '' ) { |
| 140 |
if ( empty( $slug ) ) { |
| 141 |
return false; } |
| 142 |
foreach ( $this->settings_fields as $section => $options ) { |
| 143 |
foreach ( $options as $option ) { |
| 144 |
// Skip invalid options that don't have required fields. |
| 145 |
if ( ! isset( $option['name'] ) ) { |
| 146 |
continue; |
| 147 |
} |
| 148 |
|
| 149 |
if ( $option['name'] !== $slug ) { |
| 150 |
continue; } |
| 151 |
return isset( $option['sanitize_callback'] ) && is_callable( $option['sanitize_callback'] ) ? $option['sanitize_callback'] : false; |
| 152 |
} |
| 153 |
} |
| 154 |
return false; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Get option value. |
| 159 |
* |
| 160 |
* @param mixed $option The option name. |
| 161 |
* @param mixed $section The section name. |
| 162 |
* @param mixed $default The default value. |
| 163 |
* @return mixed |
| 164 |
*/ |
| 165 |
public function get_option( $option, $section, $default = '' ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.defaultFound -- Default parameter name is acceptable |
| 166 |
$options = get_option( $section ); |
| 167 |
if ( isset( $options[ $option ] ) ) { |
| 168 |
return $options[ $option ]; |
| 169 |
} |
| 170 |
return $default; |
| 171 |
} |
| 172 |
} |
| 173 |
|