????
| Current Path : /home2/morganrand/www/wp-content/themes/wanderfuls-2/framework/addons/ |
| Current File : /home2/morganrand/www/wp-content/themes/wanderfuls-2/framework/addons/custom-actions.php |
<?php
/**
* User actions
*
* @package Wanderfuls WordPress Theme
* @subpackage Framework
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Start Class
if ( ! class_exists( 'TB_User_Actions' ) ) {
class TB_User_Actions {
private $options = array();
private $custom_actions;
/**
* Start things up
*
* @since 3.0.0
*/
public function __construct() {
// Update vars
$this->custom_actions = get_option( 'tb_custom_actions' );
// Actions
add_action( 'admin_menu', array( $this, 'add_page' ), 40 );
add_action( 'admin_init', array( $this,'register_settings' ) );
add_action( 'init', array( $this,'output' ) );
add_action( 'admin_print_styles-'. TB_ADMIN_PANEL_HOOK_PREFIX . '-user-actions', array( $this,'admin_css' ), 40 );
}
/**
* Add sub menu page
*
* @since 3.0.0
*/
public function add_page() {
$slug = TB_THEME_PANEL_SLUG;
add_submenu_page(
$slug,
__( 'Custom Actions', 'tb' ),
__( 'Custom Actions', 'tb' ),
'administrator',
$slug .'-user-actions',
array( $this, 'create_admin_page' )
);
}
/**
* Register a setting and its sanitization callback.
*
* @since 3.0.0
*/
public function register_settings() {
register_setting( 'tb_custom_actions', 'tb_custom_actions', array( $this, 'admin_sanitize' ) );
}
/**
* Main Sanitization callback
*
* @since 3.0.0
*/
public static function admin_sanitize( $options ) {
if ( ! empty( $options ) ) {
foreach ( $options as $key => $val ) {
if ( empty( $val['action'] ) ) {
unset( $options[$key] );
}
// Validate stuff
else {
if ( ! empty( $val['priority'] ) ) {
$options[$key]['priority'] = intval( $val['priority'] );
}
if ( isset( $val['php'] ) ) {
$options[$key]['php'] = true;
}
}
}
return $options;
}
}
/**
* Settings page output
*
* @since 3.0.0
*/
public function create_admin_page() { ?>
<div class="wrap tb-custom-actions-admin-wrap">
<h1 style="padding-right:0;"><?php _e( 'Custom Actions', 'tb' ); ?></h1>
<?php
// Error notice for WP_Debug
if ( ! defined( 'WP_DEBUG' ) || WP_DEBUG == false ) { ?>
<div class="notice error">
<p><?php _e( 'WP_DEBUG is disabled is currently disabled. It is highly recommended to enable it before adding action hooks incase you make a PHP error.', 'tb' ); ?></p>
</div>
<?php } ?>
<form method="post" action="options.php">
<?php settings_fields( 'tb_custom_actions' ); ?>
<?php $options = get_option( 'tb_custom_actions' ); ?>
<div id="poststuff" class="tb-custom-actions">
<div id="post-body" class="metabox-holder columns-2">
<div id="post-body-content">
<div id="post-body-content" class="postbox-container">
<div class="meta-box-sortables ui-sortable">
<?php
// Get hooks
$wp_hooks = array(
'wp_hooks' => array(
'label' => 'WordPress',
'hooks' => array(
'wp_head',
'wp_footer',
),
),
);
$hooks = $wp_hooks + tb_theme_hooks();
// Loop through sections
foreach( $hooks as $section ) { ?>
<h2><?php echo $section['label']; ?></h2>
<?php
// Loop through hooks
$hooks = $section['hooks'];
foreach ( $hooks as $hook ) {
// Get data
$action = ! empty( $options[$hook]['action'] ) ? $options[$hook]['action'] : '';
$php = isset( $options[$hook]['php'] ) ? true : false;
$priority = isset( $options[$hook]['priority'] ) ? intval( $options[$hook]['priority'] ) : 10; ?>
<div class="postbox closed">
<div class="handlediv" title="Click to toggle"></div>
<h3 class="hndle<?php if ( $action ) echo ' active'; ?>"><span><span class="dashicons dashicons-editor-code" style="padding-right:10px;"></span><?php echo $hook; ?></span></h3>
<div class="inside">
<p>
<label><?php _e( 'Code', 'tb' ); ?></label>
<textarea placeholder="<?php esc_attr_e( 'Enter your custom action here…', 'tb' ); ?>" name="tb_custom_actions[<?php echo $hook; ?>][action]" rows="10" cols="50" style="width:100%;" class="tb-textarea"><?php echo esc_textarea( $action ); ?></textarea>
</p>
<p class="clr">
<label><?php _e( 'Enable PHP', 'tb' ); ?></label>
<input name="tb_custom_actions[<?php echo $hook; ?>][php]" type="checkbox" value="" class="tb-enable-php" <?php checked( $php, true ); ?>>
</p>
<p class="clr">
<label><?php _e( 'Priority', 'tb' ); ?></label>
<input name="tb_custom_actions[<?php echo $hook; ?>][priority]" type="number" value="<?php echo $priority; ?>" class="tb-priority">
</p>
</div><!-- .inside -->
</div><!-- .postbox -->
<?php } ?>
<?php } ?>
</div><!-- .meta-box-sortables -->
</div><!-- #post-body-content -->
<div id="postbox-container-1" class="postbox-container">
<div class="postbox">
<h3 class='hndle'><span><span class="dashicons dashicons-flag" style="margin-right:7px;"></span><?php _e( 'Important PHP Notice', 'tb' ); ?></span></h3>
<div class="inside">
<p><?php _e( 'If you have enabled PHP for any action you MUST wrap your code in PHP tags. The theme will use the eval() function for outputting your PHP. Please be aware of the possibly security vulnerabilities this entails.', 'tb' ); ?></p>
<a href="http://php.net/manual/en/function.eval.php" title="<?php _e( 'Learn More', 'tb' ); ?>" target="_blank" class="button button-secondary"><?php _e( 'Learn More', 'tb' ); ?></a>
</div><!-- .inside -->
</div><!-- .postbox -->
<div class="postbox">
<h3 class='hndle'><span><span class="dashicons dashicons-upload" style="margin-right:7px;"></span><?php _e( 'Save Your Actions', 'tb' ); ?></span></h3>
<div class="inside">
<p><?php _e( 'Click the button below to save your custom actions.', 'tb' ); ?></p>
<?php submit_button(); ?>
</div><!-- .inside -->
</div><!-- .postbox -->
</div><!-- .postbox-container -->
</div><!-- #post-body-content -->
</div><!-- #post-body -->
</div><!-- #poststuff -->
</form>
<script>
( function( $ ) {
"use strict";
$( document ).ready( function() {
$( '.tb-custom-actions .handlediv, .tb-custom-actions .hndle' ).click( function( e ) {
e.preventDefault();
$( this ).parent().toggleClass( 'closed' );
} );
} );
} ) ( jQuery );
</script>
</div><!-- .wrap -->
<?php }
/**
* Outputs code on the front-end
*
* @since 3.0.0
*/
public function output() {
// Get actions
$actions = $this->custom_actions;
//print_r( $actions ); // for testing
// Front end only
if ( is_admin() || empty( $actions ) ) {
return;
}
// Loop through options
foreach ( $actions as $key => $val ) {
if ( ! empty( $val['action'] ) ) {
$priority = isset( $val['priority'] ) ? intval( $val['priority'] ) : 10;
add_action( $key, array( $this, 'execute_action' ), $priority );
}
}
}
/**
* Used to execute an action
*
* @since 3.0.0
*/
public function execute_action() {
// Set main vars
$hook = current_filter();
$actions = $this->custom_actions;
$php = ! empty( $actions[$hook]['php'] ) ? true : false;
$output = $actions[$hook]['action'];
// Output
if ( $output ) {
if ( $php ) {
eval( "?>$output<?php " );
} else {
echo do_shortcode( $output );
}
}
}
/**
* Admin CSS
*
* @since 3.0.0
*/
public static function admin_css() { ?>
<style type="text/css">
.clr:after { content: ""; display: block; height: 0; clear: both; visibility: hidden; zoom: 1; }
.tb-custom-actions-admin-wrap #poststuff h2 { font-size: 16px; margin: 10px 0; padding: 0; color: #000; font-weight: bold; }
.tb-custom-actions-admin-wrap #poststuff h3 { color: #555; }
.tb-custom-actions-admin-wrap #poststuff h3.active { color: #0095dd; }
.tb-custom-actions textarea { margin: 12px 0 3px; background: #f5f5f5; color: #000; padding: 15px; font-family: 'Verdana'; }
.tb-custom-actions p.submit { margin: 0 !important; padding: 0 !important; }
.tb-custom-actions label { color: #555; font-weight: bold; margin: 0; padding: 0; min-width: 80px; display: block; float: left; }
.tb-custom-actions .tb-priority { background: #f5f5f5; color: #000; width: 60px; }
</style>
<?php }
}
}
$tb_user_actions = new TB_User_Actions();