| 1 |
<?php |
| 2 |
class MooWoodle_Settings { |
| 3 |
|
| 4 |
private $tabs = array(); |
| 5 |
private $options; |
| 6 |
public $report; |
| 7 |
|
| 8 |
/* |
| 9 |
* Start up |
| 10 |
*/ |
| 11 |
public function __construct() { |
| 12 |
//Admin menu |
| 13 |
add_action( 'admin_menu', array( $this, 'add_settings_page' ), 100 ); |
| 14 |
add_action( 'admin_init', array( $this, 'settings_page_init' ) ); |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Add Option page |
| 19 |
*/ |
| 20 |
public function add_settings_page() { |
| 21 |
global $MooWoodle; |
| 22 |
add_menu_page( |
| 23 |
"MooWoodle", |
| 24 |
"MooWoodle", |
| 25 |
'manage_options', |
| 26 |
'moowoodle', |
| 27 |
array( $this, 'option_page' ), |
| 28 |
esc_url( $MooWoodle->plugin_url ) . 'assets/images/moowoodle.png', |
| 29 |
50 |
| 30 |
); |
| 31 |
|
| 32 |
add_submenu_page( |
| 33 |
'moowoodle', |
| 34 |
__( "Courses", 'moowoodle' ), |
| 35 |
__( "Courses", 'moowoodle' ), |
| 36 |
'manage_options', |
| 37 |
'moowoodle', |
| 38 |
array( $this, 'option_page' ) |
| 39 |
); |
| 40 |
|
| 41 |
add_submenu_page( |
| 42 |
'moowoodle', |
| 43 |
__( "Settings", 'moowoodle' ), |
| 44 |
__( "Settings", 'moowoodle' ), |
| 45 |
'manage_options', |
| 46 |
'moowoodle-settings', |
| 47 |
array( $this, 'option_page' ) |
| 48 |
); |
| 49 |
|
| 50 |
add_submenu_page( |
| 51 |
'moowoodle', |
| 52 |
__( "Synchronization", 'moowoodle' ), |
| 53 |
__( "Synchronization", 'moowoodle' ), |
| 54 |
'manage_options', |
| 55 |
'moowoodle-synchronization', |
| 56 |
array( $this, 'option_page' ) |
| 57 |
); |
| 58 |
|
| 59 |
if ( apply_filters( 'moowoodle_menu_hide', true ) ) { |
| 60 |
add_submenu_page( |
| 61 |
'moowoodle', |
| 62 |
__( "Upgrade to Pro", 'moowoodle' ), |
| 63 |
'<span class="dashicons dashicons dashicons-awards" style="font-size: 17px"></span> ' . esc_html__( "Upgrade to Pro", 'moowoodle' ), |
| 64 |
'manage_options', |
| 65 |
'', |
| 66 |
array( $this, 'handle_external_redirects' ) |
| 67 |
); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
// Upgrade to pro link |
| 72 |
public function handle_external_redirects() { |
| 73 |
wp_redirect( esc_url( 'https://dualcube.com/shop/' ) ); |
| 74 |
die; |
| 75 |
} |
| 76 |
|
| 77 |
public function option_page() { |
| 78 |
global $MooWoodle; |
| 79 |
$menu_slug = null; |
| 80 |
$page = $_REQUEST[ 'page' ]; |
| 81 |
$layout = $this->moowoodle_get_page_layout(); ?> |
| 82 |
<div class=""> |
| 83 |
<?php $this->moowoodle_plugin_options_tabs(); ?> |
| 84 |
<div class="moowoodle-space"> |
| 85 |
<?php if ( $layout == '2-col' ): ?> |
| 86 |
<div id="poststuff"> |
| 87 |
<div id="post-body" class="metabox-holder columns-2"> |
| 88 |
<div id="post-body-content"> |
| 89 |
<?php endif; ?> |
| 90 |
<form action="options.php" method="post"> |
| 91 |
<?php |
| 92 |
$show_submit = false; |
| 93 |
foreach ( $MooWoodle->library->moowoodle_get_options() as $v ) { |
| 94 |
if ( isset( $v[ 'menu_slug' ] ) ) { |
| 95 |
$menu_slug = $v[ 'menu_slug' ]; |
| 96 |
} |
| 97 |
if ( $menu_slug == $page ) { |
| 98 |
switch ( $v[ 'type' ] ) { |
| 99 |
case 'menu': |
| 100 |
break; |
| 101 |
case 'tab': |
| 102 |
$tab = $v; |
| 103 |
if ( empty( $default_tab ) ) { |
| 104 |
$default_tab = $v[ 'id' ]; |
| 105 |
} |
| 106 |
break; |
| 107 |
case 'setting': |
| 108 |
$current_tab = isset( $_GET[ 'tab' ] ) ? $_GET[ 'tab' ] : $default_tab; |
| 109 |
if ( $current_tab == $tab[ 'id' ] ) { |
| 110 |
settings_fields( $v[ 'id' ] ); |
| 111 |
$show_submit = true; |
| 112 |
} |
| 113 |
break; |
| 114 |
case 'section': |
| 115 |
$current_tab = isset( $_GET[ 'tab' ] ) ? $_GET[ 'tab' ] : $default_tab; |
| 116 |
if ( $current_tab == $tab[ 'id' ] or $current_tab === false ) { |
| 117 |
if ( $layout == '2-col' ) { |
| 118 |
echo '<div id="' . esc_attr( $v[ 'id' ] ) . '" class="postbox">'; |
| 119 |
$this->moowoodle_do_settings_sections( $v[ 'id' ], $show_submit ); |
| 120 |
echo '</div>'; |
| 121 |
} else { |
| 122 |
$this->moowoodle_do_settings_sections( $v[ 'id' ] ); |
| 123 |
} |
| 124 |
} |
| 125 |
break; |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
?> |
| 130 |
</form> |
| 131 |
|
| 132 |
<?php if ( $layout == '2-col' ): ?> |
| 133 |
</div> <!-- #post-body-content --> |
| 134 |
<div id="postbox-container-1" class="postbox-container"> |
| 135 |
<div id="side-sortables" class="meta-box-sortables ui-sortable"> |
| 136 |
<?php |
| 137 |
if ( apply_filters( 'moowoodle_free_active_side_adv', true ) ) { |
| 138 |
?> |
| 139 |
<a class="image-adv"> |
| 140 |
<img src="<?php echo esc_url( plugins_url() ) ?>/moowoodle/framework/coming-soon-pro-sidebar.jpg" /> |
| 141 |
</a> |
| 142 |
<br><br> |
| 143 |
<div class="postbox "> |
| 144 |
<div class="inside"> |
| 145 |
<div class="support-widget"> |
| 146 |
<p class="supt-link"> |
| 147 |
<a href="https://wordpress.org/support/plugin/moowoodle/" target="_blank"> |
| 148 |
<?php esc_html_e( 'Got a Support Question', 'moowoodle' ) ?> |
| 149 |
</a> |
| 150 |
<i class="fas fa-question-circle"></i> |
| 151 |
</p> |
| 152 |
</div> |
| 153 |
</div> |
| 154 |
</div> |
| 155 |
<?php |
| 156 |
} |
| 157 |
|
| 158 |
// Additional banner for pro version |
| 159 |
do_action( 'moowoodle_additional_banner' ); |
| 160 |
?> |
| 161 |
</div> |
| 162 |
</div> |
| 163 |
</div> <!-- #post-body --> |
| 164 |
</div> <!-- #poststuff --> |
| 165 |
<?php endif; ?> |
| 166 |
</div> <!-- .wrap --> |
| 167 |
</div> |
| 168 |
|
| 169 |
<?php |
| 170 |
// MooWoodle admin footer |
| 171 |
do_action( 'moowoodle_admin_footer' ); |
| 172 |
} |
| 173 |
|
| 174 |
public function moowoodle_get_page_layout() { |
| 175 |
global $MooWoodle; |
| 176 |
$layout = 'classic'; |
| 177 |
foreach ( $MooWoodle->library->moowoodle_get_options() as $v ) { |
| 178 |
switch ( $v[ 'type' ] ) { |
| 179 |
case 'menu': |
| 180 |
$page = $_REQUEST[ 'page' ]; |
| 181 |
if ( $page == $v[ 'menu_slug' ] ) { |
| 182 |
if ( isset($v[ 'layout' ] ) ) { |
| 183 |
$layout = $v[ 'layout' ]; |
| 184 |
} |
| 185 |
} |
| 186 |
break; |
| 187 |
} |
| 188 |
} |
| 189 |
return $layout; |
| 190 |
} |
| 191 |
|
| 192 |
public function moowoodle_plugin_options_tabs() { |
| 193 |
global $MooWoodle; |
| 194 |
$menu_slug = null; |
| 195 |
$page = $_REQUEST[ 'page' ]; |
| 196 |
$uses_tabs = false; |
| 197 |
$current_tab = isset ( $_GET[ 'tab' ] ) ? $_GET[ 'tab' ] : false; |
| 198 |
|
| 199 |
//Check if this config uses tabs |
| 200 |
foreach ( $MooWoodle->library->moowoodle_get_options() as $v ) { |
| 201 |
if ( $v[ 'type' ] == 'tab' ) { |
| 202 |
$uses_tabs = true; |
| 203 |
break; |
| 204 |
} |
| 205 |
} |
| 206 |
// If uses tabs then generate the tabs |
| 207 |
if ( $uses_tabs ) { |
| 208 |
echo '<h2 class="nav-tab-wrapper">'; |
| 209 |
$c = 1; |
| 210 |
foreach ( $MooWoodle->library->moowoodle_get_options() as $v ) { |
| 211 |
if ( isset( $v[ 'menu_slug' ] ) ) { |
| 212 |
$menu_slug = $v[ 'menu_slug' ]; |
| 213 |
} |
| 214 |
if ( $menu_slug == $page && $v[ 'type' ] == 'tab' ) { |
| 215 |
$active = ''; |
| 216 |
if ( $current_tab ) { |
| 217 |
$active = $current_tab == $v[ 'id' ] ? 'nav-tab-active' : ''; |
| 218 |
} elseif ( $c == 1 ) { |
| 219 |
$active = 'nav-tab-active'; |
| 220 |
} |
| 221 |
if ( $v[ 'id' ] == 'moowoodle-from' ) { |
| 222 |
echo '<a id="' . esc_attr( $v[ 'id' ] ) . '" class="nav-tab ' . $active . '" href="admin.php?moowoodle&tab=moowoodle-from">'; |
| 223 |
} else { |
| 224 |
echo '<a id="' . esc_attr( $v[ 'id' ] ) . '" class="nav-tab ' . $active . '" href="?page=' . $menu_slug . '&tab=' . $v[ 'id' ] . '">'; |
| 225 |
} |
| 226 |
if ( isset( $v[ 'font_class' ] ) ) { |
| 227 |
echo '<i class="dashicons ' . esc_attr( $v[ 'font_class' ] ) . '"></i> '; |
| 228 |
} |
| 229 |
|
| 230 |
// Add extra tab for pro version |
| 231 |
do_action( 'moowoodle_add_additional_tabs', $v ); |
| 232 |
echo esc_html( $v[ 'label' ] ) . '</a>'; |
| 233 |
$c++; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
// For free version only |
| 238 |
if ( apply_filters( 'moowoodle_free_active', true ) ) { |
| 239 |
echo '<a class="nav-tab moowoodle-upgrade" href="https://dualcube.com/shop/" target="_blank" rel="noopener noreferrer"><i class="dashicons dashicons-awards"></i> ' . esc_html__('Upgrade to Pro for More Features', 'moowoodle') . '</a>'; |
| 240 |
} |
| 241 |
|
| 242 |
// Add extra tab for pro version |
| 243 |
do_action( 'moowoodle_added_extra_tab_after', $v ); |
| 244 |
echo '</h2>'; |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
public function moowoodle_do_settings_sections( $page, $show_submit ) { |
| 249 |
global $wp_settings_sections, $wp_settings_fields; |
| 250 |
if ( ! isset( $wp_settings_sections ) || ! isset( $wp_settings_sections[ $page ] ) ) { |
| 251 |
return; |
| 252 |
} |
| 253 |
foreach ( (array) $wp_settings_sections[ $page ] as $section ) { |
| 254 |
echo '<div class="postbox-header">'; |
| 255 |
echo "<h3 class='hndle'>{$section['title']}</h3>\n"; |
| 256 |
echo '</div>'; |
| 257 |
echo '<div class="inside">'; |
| 258 |
if ( ! isset( $wp_settings_fields ) || ! isset( $wp_settings_fields[ $page ] ) || ! isset( $wp_settings_fields[ $page ][ $section[ 'id' ] ] ) ) { |
| 259 |
continue; |
| 260 |
} |
| 261 |
echo '<table class="form-table">'; |
| 262 |
$this->moowoodle_do_settings_fields( $page, $section[ 'id' ] ); |
| 263 |
echo '</table>'; |
| 264 |
if ( $show_submit ): ?> |
| 265 |
<p> |
| 266 |
<?php |
| 267 |
if ( $page == "moowoodle-sync-products" || $page == "moowoodle-sync-courses" ){ |
| 268 |
?> |
| 269 |
<input name="syncnow" type="submit" value="<?php esc_html_e( 'Sync Now', 'moowoodle' ); ?>" class="button-primary" /> |
| 270 |
<?php |
| 271 |
} elseif ( $page == "moowoodle-link-course-table" ) { |
| 272 |
echo esc_html_e( "Cannot find your course in this list?", 'moowoodle' ); |
| 273 |
?> |
| 274 |
<a href="<?php echo esc_url( get_site_url() ); ?>/wp-admin/admin.php?page=moowoodle-synchronization" ><?php esc_html_e('Synchronize Moodle Courses from here.', 'moowoodle'); ?></a> |
| 275 |
|
| 276 |
<?php |
| 277 |
} else { |
| 278 |
?> |
| 279 |
<input name="submit" type="submit" value="<?php esc_html_e('Save All Changes', 'moowoodle'); ?>" class="button-primary" /> |
| 280 |
<?php |
| 281 |
} |
| 282 |
?> |
| 283 |
</p> |
| 284 |
<?php endif; |
| 285 |
echo '</div>'; |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
function moowoodle_do_settings_fields( $page, $section ) { |
| 290 |
global $wp_settings_fields; |
| 291 |
|
| 292 |
if ( ! isset( $wp_settings_fields ) || ! isset( $wp_settings_fields[ $page ] ) || ! isset( $wp_settings_fields[ $page ][ $section ] ) ) { |
| 293 |
return; |
| 294 |
} |
| 295 |
foreach ( (array) $wp_settings_fields[ $page ][ $section ] as $field) { |
| 296 |
echo '<tr valign="top">'; |
| 297 |
if ( ! empty( $field[ 'args' ][ 'label_for' ] ) ) { |
| 298 |
echo '<th scope="row"><label for="' . esc_attr( $field[ 'args' ][ 'label_for' ] ) . '">' . $field[ 'title' ] . '</label></th>'; |
| 299 |
} else { |
| 300 |
echo '<th scope="row" class="' . esc_attr( $field[ 'id' ] ) . '"><strong>' . $field[ 'title' ] . '</strong><!--<br>' . $field[ 'args' ][ 'desc' ] . '--></th>'; |
| 301 |
} |
| 302 |
echo '<td>'; |
| 303 |
call_user_func( $field[ 'callback' ], $field[ 'args' ]); |
| 304 |
echo '</td>'; |
| 305 |
echo '</tr>'; |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* Register and add settings |
| 311 |
*/ |
| 312 |
public function settings_page_init() { |
| 313 |
global $MooWoodle; |
| 314 |
foreach ( $MooWoodle->library->moowoodle_get_options() as $k => $v ) { |
| 315 |
switch ( $v[ 'type' ] ) { |
| 316 |
case 'menu': |
| 317 |
$menu_slug = $v[ 'menu_slug' ]; |
| 318 |
break; |
| 319 |
case 'setting': |
| 320 |
if ( empty( $v[ 'validate_function' ] ) ) { |
| 321 |
$v[ 'validate_function' ] = array( |
| 322 |
&$this, |
| 323 |
'validate_machine' |
| 324 |
); |
| 325 |
} |
| 326 |
register_setting( $v[ 'id' ], $v[ 'id' ], $v[ 'validate_function' ] ); |
| 327 |
$setting_id = $v[ 'id' ]; |
| 328 |
break; |
| 329 |
case 'section': |
| 330 |
if ( empty( $v[ 'desc_callback' ] ) ) { |
| 331 |
$v[ 'desc_callback' ] = array( |
| 332 |
&$this, |
| 333 |
'return_empty_string' |
| 334 |
); |
| 335 |
} else { |
| 336 |
$v[ 'desc_callback' ] = $v[ 'desc_callback' ]; |
| 337 |
} |
| 338 |
add_settings_section( $v[ 'id' ], $v[ 'label' ], $v[ 'desc_callback' ], $v[ 'id' ] ); |
| 339 |
$section_id = $v[ 'id' ]; |
| 340 |
break; |
| 341 |
case 'tab': |
| 342 |
break; |
| 343 |
default: |
| 344 |
if ( empty( $v[ 'callback' ] ) ) { |
| 345 |
$v[ 'callback' ] = array( $this, 'field_machine' ); |
| 346 |
} |
| 347 |
|
| 348 |
add_settings_field( $v[ 'id' ], $v[ 'label' ], $v[ 'callback' ], $section_id, $section_id, |
| 349 |
apply_filters( 'moowoodle_add_settings_field', |
| 350 |
array( |
| 351 |
'id' => $v[ 'id' ], |
| 352 |
'name' => ( isset( $v[ 'name' ] ) ? $v[ 'name' ] : '' ), |
| 353 |
'desc' => ( isset( $v[ 'desc' ] ) ? $v[ 'desc' ] : '' ), |
| 354 |
'setting_id' => $setting_id, |
| 355 |
'class' => ( isset( $v[ 'class' ] ) ? $v[ 'class' ] : '' ), |
| 356 |
'type' => $v[ 'type' ], |
| 357 |
'default_value' => ( isset( $v[ 'default_value' ] ) ? $v[ 'default_value' ] : '' ), |
| 358 |
'option_values' => ( isset( $v[ 'option_values' ] ) ? $v[ 'option_values' ] : '' ), |
| 359 |
'extra_input' => ( isset( $v[ 'extra_input' ] ) ? $v[ 'extra_input' ] : '' ), |
| 360 |
'font_class' => ( isset( $v[ 'font_class' ] ) ? $v[ 'font_class' ] : '' ) |
| 361 |
), |
| 362 |
$v |
| 363 |
) |
| 364 |
); |
| 365 |
} |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
public function field_machine( $args ) { |
| 370 |
global $MooWoodle; |
| 371 |
extract($args); //$id, $desc, $setting_id, $class, $type, $default_value, $option_values |
| 372 |
// Load defaults |
| 373 |
$defaults = array( ); |
| 374 |
foreach ( $MooWoodle->library->moowoodle_get_options() as $k ) { |
| 375 |
switch ( $k[ 'type' ] ) { |
| 376 |
case 'setting': |
| 377 |
case 'section': |
| 378 |
case 'tab': |
| 379 |
break; |
| 380 |
default: |
| 381 |
if ( isset( $k[ 'default_value' ] ) ) { |
| 382 |
$defaults[ $k[ 'id' ] ] = $k[ 'default_value' ]; |
| 383 |
} |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
$options = get_option( $setting_id ); |
| 388 |
$options = wp_parse_args( $options, $defaults ); |
| 389 |
$path = $MooWoodle->plugin_path . 'framework/field-types/' . $type . '.php'; |
| 390 |
if ( file_exists( $path ) ) { |
| 391 |
// Show Field |
| 392 |
include( $path ); |
| 393 |
// Show description |
| 394 |
if ( ! empty( $desc ) ) { |
| 395 |
echo "<small class='description'>{$desc}</small>"; |
| 396 |
} |
| 397 |
} |
| 398 |
} |
| 399 |
} |