| 1 |
<?php |
| 2 |
/** |
| 3 |
* UpStream_Admin_Options |
| 4 |
* |
| 5 |
* @package UpStream |
| 6 |
*/ |
| 7 |
|
| 8 |
// Exit if accessed directly. |
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
if ( ! class_exists( 'UpStream_Admin_Options' ) ) : |
| 14 |
|
| 15 |
/** |
| 16 |
* CMB2 Theme Options |
| 17 |
* |
| 18 |
* @version 0.1.0 |
| 19 |
*/ |
| 20 |
class UpStream_Admin_Options { |
| 21 |
|
| 22 |
|
| 23 |
/** |
| 24 |
* Array of metaboxes/fields |
| 25 |
* |
| 26 |
* @var array |
| 27 |
*/ |
| 28 |
public $option_metabox = array(); |
| 29 |
|
| 30 |
/** |
| 31 |
* Array of metaboxes/fields |
| 32 |
* |
| 33 |
* @var array |
| 34 |
*/ |
| 35 |
public $metabox_id = ''; |
| 36 |
|
| 37 |
/** |
| 38 |
* Options Page title |
| 39 |
* |
| 40 |
* @var string |
| 41 |
*/ |
| 42 |
protected $title = ''; |
| 43 |
|
| 44 |
/** |
| 45 |
* Options Page title |
| 46 |
* |
| 47 |
* @var string |
| 48 |
*/ |
| 49 |
protected $menu_title = ''; |
| 50 |
|
| 51 |
/** |
| 52 |
* Options Tab Pages |
| 53 |
* |
| 54 |
* @var array |
| 55 |
*/ |
| 56 |
public $options_pages = array(); |
| 57 |
|
| 58 |
/** |
| 59 |
* Holds an instance of the object |
| 60 |
* |
| 61 |
* @var Myprefix_Admin |
| 62 |
**/ |
| 63 |
private static $instance = null; |
| 64 |
|
| 65 |
/** |
| 66 |
* Constructor |
| 67 |
* |
| 68 |
* @since 0.1.0 |
| 69 |
*/ |
| 70 |
private function __construct() { |
| 71 |
// Set our title. |
| 72 |
$this->menu_title = __( 'UpStream', 'upstream' ); |
| 73 |
$this->title = __( 'UpStream Project Manager', 'upstream' ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Returns the running object |
| 78 |
* |
| 79 |
* @return Myprefix_Admin |
| 80 |
**/ |
| 81 |
public static function get_instance() { |
| 82 |
if ( is_null( self::$instance ) ) { |
| 83 |
self::$instance = new self(); |
| 84 |
self::$instance->hooks(); |
| 85 |
} |
| 86 |
|
| 87 |
return self::$instance; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Initiate our hooks |
| 92 |
* |
| 93 |
* @since 0.1.0 |
| 94 |
*/ |
| 95 |
public function hooks() { |
| 96 |
add_action( 'admin_init', array( $this, 'init' ) ); |
| 97 |
add_action( 'admin_menu', array( $this, 'add_options_pages' ) ); |
| 98 |
|
| 99 |
add_filter( 'cmb2_set_options', array( $this, 'filter_cmb2_set_options' ), 10, 2 ); |
| 100 |
add_filter( 'allex_upgrade_show_sidebar_ad', array( $this, 'filter_allex_upgrade_show_sidebar_ad' ), 20, 3 ); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Register our setting to WP |
| 105 |
* |
| 106 |
* @since 0.1.0 |
| 107 |
*/ |
| 108 |
public function init() { |
| 109 |
$option_tabs = self::option_fields(); |
| 110 |
foreach ( $option_tabs as $index => $option_tab ) { |
| 111 |
register_setting( $option_tab['id'], $option_tab['id'] ); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Add Options Pages |
| 117 |
* |
| 118 |
* @return void |
| 119 |
*/ |
| 120 |
public function add_options_pages() { |
| 121 |
$option_tabs = self::option_fields(); |
| 122 |
|
| 123 |
foreach ( $option_tabs as $index => $option_tab ) { |
| 124 |
if ( 0 === $index ) { |
| 125 |
$this->options_pages[] = add_menu_page( |
| 126 |
$this->title, |
| 127 |
$this->menu_title, |
| 128 |
'manage_upstream', |
| 129 |
$option_tab['id'], |
| 130 |
array( $this, 'admin_page_display' ), |
| 131 |
'dashicons-arrow-up-alt' |
| 132 |
); // Link admin menu to first tab. |
| 133 |
|
| 134 |
add_submenu_page( |
| 135 |
$option_tabs[0]['id'], |
| 136 |
$this->menu_title, |
| 137 |
$option_tab['menu_title'], |
| 138 |
'manage_upstream', |
| 139 |
$option_tab['id'], |
| 140 |
array( $this, 'admin_page_display' ) |
| 141 |
); // Duplicate menu link for first submenu page. |
| 142 |
} else { |
| 143 |
$this->options_pages[] = add_submenu_page( |
| 144 |
$option_tabs[0]['id'], |
| 145 |
$this->menu_title, |
| 146 |
$option_tab['menu_title'], |
| 147 |
'manage_upstream', |
| 148 |
$option_tab['id'], |
| 149 |
array( $this, 'admin_page_display' ) |
| 150 |
); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
foreach ( $this->options_pages as $page ) { |
| 155 |
// Include CMB CSS in the head to avoid FOUC. |
| 156 |
add_action( "admin_print_styles-{$page}", array( 'CMB2_hookup', 'enqueue_cmb_css' ) ); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Block displaying the sidebar twice, when viewing the extensions' page. |
| 162 |
* |
| 163 |
* @param mixed $show_display Show Display. |
| 164 |
* @param string $plugin_name Plugin Name. |
| 165 |
* @param string $context Context. |
| 166 |
* |
| 167 |
* @return bool |
| 168 |
*/ |
| 169 |
public function filter_allex_upgrade_show_sidebar_ad( $show_display, $plugin_name = '', $context = '' ) { |
| 170 |
if ( 'upstream' === $plugin_name && 'addons' === $context ) { |
| 171 |
return false; |
| 172 |
} |
| 173 |
|
| 174 |
return $show_display; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Admin page markup. Mostly handled by CMB2 |
| 179 |
* |
| 180 |
* @since 0.1.0 |
| 181 |
*/ |
| 182 |
public function admin_page_display() { |
| 183 |
$option_tabs = apply_filters( 'upstream_option_metaboxes', self::option_fields() ); // get all option tabs. |
| 184 |
$tab_forms = array(); |
| 185 |
|
| 186 |
/** |
| 187 |
* Filter to return a boolean value to say if it should display or not a sidebar. |
| 188 |
* |
| 189 |
* @var bool |
| 190 |
*/ |
| 191 |
$show_sidebar = apply_filters( 'allex_upgrade_show_sidebar_ad', true, 'upstream' ); ?> |
| 192 |
|
| 193 |
<div class="wrap upstream_options <?php echo $show_sidebar ? 'upstream_with_sidebar container' : ''; ?>"> |
| 194 |
|
| 195 |
<h2><?php echo esc_html( $this->title ); ?></h2> |
| 196 |
|
| 197 |
<div class="row"> |
| 198 |
<div class="<?php echo $show_sidebar ? 'col-md-8' : 'col-md-12'; ?>"> |
| 199 |
<!-- Options Page Nav Tabs --> |
| 200 |
<h2 class="nav-tab-wrapper"> |
| 201 |
<?php |
| 202 |
foreach ( $option_tabs as $option_tab ) : |
| 203 |
$tab_slug = $option_tab['id']; |
| 204 |
$nav_class = 'nav-tab'; |
| 205 |
$get_data = isset( $_GET ) ? wp_unslash( $_GET ) : array(); |
| 206 |
|
| 207 |
if ( sanitize_text_field( $get_data['page'] ) === $tab_slug ) { |
| 208 |
$nav_class .= ' nav-tab-active'; // add active class to current tab. |
| 209 |
$tab_forms[] = $option_tab; // add current tab to forms to be rendered. |
| 210 |
} |
| 211 |
?> |
| 212 |
<a class="<?php echo esc_attr( $nav_class ); ?>" |
| 213 |
href="<?php esc_url( menu_page_url( $tab_slug ) ); ?>"> |
| 214 |
<?php |
| 215 |
echo esc_attr( $option_tab['title'] ); |
| 216 |
?> |
| 217 |
</a> |
| 218 |
<?php endforeach; ?> |
| 219 |
</h2> |
| 220 |
<!-- End of Nav Tabs --> |
| 221 |
|
| 222 |
<?php foreach ( $tab_forms as $tab_form ) : // render all tab forms (normally just 1 form). ?> |
| 223 |
<div id="<?php echo esc_attr( $tab_form['id'] ); ?>" class="cmb-form group"> |
| 224 |
<div class="metabox-holder"> |
| 225 |
<div class="postbox pad"> |
| 226 |
<h3 class="title"><?php // esc_html_e($tab_form['title'], 'upstream');. ?></h3> |
| 227 |
<div class="desc"><?php echo esc_html( $tab_form['desc'] ); ?></div> |
| 228 |
<?php cmb2_metabox_form( $tab_form, $tab_form['id'] ); ?> |
| 229 |
</div> |
| 230 |
</div> |
| 231 |
</div> |
| 232 |
<?php endforeach; ?> |
| 233 |
</div> |
| 234 |
|
| 235 |
<?php if ( $show_sidebar ) : ?> |
| 236 |
<div class="col-md-3"> |
| 237 |
<?php do_action( 'allex_upgrade_sidebar_ad', 'upstream' ); ?> |
| 238 |
</div> |
| 239 |
<?php endif; ?> |
| 240 |
</div> |
| 241 |
</div> |
| 242 |
|
| 243 |
<?php |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
/** |
| 248 |
* Add the options metabox to the array of metaboxes |
| 249 |
* |
| 250 |
* @since 0.1.0 |
| 251 |
*/ |
| 252 |
public function option_fields() { |
| 253 |
// hook in our save notices |
| 254 |
// add_action( "cmb2_save_options-page_fields_{$this->metabox_id}", array( $this, 'settings_notices' ), 10, 2 );. |
| 255 |
|
| 256 |
// Only need to initiate the array once per page-load. |
| 257 |
if ( ! empty( $this->option_metabox ) ) { |
| 258 |
return $this->option_metabox; |
| 259 |
} |
| 260 |
|
| 261 |
$general_options = new UpStream_Options_General(); |
| 262 |
$this->option_metabox[] = $general_options->options(); |
| 263 |
|
| 264 |
$project_options = new UpStream_Options_Projects(); |
| 265 |
$this->option_metabox[] = $project_options->options(); |
| 266 |
|
| 267 |
// $milestone_options = new UpStream_Options_Milestones(); |
| 268 |
// $this->option_metabox[] = $milestone_options->options(); |
| 269 |
|
| 270 |
if ( ! upstream_disable_tasks() ) { |
| 271 |
$task_options = new UpStream_Options_Tasks(); |
| 272 |
$this->option_metabox[] = $task_options->options(); |
| 273 |
} |
| 274 |
|
| 275 |
if ( ! upstream_disable_bugs() ) { |
| 276 |
$bug_options = new UpStream_Options_Bugs(); |
| 277 |
$this->option_metabox[] = $bug_options->options(); |
| 278 |
} |
| 279 |
|
| 280 |
$options = (array) get_option( 'upstream_general' ); |
| 281 |
$enable = ! empty( $options['beta_features'] ) && ! empty( $options['beta_features'][0] ) ? '1' === (string) $options['beta_features'][0] : false; |
| 282 |
|
| 283 |
if ( $enable ) { |
| 284 |
if ( class_exists( 'UpStream_Options_Import' ) ) { |
| 285 |
$import_options = new UpStream_Options_Import(); |
| 286 |
$this->option_metabox[] = $import_options->options(); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
$container = UpStream::instance()->get_container(); |
| 291 |
$ext_options = new UpStream_Options_Extensions( $container ); |
| 292 |
$this->option_metabox[] = $ext_options->get_options(); |
| 293 |
|
| 294 |
return apply_filters( 'upstream_option_metaboxes', $this->option_metabox ); |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* Public getter method for retrieving protected/private variables |
| 299 |
* |
| 300 |
* @since 0.1.0 |
| 301 |
* |
| 302 |
* @param string $field Field to retrieve. |
| 303 |
* @throws Exception Exception. |
| 304 |
* @return mixed Field value or exception is thrown. |
| 305 |
*/ |
| 306 |
public function __get( $field ) { |
| 307 |
// Allowed fields to retrieve. |
| 308 |
if ( in_array( $field, array( 'key', 'fields', 'title', 'options_pages' ), true ) ) { |
| 309 |
return $this->{$field}; |
| 310 |
} |
| 311 |
if ( 'option_metabox' === $field ) { |
| 312 |
return $this->option_fields(); |
| 313 |
} |
| 314 |
|
| 315 |
throw new Exception( 'Invalid property: ' . $field ); |
| 316 |
} |
| 317 |
|
| 318 |
/** |
| 319 |
* Get a list of user roles. |
| 320 |
* |
| 321 |
* @return array |
| 322 |
*/ |
| 323 |
protected function get_roles() { |
| 324 |
$list = array(); |
| 325 |
$roles = get_editable_roles(); |
| 326 |
|
| 327 |
foreach ( $roles as $role => $data ) { |
| 328 |
$list[ $role ] = $data['name']; |
| 329 |
} |
| 330 |
|
| 331 |
return $list; |
| 332 |
} |
| 333 |
|
| 334 |
/** |
| 335 |
* Filter Cmb2 Set Options |
| 336 |
* |
| 337 |
* @param string $key Key. |
| 338 |
* @param array $options Options. |
| 339 |
*/ |
| 340 |
public function filter_cmb2_set_options( $key, $options ) { |
| 341 |
if ( 'upstream_general' === $key ) { |
| 342 |
// For "Who can post images in comments", update capabilities based on the selected roles. |
| 343 |
$selected = $options['media_comment_images']; |
| 344 |
|
| 345 |
$roles = $this->get_roles(); |
| 346 |
|
| 347 |
foreach ( $roles as $index => $role_name ) { |
| 348 |
$role = get_role( $index ); |
| 349 |
|
| 350 |
if ( in_array( $index, $selected ) ) { |
| 351 |
$role->add_cap( 'upstream_comment_images' ); |
| 352 |
} else { |
| 353 |
$role->remove_cap( 'upstream_comment_images' ); |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
return $options; |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
/** |
| 363 |
* Helper function to get/return the UpStream_Admin_Options object |
| 364 |
* |
| 365 |
* @since 0.1.0 |
| 366 |
* @return UpStream_Admin_Options object |
| 367 |
*/ |
| 368 |
function upstream_admin_options() { |
| 369 |
return UpStream_Admin_Options::get_instance(); |
| 370 |
} |
| 371 |
|
| 372 |
// Get it started. |
| 373 |
upstream_admin_options(); |
| 374 |
|
| 375 |
endif; |
| 376 |
|