| 1 |
<?php // phpcs:ignore Squiz.Commenting.FileComment.Missing -- File doc comment exists |
| 2 |
/** |
| 3 |
* Analytify Settings Definitions Trait |
| 4 |
* |
| 5 |
* This trait contains the core settings structure and configuration for the Analytify plugin. |
| 6 |
* It was created to separate the settings definitions logic from the main settings class, |
| 7 |
* making the code more modular and easier to maintain. |
| 8 |
* |
| 9 |
* PURPOSE: |
| 10 |
* - Defines the main settings sections and tabs structure |
| 11 |
* - Manages settings sections and fields arrays |
| 12 |
* - Provides methods to add and retrieve settings configuration |
| 13 |
* |
| 14 |
* @package WP_Analytify |
| 15 |
* @subpackage Settings |
| 16 |
* @since 8.0.0 |
| 17 |
*/ |
| 18 |
|
| 19 |
trait Analytify_Settings_Definitions { |
| 20 |
|
| 21 |
/** |
| 22 |
* Set the complete sections array for settings. |
| 23 |
* |
| 24 |
* @param array<string, mixed> $sections Array of settings sections. |
| 25 |
* @return $this For method chaining. |
| 26 |
*/ |
| 27 |
public function set_sections( $sections ) { |
| 28 |
$this->settings_sections = $sections; |
| 29 |
return $this; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Add a new section to the settings. |
| 34 |
* |
| 35 |
* @param array<string, mixed> $section Section configuration array. |
| 36 |
* @return $this For method chaining. |
| 37 |
*/ |
| 38 |
public function add_section( $section ) { |
| 39 |
if ( isset( $section['id'] ) ) { |
| 40 |
$this->settings_sections[ $section['id'] ] = $section; |
| 41 |
} else { |
| 42 |
// Generate a unique key for sections without ID. |
| 43 |
$key = 'section_' . count( $this->settings_sections ); |
| 44 |
$this->settings_sections[ $key ] = $section; |
| 45 |
} |
| 46 |
return $this; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Set the complete fields array for settings. |
| 51 |
* |
| 52 |
* @param array<string, mixed> $fields Array of settings fields. |
| 53 |
* @return $this For method chaining. |
| 54 |
*/ |
| 55 |
public function set_fields( $fields ) { |
| 56 |
$this->settings_fields = $fields; |
| 57 |
return $this; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Get the main settings sections with tabs configuration. |
| 62 |
* |
| 63 |
* This method defines the core structure of the Analytify settings page, |
| 64 |
* including authentication, profile selection, admin settings, tracking options, |
| 65 |
* advanced configurations, and help sections. |
| 66 |
* |
| 67 |
* @return array<string, mixed> Array of settings sections with tabs and accordion structure. |
| 68 |
* @version 9.0.0 |
| 69 |
*/ |
| 70 |
public function get_settings_sections() { |
| 71 |
$tabs = array( |
| 72 |
array( |
| 73 |
'id' => 'wp-analytify-authentication', |
| 74 |
'title' => __( 'Authentication', 'wp-analytify' ), |
| 75 |
'priority' => '5', |
| 76 |
), |
| 77 |
array( |
| 78 |
'id' => 'wp-analytify-profile', |
| 79 |
'title' => __( 'Profile', 'wp-analytify' ), |
| 80 |
'desc' => 'Select your profiles for front-end and back-end sections.', |
| 81 |
'priority' => '10', |
| 82 |
), |
| 83 |
array( |
| 84 |
'id' => 'wp-analytify-admin', |
| 85 |
'title' => __( 'Admin', 'wp-analytify' ), |
| 86 |
'desc' => 'Following settings will take effect statistics under the posts, custom post types or pages.', |
| 87 |
'priority' => '20', |
| 88 |
), |
| 89 |
array( |
| 90 |
'id' => 'wp-analytify-tracking', |
| 91 |
'title' => __( 'Tracking', 'wp-analytify' ), |
| 92 |
'desc' => 'This section has options to Track forms, events, conversions, Setup and Custom Dimensions.', |
| 93 |
'accordion' => array( |
| 94 |
array( |
| 95 |
'id' => 'wp-analytify-events-tracking', |
| 96 |
'title' => __( 'Events Tracking', 'wp-analytify' ), |
| 97 |
), |
| 98 |
array( |
| 99 |
'id' => 'wp-analytify-custom-dimensions', |
| 100 |
'title' => __( 'Custom Dimensions', 'wp-analytify' ), |
| 101 |
), |
| 102 |
array( |
| 103 |
'id' => 'wp-analytify-forms', |
| 104 |
'title' => __( 'Forms Tracking', 'wp-analytify' ), |
| 105 |
), |
| 106 |
array( |
| 107 |
'id' => 'analytify-google-ads-tracking', |
| 108 |
'title' => __( 'Google Ads Tracking', 'wp-analytify' ), |
| 109 |
), |
| 110 |
array( |
| 111 |
'id' => 'analytify-pixels-tracking', |
| 112 |
'title' => __( 'Pixels Tracking', 'wp-analytify' ), |
| 113 |
), |
| 114 |
), |
| 115 |
'priority' => '32', |
| 116 |
), |
| 117 |
array( |
| 118 |
'id' => 'wp-analytify-advanced', |
| 119 |
'title' => __( 'Advanced', 'wp-analytify' ), |
| 120 |
'desc' => 'Configure the following settings for advanced analytics tracking.', |
| 121 |
'priority' => '35', |
| 122 |
), |
| 123 |
array( |
| 124 |
'id' => 'wp-analytify-help', |
| 125 |
'title' => __( 'Help', 'wp-analytify' ), |
| 126 |
'priority' => '45', |
| 127 |
), |
| 128 |
); |
| 129 |
|
| 130 |
$setting_tabs = apply_filters( 'wp_analytify_pro_setting_tabs', $tabs ); |
| 131 |
|
| 132 |
// Ensure all tabs have priority and validate data. |
| 133 |
$setting_tabs = array_map( |
| 134 |
function ( $tab ) { |
| 135 |
// Ensure priority exists and is numeric. |
| 136 |
if ( ! isset( $tab['priority'] ) || ! is_numeric( $tab['priority'] ) ) { |
| 137 |
$tab['priority'] = 999; // Default high priority for tabs without priority. |
| 138 |
} |
| 139 |
|
| 140 |
// Ensure required fields exist. |
| 141 |
if ( ! isset( $tab['id'] ) ) { |
| 142 |
$tab['id'] = 'unknown-tab-' . uniqid(); |
| 143 |
} |
| 144 |
|
| 145 |
if ( ! isset( $tab['title'] ) ) { |
| 146 |
$tab['title'] = __( 'Unknown Tab', 'wp-analytify' ); |
| 147 |
} |
| 148 |
|
| 149 |
return $tab; |
| 150 |
}, |
| 151 |
$setting_tabs |
| 152 |
); |
| 153 |
|
| 154 |
// Sort tabs by priority. |
| 155 |
usort( |
| 156 |
$setting_tabs, |
| 157 |
function ( $a, $b ) { |
| 158 |
return (int) $a['priority'] - (int) $b['priority']; |
| 159 |
} |
| 160 |
); |
| 161 |
|
| 162 |
// Convert to associative array with id as key. |
| 163 |
$result = array(); |
| 164 |
foreach ( $setting_tabs as $tab ) { |
| 165 |
if ( isset( $tab['id'] ) ) { |
| 166 |
$result[ $tab['id'] ] = $tab; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
return $result; |
| 171 |
} |
| 172 |
} |
| 173 |
|