create_script( $_POST['name'] );
$this->page_edit( $sanitized_name );
break;
case "edit":
$this->page_edit( $_GET['name'] );
break;
case "update":
$sanitized_name = $this->update_script( $_POST['prev_name'], $_POST['script'] );
if( isset( $_POST['eval_after_update'] ) ) {
$this->page_eval( $sanitized_name );
} else {
$this->page_edit( $sanitized_name );
}
break;
case "eval":
$this->page_eval( $_GET['name'] );
break;
case "delete":
$this->delete_script( $_GET['name'] );
$this->page_default();
break;
default:
$this->page_default();
break;
}
}
function page_default() {
$script_table = new AdminPhpEval_ScriptTable( $this );
$script_table->prepare_items();
?>
".$script['name']."" ); ?>
« | ", "" ); ?> | ", "" ); ?> »
$code" ); ?>
".print_r( $ret, true )."" ); ?>
« | ", "" ); ?> | ", "" ); ?> »
log( "Evaluated script $name with return value ".print_r( $ret, true ).".", 2 );
}
function get_scripts() {
return get_option( APE_SLUG, array() );
}
function update_scripts( $scripts ) {
update_option( APE_SLUG, $scripts );
}
function create_script( $name ) {
$scripts = $this->get_scripts();
$sanitized_name = sanitize_title( $name, 'new-script' );
$scripts[$sanitized_name] = array(
'name' => $sanitized_name,
'description' => '',
'code' => ''
);
$this->log( "Creating script $name.", 2 );
$this->update_scripts( $scripts );
return $sanitized_name;
}
function delete_script( $name ) {
$scripts = $this->get_scripts();
unset( $scripts[$name] );
$this->update_scripts( $scripts );
$this->log( "Deleted script $name", 2 );
}
function get_script( $name ) {
$scripts = $this->get_scripts();
return isset( $scripts[$name] ) ? $scripts[$name] : NULL;
}
function update_script( $prev_name, $script ) {
$scripts = $this->get_scripts();
unset( $scripts[$prev_name] );
$sanitized_name = sanitize_title( $script["name"], 'new-script' );
$script["name"] = $sanitized_name;
$scripts[$sanitized_name] = $script;
$this->update_scripts( $scripts );
$this->log( "Script $prev_name has been updated to ".print_r( $script, true ).".", 1 );
return $sanitized_name;
}
}
class AdminPhpEval_ScriptTable extends WP_List_Table {
const scripts_per_page = 50;
private $_p;
function __construct( $ape ) {
$this->_p = $ape;
parent::__construct( array(
'singular' => 'script', //singular name of the listed records
'plural' => 'scripts', //plural name of the listed records
'ajax' => false //does this table support ajax?
) );
}
function get_columns() {
$columns = array(
'name' => __( 'Name', PCD_TXD ),
'description' => __( 'Description', PCD_TXD ),
);
return $columns;
}
function get_sortable_columns() {
$sortable_columns = array(
'name' => array( 'name', true ), // true means its already sorted
);
return $sortable_columns;
}
function prepare_items() {
$columns = $this->get_columns();
$hidden = array();
$sortable = $this->get_sortable_columns();
$this->_column_headers = array($columns, $hidden, $sortable);
$per_page = AdminPhpEval_ScriptTable::scripts_per_page;
$current_page = $this->get_pagenum();
$this->items = $this->_p->get_scripts();
$total_items = count($this->items);
$order = ( !empty( $_REQUEST['order'] ) ) ? $_REQUEST['order'] : 'asc';
$cmp = create_function( '$a,$b',
'$c = strcoll( mb_strtoupper($a["name"], "UTF-8"), mb_strtoupper($b["name"], "UTF-8") );
if( $c == 0 ) {
return 0;
} else if( $c > 0 ) {
return ( '.$order.' == "asc" ) ? 1 : -1;
} else {
return ( '.$order.' == "desc" ) ? 1 : -1;
}'
);
usort( $this->items, $cmp );
$this->set_pagination_args( array(
'total_items' => $total_items, //WE have to calculate the total number of items
'per_page' => $per_page, //WE have to determine how many items to show on a page
'total_pages' => ceil($total_items/$per_page) //WE have to calculate the total number of pages
) );
}
function column_name( $item ) {
$lt = '