' . tpl_kses( $msg ) . '
';
}
// Or as an admin notice
else {
add_action( 'admin_notices', function() use ( $msg, $class, $entity ) {
if ( $class == 'error' ) {
$msg = '' . tpl_kses( $tpl_sections[$arg["id"]]["description"] ) . '
';
}
// Section callback function
function tpl_section_name ( $section_slug ) {
global $tpl_sections;
if ( !tpl_section_registered ( $section_slug ) ) {
return $section_slug;
}
return $tpl_sections[$section_slug]["title"];
}
// Returns true if $section_name is connected to $post_type
function tpl_has_section_post_type ( $section_name, $post_type ) {
global $tpl_sections;
if ( isset( $tpl_sections[$section_name] ) ) {
if ( is_array( $tpl_sections[$section_name]["post_type"] ) ) {
if ( in_array( $post_type, $tpl_sections[$section_name]["post_type"] ) ) {
return true;
}
}
else {
if ( $post_type == $tpl_sections[$section_name]["post_type"] ) {
return true;
}
}
}
return false;
}
// This function puts an option input field on the settings page
function tpl_settings_page_callback ( $args ) {
global $tpl_options_array;
$name = $args["name"];
$type = $args["type"];
if ( $type == "" ) {
tpl_error (
sprintf( __( 'No data type was set up for option: %s', 'tpl' ),
esc_html( $name )
) );
}
elseif ( !tpl_type_registered ( $type ) ) {
tpl_error (
sprintf( __( 'Invalid data type (%1$s) was set for option: %2$s', 'tpl' ),
esc_html( $type ),
esc_html( $name )
) );
}
else {
$tpl_options_array[$name]->form_field();
if ( $tpl_options_array[$name]->repeat !== false && !isset( $tpl_options_array[$name]->repeat["number"] ) ) {
echo ''. tpl_kses( $args["description"] );
}
}
// Gets the unformatted values from wpdb
function tpl_get_option ( $name, $post_id = 0 ) {
global $tpl_options_array;
if ( is_array( $name ) ) {
$path = explode( '/', $name["name"] );
$name["name"] = $path[0];
$name["path"] = $path;
if ( isset ( $tpl_options_array[$name["name"]]->type ) ) {
return $tpl_options_array[$name["name"]]->get_option( $name, $post_id );
}
else {
return '';
}
}
else {
$path = explode( '/', $name );
$name = $path[0];
if ( isset ( $tpl_options_array[$name]->type ) ) {
return $tpl_options_array[$name]->get_option( array( 'path' => $path ), $post_id );
}
else {
return '';
}
}
}
// Gets the formatted values of an option
function tpl_get_value ( $name, $post_id = 0 ) {
global $tpl_options_array;
if ( is_array( $name ) ) {
$path = explode( '/', $name["name"] );
$name["name"] = $path[0];
$name["path"] = $path;
if ( isset ( $tpl_options_array[$name["name"]]->type ) ) {
return $tpl_options_array[$name["name"]]->get_value( $name, $post_id );
}
else {
return '';
}
}
else {
$path = explode( '/', $name );
$name = $path[0];
if ( isset ( $tpl_options_array[$name]->type ) ) {
return $tpl_options_array[$name]->get_value( array( 'path' => $path ), $post_id );
}
else {
return '';
}
}
}
// Prints the values of an option
function tpl_value ( $name, $post_id = 0 ) {
global $tpl_options_array;
if ( is_array( $name ) ) {
$path = explode( '/', $name["name"] );
$name["name"] = $path[0];
$name["path"] = $path;
if ( isset ( $tpl_options_array[$name["name"]]->type ) ) {
return $tpl_options_array[$name["name"]]->value( $name, $post_id );
}
else {
return '';
}
}
else {
$path = explode( '/', $name );
$name = $path[0];
if ( isset ( $tpl_options_array[$name]->type ) ) {
return $tpl_options_array[$name]->value( array( 'path' => $path ), $post_id );
}
else {
return '';
}
}
}
// Gets the full option object for more advanced use. Devs can reach the full spectrum of data type functions with this function.
function tpl_get_option_object ( $name, $post_id = 0 ) {
global $tpl_options_array;
if ( is_array( $name ) ) {
$path = explode( '/', $name["name"] );
$name["name"] = $path[0];
$name["path"] = $path;
if ( isset ( $tpl_options_array[$name["name"]]->type ) ) {
return $tpl_options_array[$name["name"]]->get_object( $name, $post_id );
}
else {
return '';
}
}
else {
$path = explode( '/', $name );
$name = $path[0];
if ( isset( $tpl_options_array[$name] ) && isset ( $tpl_options_array[$name]->type ) ) {
return $tpl_options_array[$name]->get_object( array( 'path' => $path ), $post_id );
}
else {
return '';
}
}
}
// Checks if this is primary (Plugin Settings, etc.) or secondary (e.g. Post Metabox) section
function tpl_is_primary_section ( $section ) {
global $tpl_sections, $tpl_settings_pages;
if ( isset( $tpl_sections[$section]["post_type"] ) && !is_array( $tpl_sections[$section]["post_type"] ) ) {
foreach ( $tpl_settings_pages as $key => $settings_page ) {
if ( $settings_page["post_type"] == $tpl_sections[$section]["post_type"] ) {
return true;
}
}
}
if ( !isset( $tpl_sections[$section]["post_type"] ) || $tpl_sections[$section]["post_type"] == '' ) {
return true;
}
return false;
}
// This is the modified version of WP's do_settings_sections that allows us to use jQuery UI tabs in the settings pages.
function tpl_settings_sections ( $page ) {
global $wp_settings_sections, $wp_settings_fields;
if ( !isset( $wp_settings_sections[$page] ) ) {
return;
}
foreach ( (array) $wp_settings_sections[$page] as $section ) {
if ( tpl_is_primary_section ( $section["id"] ) == true ) {
echo '';
if ( $section["title"] ) {
echo ' ' . esc_html( $section["title"] ) . '';
}
if ( $section["callback"] ) {
call_user_func( $section["callback"], $section );
}
if ( ! isset( $wp_settings_fields ) || !isset( $wp_settings_fields[$page] ) || !isset( $wp_settings_fields[$page][$section["id"]] ) ) {
continue;
}
echo '
';
}
}
}
// General function for creating TPL settings pages in the back end
function tpl_settings_page ( $settings_page ) {
global $tpl_sections, $tpl_settings_pages;
// Make the base structure of the Settings page
echo '
' . esc_html( $tpl_settings_pages[$settings_page]["page_title"] ) . '
';
do_action( 'tpl_after_primary_sections' );
}
// Adds the post metaboxes from the registered sections
function tpl_add_custom_box ( $post_type ) {
$sections = tpl_get_sections ( $post_type );
foreach ( $sections as $section ) {
add_meta_box(
$section["name"],
$section["title"],
'tpl_inner_custom_box',
$post_type,
'normal',
'low',
array ( "section" => $section["name"], "description" => $section["description"] )
);
}
}
// Display contents of custom metabox for post types
function tpl_inner_custom_box ( $post, $metabox ) {
// Add an nonce field so we can check for it later.
wp_nonce_field ( 'tpl_inner_custom_box', 'tpl_inner_custom_box_nonce' );
if ( $metabox["args"]["description"] != '' ) {
echo tpl_kses( $metabox["args"]["description"] );
}
// Selects all options from $tpl_options_array which are in the current section
$options = tpl_options_by_section ( $metabox["args"]["section"] );
foreach ( $options as $option ) {
$meta_key = '_tpl_' . $option->get_data_section();
if ( get_post_meta ( $post->ID, $meta_key ) == "" ) {
$values = array();
}
else {
$values = get_post_meta ( $post->ID, $meta_key );
}
if ( !isset ( $values[0][$option->name] ) ) {
$values[0][$option->name] = "";
}
$data_connected = '';
if ( $option->condition_connected != '' ) {
$data_connected = ' data-connected="' . esc_attr( $option->condition_connected ) . '"';
}
echo ''. tpl_kses( $option->description ) .' ';
}
echo '';
}
// Saves post meta into the database
function tpl_save_postdata( $post_id ) {
// Check if our nonce is set.
if ( ! isset( $_POST['tpl_inner_custom_box_nonce'] ) ) {
return $post_id;
}
$nonce = $_POST['tpl_inner_custom_box_nonce'];
// Verify that the nonce is valid.
if ( !wp_verify_nonce ( $nonce, 'tpl_inner_custom_box' ) ) {
return $post_id;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
// Check the user's permissions.
if ( 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return $post_id;
}
}
else {
if ( !current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
}
/* OK, its safe for us to save the data now. */
// Update the meta field in the database.
$sections = tpl_get_sections ( get_post_type ( $post_id ) );
foreach ( $sections as $section ) {
$options = tpl_options_by_section ( $section["name"] );
foreach ( $options as $option ) {
$sn = $option->form_ref();
$sn = explode( '[', $sn );
$sn = $sn[0];
if ( $option->repeat == false ) {
$data = $_POST[$sn][0];
}
else {
$data = $_POST[$sn];
}
update_post_meta( $post_id, '_tpl_' . $option->name, $data );
}
}
}
// Build up a bank of repeater fields in the footer to help repeater fields work faster
function tpl_repeater_bank() {
global $tpl_options_array;
$screen = get_current_screen();
$post_type = str_replace( array( 'appearance_page_', 'settings_page_', 'tpl_' ), '', $screen->id );
echo '';
foreach ( $tpl_options_array as $option ) {
if ( tpl_has_section_post_type ( $option->section, $post_type ) ) {
$option->form_field( array( "for_bank" => true ) );
}
}
echo ' ';
}
/*
SCRIPT HANDLING
*/
// Load the scripts needed in TPL Admin
function tpl_admin_scripts() {
// Scripts
wp_enqueue_script( 'jquery-ui-tabs', '', array( 'jquery', 'jquery-ui-core' ) );
wp_enqueue_script( 'tpl-admin-scripts', tpl_base_uri() . '/framework/script/admin-scripts.min.js', array( 'jquery', 'jquery-ui-tabs' ), TPL_VERSION );
// Variables to be used in scripts
wp_localize_script( 'tpl-admin-scripts', 'TPL_Admin', array_merge( apply_filters( 'tpl_admin_js_strings', array() ), tpl_admin_vars_to_js() ) );
// Styles
wp_enqueue_style( 'font-awesome', tpl_base_uri() . '/framework/lib/font-awesome/fonts/font-awesome.min.css', array(), TPL_VERSION );
wp_enqueue_style( 'tpl-admin-style', tpl_base_uri() . '/framework/style/admin.css', array( 'font-awesome' ), TPL_VERSION );
}
// JS vars, admin version
function tpl_admin_vars_to_js() {
global $tpl_options_array;
$to_js = array();
if ( is_admin() ) {
$screen = get_current_screen();
$post_type = str_replace( array( 'appearance_page_', 'settings_page_', 'tpl_' ), '', $screen->id );
}
foreach ( $tpl_options_array as $option ) {
if ( tpl_has_section_post_type ( $option->section, "framework_options" ) ) {
if ( isset ( $option->js ) && ( $option->js == true ) ) {
$func_name = $option->js_func;
$to_js[$option->name] = $option->$func_name();
}
}
// Add the conditional options as they will be used in admin
if ( $option->get_conditions() !== false ) {
if ( tpl_has_section_post_type ( $option->section, $post_type ) ) {
foreach ( $option->get_conditions() as $key => $value ) {
$to_js["Conditions"][$key] = $value;
}
}
}
}
return $to_js;
}
/*
DEALING WITH IMAGE SIZES
*/
// Sets up added image sizes
function tpl_images_setup () {
$image_sizes = apply_filters( 'tpl_image_sizes', array() );
// Registering new image sizes
if ( !empty( $image_sizes ) ) {
// Add extra image sizes
foreach ( $image_sizes as $name => $image_size ) {
add_image_size( $name, $image_size["width"], $image_size["height"], $image_size["crop"] );
}
}
}
add_action ( 'after_setup_theme', 'tpl_images_setup', 20 );
// If you defined the "select" attribute of an image size as TRUE in $tpl_image_sizes, this function will add it to the image size selector menu in the post editor
function tpl_image_selector_sizes( $sizes ) {
$image_sizes = apply_filters( 'tpl_image_sizes', array() );
$addsizes = array();
foreach ( $image_sizes as $name => $image_size ) {
if ( $image_size["select"] == true ) {
$addsizes[$name] = $image_size["title"];
}
}
$newsizes = array_merge( $sizes, $addsizes );
return $newsizes;
}
add_filter( 'image_size_names_choose', 'tpl_image_selector_sizes' );
|