"$protocol://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.3/highlight.min.js", ); $css = array( 'highlight' => "$protocol://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.3/styles/default.min.css", ); foreach ( $js as $handle => $src ) { wp_enqueue_script( $handle, $src ); } foreach ( $css as $handle => $src ) { wp_enqueue_style( $handle, $src ); } } add_action( 'admin_init', 'paf_enqueue' ); /** * Load important gobal data * * The data is: * - paf * - paf_page_tabs * - paf_page_options * - paf_page_sections * - paf_page_shortcodes * - paf_page * - paf_tab */ function paf_load() { global $paf; $paf = get_option( 'paf', array() ); global $paf_options, $paf_pages, $paf_sections, $paf_shortcodes, $paf_tabs; // Make sure $GLOBALS[ 'paf_...' ] exist foreach ( array( 'paf_options', 'paf_pages', 'paf_sections', 'paf_shortcodes', 'paf_tabs' ) as $k ) { if( empty( $GLOBALS[ $k ] ) ) { $GLOBALS[ $k ] = array(); } } global $paf_page_tabs, $paf_page_options, $paf_page_sections; $paf_page_tabs = $paf_page_options = $paf_page_sections = array(); global $paf_page, $paf_tab; $paf_page = K::get_var( 'page', $_GET ); $paf_tab = K::get_var( 'tab', $_GET ); // Get defined page tabs foreach ( $paf_tabs as $slug => $page_tab ) { if( $paf_page === $paf_tabs[ $slug ][ 'page' ] ) { $paf_page_tabs[ $slug ] = $page_tab; } } /** * Use the first tab: * - if page has tabs and none is specified in $_GET * - or if the specified tab doesn't exist */ if( ( $paf_page_tabs && ! $paf_tab ) || ( $paf_page_tabs && $paf_tab && ! K::get_var( $paf_tab, $paf_page_tabs ) ) ) { reset( $paf_page_tabs ); $paf_tab = key( $paf_page_tabs ); } // If the page has a tab, force tab-less options to use the default tab reset( $paf_options ); foreach ( $paf_options as $id => $paf_option ) { if( $paf_page === K::get_var( 'page', $paf_option ) ) { if( $paf_tab && ! K::get_var( 'tab', $paf_option ) ) { $paf_options[ $id ][ 'tab' ] = key( $paf_page_tabs ); } } } // Get defined page and tab options reset( $paf_options ); foreach ( $paf_options as $id => $paf_option ) { if( $paf_page === K::get_var( 'page', $paf_option ) ) { if( ! $paf_tab || ( $paf_tab === $paf_option[ 'tab' ] ) ) { $paf_page_options[ $id ] = $paf_option; } } } // Get defined page and tab sections reset( $paf_page_options ); foreach ( $paf_page_options as $id => $paf_option ) { if ( K::get_var( 'section', $paf_option ) ) { $paf_page_sections[ $paf_option[ 'section' ] ] = K::get_var( $paf_option[ 'section' ], $paf_sections, array() ); } } } add_action( 'admin_init', 'paf_load' ); /** * Save options * * The function will preserve options saved on other pages */ function paf_save() { global $paf_page_options; // Abort saving if the nonce is not valid if ( ! wp_verify_nonce( K::get_var( 'paf_nonce', $_POST ), 'paf_save' ) ) { return; } // Force select and radio to have a value to prevent skipping empty $_POST[ 'paf' ] = K::get_var( 'paf', $_POST, array() ); foreach ( $paf_page_options as $option_id => $option_def ) { $option_type = K::get_var( 'type', $option_def, 'text' ); switch ( $option_type ) { case 'radio': $_POST['paf'][ $option_id ] = K::get_var( $option_id, $_POST['paf'], '' ); case 'checkbox': case 'select': $_POST['paf'][ $option_id ] = K::get_var( $option_id, $_POST['paf'], array() ); break; } } // Combine old and saved options $paf = get_option( 'paf ', array() ); $paf = array_merge( $paf, $_POST[ 'paf' ] ); // Save delete_option( 'paf' ); add_option( 'paf', $paf, '', TRUE ); // Bind showing the success message add_action( 'admin_notices', 'paf_notice' ); } add_action( 'admin_init', 'paf_save' ); /** * Add $pages to the global $paf_pages */ function paf_pages( $pages ) { $GLOBALS[ 'paf_pages'] = array_merge( K::get_var( 'paf_pages', $GLOBALS, array() ) , $pages ); } /** * Add $options to the global $paf_options */ function paf_options( $options ) { $GLOBALS[ 'paf_options'] = array_merge( K::get_var( 'paf_options', $GLOBALS, array() ) , $options ); } /** * Add $tabs to the global $paf_tabs */ function paf_tabs( $tabs ) { $GLOBALS[ 'paf_tabs'] = array_merge( K::get_var( 'paf_tabs', $GLOBALS, array() ) , $tabs ); // ksort( $GLOBALS[ 'paf_tabs' ] ); } /** * Add $sections to the global $paf_sections */ function paf_sections( $sections ) { $GLOBALS[ 'paf_sections'] = array_merge( K::get_var( 'paf_sections', $GLOBALS, array() ) , $sections ); // ksort( $GLOBALS[ 'paf_sections' ] ); } /** * Add $sections to the global $paf_sections */ function paf_shortcodes( $shortcodes ) { $GLOBALS[ 'paf_shortcodes' ] = array_merge( K::get_var( 'paf_shortcodes', $GLOBALS, array() ) , $shortcodes ); // ksort( $GLOBALS[ 'paf_shortcodes' ] ); } /** * Show message when options are saved successfully * * Seeks in this order: tab > page > default */ function paf_notice() { global $paf_pages, $paf_tabs, $paf_page, $paf_tab; // Look in tab configuration if ( $paf_tab && $message = K::get_var( 'success', $paf_tabs[ $paf_tab ] ) ) {} // Look in page configuration else if ( $message = K::get_var( 'success', $paf_pages[ $paf_page ] ) ) {} // Use default else { $message = __( 'Settings saved.'); } printf( '

%s

' , $message ); } /** * Adds some JS in the header: * - paf_assets, here paf assets can be found */ function paf_header() { $home_path = get_home_path(); $assets_path = str_replace( array( $home_path, 'core/core.php' ) , array( '', 'assets/' ) , __FILE__ ); $assets_dir_url = home_url( $assets_path ); ?> run( $o ); case 'js': $o = JSMin::minify( $o ); } // Wrap in tags $o = sprintf( '<%s>%s' , 'css' === $type ? 'style' : 'script' , $o , 'css' === $type ? 'style' : 'script' ); // Output print( $o ); } function paf_url() { return 'http' . ( is_ssl() ? 's' : '' ) . '://' . $_SERVER[ 'SERVER_NAME' ] . ( 80 != $_SERVER[ 'SERVER_PORT' ] ? ":$_SERVER[SERVER_PORT]" : '' ) . $_SERVER[ 'REQUEST_URI' ] ; } function paf_htmlspecialchars_recursive( &$array ) { $array = htmlspecialchars( $array ); } function skelet_dir( $dir ) { foreach ( array( 'pages', 'tabs', 'sections', 'options', 'shortcodes' ) as $option_file_name ) { $option_file_path = $dir . '/' . $option_file_name . '.php'; if ( file_exists( $option_file_path ) ) { require $option_file_path; } } }