| 1 |
<?php |
| 2 |
|
| 3 |
abstract class FV_Player_Wizard_Base_Class { |
| 4 |
|
| 5 |
private $args = array(); |
| 6 |
|
| 7 |
private $id = false; |
| 8 |
|
| 9 |
private $page; |
| 10 |
|
| 11 |
// array of class names based on FV_Player_Wizard_Step_Base_Class |
| 12 |
private $steps = array(); |
| 13 |
|
| 14 |
private $version = '8.0.20'; |
| 15 |
|
| 16 |
private $title = false; |
| 17 |
|
| 18 |
/** |
| 19 |
* Create a new instance of the Wizard class |
| 20 |
* |
| 21 |
* @param array $args { |
| 22 |
* id string Used for WP Ajax |
| 23 |
* page string the ?page=NAME of the wp-admin page with the Wizard for CSS loadign |
| 24 |
* steps_path string Path to folder with all the step_*.php Wizard step files |
| 25 |
* title string Wizard title to display |
| 26 |
* } |
| 27 |
* |
| 28 |
*/ |
| 29 |
function __construct( $args ) { |
| 30 |
$this->args = $args; |
| 31 |
$this->args['steps_path'] = trailingslashit($this->args['steps_path']); |
| 32 |
|
| 33 |
$this->set_id($this->args['id']); |
| 34 |
|
| 35 |
$this->load_steps($this->args['steps_path']); |
| 36 |
|
| 37 |
$this->page = $this->args['page']; |
| 38 |
|
| 39 |
$this->set_title($this->args['title']); |
| 40 |
|
| 41 |
// TODO: Somehow make these work even if self is only loadedd |
| 42 |
// on the wizard screen |
| 43 |
add_action( 'wp_ajax_'.$this->get_id().'_step', array( $this, 'ajax' ) ); |
| 44 |
|
| 45 |
add_action( 'admin_init', array( $this, 'styles' ) ); |
| 46 |
} |
| 47 |
|
| 48 |
/* |
| 49 |
* Accepts a callback function which should use $fv_fp->_get_input_text or similar |
| 50 |
*/ |
| 51 |
function add_step($step) { |
| 52 |
$this->steps[] = $step; |
| 53 |
} |
| 54 |
|
| 55 |
function ajax() { |
| 56 |
if( !wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), $this->get_id() ) ) { |
| 57 |
wp_send_json( array( 'error' => 'Invalid nonce' ) ); |
| 58 |
} |
| 59 |
|
| 60 |
$class_name = (string)$this->get_step( sanitize_text_field( $_POST['step_name'] ) ); |
| 61 |
$step = new $class_name; |
| 62 |
|
| 63 |
$result = call_user_func( array($step,'process') ); |
| 64 |
wp_send_json( $result ); |
| 65 |
} |
| 66 |
|
| 67 |
function get_id() { |
| 68 |
return $this->id; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* @param string $i Step name to get |
| 73 |
* |
| 74 |
* @return string Step name, if it's indeed added to the list of steps |
| 75 |
*/ |
| 76 |
function get_step($i) { |
| 77 |
if( is_string($i) && in_array($i, $this->steps, true ) ) { |
| 78 |
return $i; |
| 79 |
|
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
function get_steps() { |
| 84 |
return $this->steps; |
| 85 |
} |
| 86 |
|
| 87 |
function get_title() { |
| 88 |
return $this->title; |
| 89 |
} |
| 90 |
|
| 91 |
function load_steps($path) { |
| 92 |
$files = glob($path.'/step_*.php'); |
| 93 |
if( count($files) > 0 ) { |
| 94 |
foreach( $files AS $file ) { |
| 95 |
include_once($file); |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
function log( $args ) { |
| 101 |
$args = wp_parse_args( $args, array( |
| 102 |
'title' => false, |
| 103 |
'message' => false, |
| 104 |
'date' => gmdate('r'), |
| 105 |
'status' => 'info', |
| 106 |
'time' => time() |
| 107 |
)); |
| 108 |
|
| 109 |
$logs = get_option( $this->log_name(), array() ); |
| 110 |
$logs[] = $args; |
| 111 |
update_option( $this->log_name(), $logs, false ); |
| 112 |
} |
| 113 |
|
| 114 |
function log_get() { |
| 115 |
return get_option( $this->log_name(), array() ); |
| 116 |
} |
| 117 |
|
| 118 |
function log_name() { |
| 119 |
return $this->get_id().'-logs'; |
| 120 |
} |
| 121 |
|
| 122 |
function log_show() { |
| 123 |
if( count($this->log_get()) ) : ?> |
| 124 |
<a href='#' class='button' data-fv-wizard-show-log='<?php echo esc_attr($this->get_id()); ?>'>Show wizard log (<?php echo count($this->log_get()); ?>)</a> |
| 125 |
<table data-fv-wizard-log-wrap='<?php echo esc_attr($this->get_id()); ?>' style='display: none'> |
| 126 |
<?php foreach( array_reverse($this->log_get()) AS $log ) : |
| 127 |
$color = '#8f8'; |
| 128 |
if( $log['status'] == 'warning' ) { |
| 129 |
$color = '#ff8'; |
| 130 |
} else if( $log['status'] == 'error' ) { |
| 131 |
$color = '#f88'; |
| 132 |
} |
| 133 |
?> |
| 134 |
<tr style='background-color: <?php echo htmlentities($color); ?>'> |
| 135 |
<td><?php echo htmlentities( $log['date'] ); ?></td> |
| 136 |
<td><?php echo htmlentities($log['title']); ?></td> |
| 137 |
<td><?php echo htmlentities(print_r($log['message'], true)); ?></td> |
| 138 |
</tr> |
| 139 |
<?php endforeach; ?> |
| 140 |
</table> |
| 141 |
|
| 142 |
<script> |
| 143 |
jQuery(document).on( 'click', '[data-fv-wizard-show-log]', function() { |
| 144 |
jQuery('[data-fv-wizard-log-wrap='+jQuery(this).data('fv-wizard-show-log')+']').toggle(); |
| 145 |
return false; |
| 146 |
}); |
| 147 |
</script> |
| 148 |
<?php endif; |
| 149 |
} |
| 150 |
|
| 151 |
function register_step($class) { |
| 152 |
if( in_array($class,$this->get_steps()) ) { |
| 153 |
echo "<div class='error'><p>Error: Step " . esc_html( $class ) . " already loaded!</p></div>"; |
| 154 |
return; |
| 155 |
} |
| 156 |
$this->add_step($class); |
| 157 |
} |
| 158 |
|
| 159 |
function show() { |
| 160 |
?> |
| 161 |
<style> |
| 162 |
.regular-text { |
| 163 |
width: 40em; |
| 164 |
} |
| 165 |
img.fv-player-wizard-logo { |
| 166 |
float: left; |
| 167 |
height: 32px; |
| 168 |
margin-top: -5px; |
| 169 |
} |
| 170 |
</style> |
| 171 |
<div class="fv-player-wizard" data-fv-player-wizard> |
| 172 |
<?php |
| 173 |
echo '<img class="fv-player-wizard-logo" src="'.plugins_url( 'images/logo.png', $this->args['steps_path'].'sample' ).'" /><h1 class="wp-heading-inline">'.$this->get_title().'</h1>'; |
| 174 |
|
| 175 |
$shown_one_step = false; // only show a single step |
| 176 |
$i = 0; // keep track of the step being processed |
| 177 |
$first_step_number = -1; // remember the first visible step number, very important |
| 178 |
foreach( $this->get_steps() AS $class_name ) { |
| 179 |
$step = new $class_name; |
| 180 |
|
| 181 |
if( !call_user_func( array($step,'should_show') ) ) { |
| 182 |
$i++; |
| 183 |
continue; |
| 184 |
} |
| 185 |
|
| 186 |
$style = $shown_one_step ? " style='display: none;'" : ""; |
| 187 |
$shown_one_step = true; |
| 188 |
|
| 189 |
$extra_fields = ""; |
| 190 |
if( $fields = call_user_func( array($step,'extra_fields') ) ) { |
| 191 |
$extra_fields = " data-extra-fields='".wp_json_encode($fields)."'"; |
| 192 |
} |
| 193 |
|
| 194 |
echo "<table class='form-table fv-player-wizard-step' data-step='".intval($i)."' data-step_name='".esc_attr($class_name)."'".$style.$extra_fields.">\n"; |
| 195 |
|
| 196 |
call_user_func( array($step,'display') ); |
| 197 |
|
| 198 |
call_user_func( array($step,'buttons') ); |
| 199 |
|
| 200 |
echo "</table>\n"; |
| 201 |
|
| 202 |
call_user_func( array($step,'extra_scripts') ); |
| 203 |
|
| 204 |
if( $first_step_number < 0 ) { |
| 205 |
$first_step_number = $i; |
| 206 |
} |
| 207 |
|
| 208 |
$i++; |
| 209 |
} |
| 210 |
?> |
| 211 |
</div> |
| 212 |
<?php |
| 213 |
|
| 214 |
$this->log_show(); |
| 215 |
|
| 216 |
wp_enqueue_script( 'fv_player_wizard_base', plugins_url('fv-player-wizard-base.js', __FILE__), array('jquery'), $this->version ); |
| 217 |
wp_localize_script( 'fv_player_wizard_base', 'fv_player_wizard_base', array( |
| 218 |
'first_step_number' => $first_step_number, |
| 219 |
'id' => $this->id, |
| 220 |
'nonce' => wp_create_nonce( $this->get_id() ) |
| 221 |
)); |
| 222 |
|
| 223 |
} |
| 224 |
|
| 225 |
function styles() { |
| 226 |
if( !empty($_GET['page']) && strcmp( $this->page, sanitize_key( $_GET['page'] ) ) == 0 ) { |
| 227 |
wp_enqueue_style( 'fv_player_wizard_base', plugins_url('fv-player-wizard-base.css', __FILE__), array(), $this->version ); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
function set_id($id) { |
| 232 |
$this->id = $id; |
| 233 |
} |
| 234 |
|
| 235 |
function set_title($title) { |
| 236 |
$this->title = $title; |
| 237 |
} |
| 238 |
|
| 239 |
} |