| 1 |
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName -- File naming is acceptable |
| 2 |
/** |
| 3 |
* Settings management class. |
| 4 |
* |
| 5 |
* This class handles all settings-related functionality for the Analytify plugin. |
| 6 |
* |
| 7 |
* @package WP_Analytify |
| 8 |
* @since 1.0.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; // Exit if accessed directly. |
| 13 |
} |
| 14 |
|
| 15 |
// phpcs:ignore WordPress.Files.FileName.InvalidClassFileName -- File name follows project convention |
| 16 |
if ( ! class_exists( 'WP_Analytify_Settings' ) ) { |
| 17 |
|
| 18 |
// Include split traits for settings. |
| 19 |
require_once __DIR__ . '/analytify-settings/analytify-settings-sanitizers.php'; |
| 20 |
require_once __DIR__ . '/analytify-settings/definitions.php'; |
| 21 |
require_once __DIR__ . '/analytify-settings/render.php'; |
| 22 |
require_once __DIR__ . '/analytify-settings/helpers.php'; |
| 23 |
require_once __DIR__ . '/analytify-settings/actions.php'; |
| 24 |
require_once __DIR__ . '/analytify-settings/fields.php'; |
| 25 |
require_once __DIR__ . '/analytify-settings/promo.php'; |
| 26 |
require_once __DIR__ . '/analytify-settings/promo-helpers.php'; |
| 27 |
|
| 28 |
/** |
| 29 |
* Settings class. |
| 30 |
* |
| 31 |
* @package WP_Analytify |
| 32 |
* @since 1.0.0 |
| 33 |
*/ |
| 34 |
class WP_Analytify_Settings { |
| 35 |
|
| 36 |
use Analytify_Settings_Definitions; |
| 37 |
use Analytify_Settings_Render; |
| 38 |
use Analytify_Settings_Helpers; |
| 39 |
use Analytify_Settings_Actions { |
| 40 |
analytify_delete_cache as trait_analytify_delete_cache; |
| 41 |
} |
| 42 |
use Analytify_Settings_Fields { |
| 43 |
get_settings_fields as trait_get_settings_fields; |
| 44 |
} |
| 45 |
use Analytify_Settings_Promo { |
| 46 |
pro_features as trait_pro_features; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Settings sections array. |
| 51 |
* |
| 52 |
* @var array<string, mixed> |
| 53 |
*/ |
| 54 |
protected $settings_sections = array(); |
| 55 |
|
| 56 |
/** |
| 57 |
* Settings fields array. |
| 58 |
* |
| 59 |
* @var array<string, mixed> |
| 60 |
*/ |
| 61 |
protected $settings_fields = array(); |
| 62 |
|
| 63 |
/** |
| 64 |
* Constructor. |
| 65 |
*/ |
| 66 |
public function __construct() { |
| 67 |
|
| 68 |
if ( current_user_can( 'manage_options' ) && ! wp_doing_ajax() ) { |
| 69 |
add_action( 'admin_init', array( $this, 'admin_init' ) ); |
| 70 |
} |
| 71 |
|
| 72 |
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); |
| 73 |
add_action( 'admin_post_analytify_delete_cache', array( $this, 'analytify_delete_cache' ) ); |
| 74 |
add_action( 'admin_enqueue_scripts', array( $this, 'analytify_email_enqueue_media' ) ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Get current user roles. |
| 79 |
* |
| 80 |
* Back-compat: allow static calls from Pro to get roles list. |
| 81 |
* Also safe to call as instance method. |
| 82 |
* |
| 83 |
* @return array<string, mixed> Array of user roles. |
| 84 |
*/ |
| 85 |
public static function get_current_roles() { |
| 86 |
$roles = array(); |
| 87 |
$editable_roles = get_editable_roles(); |
| 88 |
if ( is_array( $editable_roles ) && ! empty( $editable_roles ) ) { |
| 89 |
foreach ( $editable_roles as $role_slug => $role_info ) { |
| 90 |
if ( isset( $role_info['name'] ) ) { |
| 91 |
$roles[ $role_slug ] = $role_info['name']; |
| 92 |
} |
| 93 |
} |
| 94 |
} else { |
| 95 |
$roles['empty'] = 'no roles found'; |
| 96 |
} |
| 97 |
return $roles; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Get current post types. |
| 102 |
* |
| 103 |
* Back-compat: allow static calls from Pro to get public post types. |
| 104 |
* Also safe to call as instance method. |
| 105 |
* |
| 106 |
* @return array<string, mixed> Array of post types. |
| 107 |
*/ |
| 108 |
public static function get_current_post_types() { |
| 109 |
$post_types_list = array(); |
| 110 |
$post_types = get_post_types( array( 'public' => true ) ); |
| 111 |
if ( is_array( $post_types ) ) { |
| 112 |
foreach ( $post_types as $post_type ) { |
| 113 |
$post_types_list[ $post_type ] = $post_type; |
| 114 |
} |
| 115 |
} |
| 116 |
return $post_types_list; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Add field to a section. |
| 121 |
* |
| 122 |
* @param string $section Section ID. |
| 123 |
* @param array<string, mixed> $field Field arguments. |
| 124 |
* @return $this |
| 125 |
*/ |
| 126 |
public function add_field( $section, $field ) { |
| 127 |
$defaults = array( |
| 128 |
'name' => '', |
| 129 |
'label' => '', |
| 130 |
'desc' => '', |
| 131 |
'type' => 'text', |
| 132 |
); |
| 133 |
|
| 134 |
$args = wp_parse_args( $field, $defaults ); |
| 135 |
$this->settings_fields[ $section ][] = $args; |
| 136 |
|
| 137 |
return $this; |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|