| 1 |
<?php |
| 2 |
/** |
| 3 |
* Admin Settings Page |
| 4 |
* |
| 5 |
* @package AutomatorWP\Admin\Settings |
| 6 |
* @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]> |
| 7 |
* @since 1.3.2 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
require_once AUTOMATORWP_DIR . 'includes/admin/settings/general.php'; |
| 13 |
|
| 14 |
/** |
| 15 |
* Register AutomatorWP Settings with Settings API. |
| 16 |
* |
| 17 |
* @since 1.0.0 |
| 18 |
* |
| 19 |
* @return void |
| 20 |
*/ |
| 21 |
function automatorwp_register_settings() { |
| 22 |
|
| 23 |
register_setting( 'automatorwp_settings', 'automatorwp_settings' ); |
| 24 |
|
| 25 |
} |
| 26 |
add_action( 'admin_init', 'automatorwp_register_settings' ); |
| 27 |
|
| 28 |
/** |
| 29 |
* Register settings page. |
| 30 |
* |
| 31 |
* @since 1.0.0 |
| 32 |
* |
| 33 |
* @return void |
| 34 |
*/ |
| 35 |
function automatorwp_register_settings_page() { |
| 36 |
|
| 37 |
$tabs = array(); |
| 38 |
$boxes = array(); |
| 39 |
|
| 40 |
$is_settings_page = ( isset( $_GET['page'] ) && $_GET['page'] === 'automatorwp_settings' ); |
| 41 |
|
| 42 |
if( $is_settings_page ) { |
| 43 |
|
| 44 |
// Loop settings sections |
| 45 |
foreach( automatorwp_get_settings_sections() as $section_id => $section ) { |
| 46 |
|
| 47 |
$meta_boxes = array(); |
| 48 |
|
| 49 |
/** |
| 50 |
* Filter: automatorwp_settings_{$section_id}_meta_boxes |
| 51 |
* |
| 52 |
* @param array $meta_boxes |
| 53 |
* |
| 54 |
* @return array |
| 55 |
*/ |
| 56 |
$meta_boxes = apply_filters( "automatorwp_settings_{$section_id}_meta_boxes", $meta_boxes ); |
| 57 |
|
| 58 |
if( ! empty( $meta_boxes ) ) { |
| 59 |
|
| 60 |
// Loop settings section meta boxes |
| 61 |
foreach( $meta_boxes as $meta_box_id => $meta_box ) { |
| 62 |
|
| 63 |
// Check meta box tabs |
| 64 |
if( isset( $meta_box['tabs'] ) && ! empty( $meta_box['tabs'] ) ) { |
| 65 |
|
| 66 |
// Loop meta box tabs |
| 67 |
foreach( $meta_box['tabs'] as $tab_id => $tab ) { |
| 68 |
|
| 69 |
$tab['id'] = $tab_id; |
| 70 |
|
| 71 |
$meta_box['tabs'][$tab_id] = $tab; |
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
// Only add settings meta box if has fields |
| 78 |
if( isset( $meta_box['fields'] ) && ! empty( $meta_box['fields'] ) ) { |
| 79 |
|
| 80 |
// Loop meta box fields |
| 81 |
foreach( $meta_box['fields'] as $field_id => $field ) { |
| 82 |
|
| 83 |
$field['id'] = $field_id; |
| 84 |
|
| 85 |
// Support for group fields |
| 86 |
if( isset( $field['fields'] ) && is_array( $field['fields'] ) ) { |
| 87 |
|
| 88 |
foreach( $field['fields'] as $group_field_id => $group_field ) { |
| 89 |
|
| 90 |
$field['fields'][$group_field_id]['id'] = $group_field_id; |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
$meta_box['fields'][$field_id] = $field; |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
$meta_box['id'] = $meta_box_id; |
| 101 |
|
| 102 |
$meta_box['display_cb'] = false; |
| 103 |
$meta_box['admin_menu_hook'] = false; |
| 104 |
$meta_box['priority'] = 'high'; // Fixes issue with CMB2 2.9.0 |
| 105 |
|
| 106 |
$meta_box['show_on'] = array( |
| 107 |
'key' => 'options-page', |
| 108 |
'value' => array( 'automatorwp_settings' ), |
| 109 |
); |
| 110 |
|
| 111 |
$box = new_cmb2_box( $meta_box ); |
| 112 |
|
| 113 |
$box->object_type( 'options-page' ); |
| 114 |
|
| 115 |
$boxes[] = $box; |
| 116 |
|
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
$tabs[] = array( |
| 121 |
'id' => $section_id, |
| 122 |
'title' => ( ( isset( $section['icon'] ) ) ? '<i class="dashicons ' . $section['icon'] . '"></i>' : '' ) . $section['title'], |
| 123 |
'desc' => '', |
| 124 |
'boxes' => array_keys( $meta_boxes ), |
| 125 |
); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
try { |
| 132 |
// Create the options page |
| 133 |
new Cmb2_Metatabs_Options( array( |
| 134 |
'key' => 'automatorwp_settings', |
| 135 |
'class' => 'automatorwp-page', |
| 136 |
'title' => __( 'Settings', 'automatorwp' ), |
| 137 |
'topmenu' => 'automatorwp', |
| 138 |
'cols' => 1, |
| 139 |
'boxes' => $boxes, |
| 140 |
'tabs' => $tabs, |
| 141 |
'menuargs' => array( |
| 142 |
'menu_title' => __( 'Settings', 'automatorwp' ), |
| 143 |
'capability' => 'manage_options', |
| 144 |
'view_capability' => 'manage_options', |
| 145 |
), |
| 146 |
'savetxt' => __( 'Save Settings', 'automatorwp' ), |
| 147 |
'resettxt' => __( 'Reset Settings', 'automatorwp' ), |
| 148 |
) ); |
| 149 |
} catch ( Exception $e ) { |
| 150 |
|
| 151 |
} |
| 152 |
|
| 153 |
} |
| 154 |
add_action( 'cmb2_admin_init', 'automatorwp_register_settings_page', 12 ); |
| 155 |
|
| 156 |
/** |
| 157 |
* AutomatorWP registered settings sections |
| 158 |
* |
| 159 |
* @since 1.0.1 |
| 160 |
* |
| 161 |
* @return array |
| 162 |
*/ |
| 163 |
function automatorwp_get_settings_sections() { |
| 164 |
|
| 165 |
$automatorwp_settings_sections = array( |
| 166 |
'general' => array( |
| 167 |
'title' => __( 'Settings', 'automatorwp' ), |
| 168 |
'icon' => 'dashicons-admin-settings', |
| 169 |
), |
| 170 |
); |
| 171 |
|
| 172 |
return apply_filters( 'automatorwp_settings_sections', $automatorwp_settings_sections ); |
| 173 |
|
| 174 |
} |
| 175 |
|