current = $this->currentItemMenu(); if ( array_key_exists( $this->current, $this->listMenu() ) ) { $this->catch_request(); } } /** * create template for admin page */ function createTpl( $content ) { $this->header(); echo $content; $this->footer(); } /** * show current template * @return bool or show template */ function getTemplate() { $current = $this->current; $args = $this->listMenu(); if ( !isset( $args[ $current ][ 'tmp' ] ) ) { return false; } $cancel = false; foreach ( $args as $key => $val ) { if ( $cancel ) { continue; } if ( $key == $current ) { $this->title = $val[ 'title' ]; $this->description = $val[ 'description' ]; $this->icon = $val[ 'icon' ]; $cancel = true; } if ( isset( $val[ 'submenu' ] ) ) { foreach ( $val[ 'submenu' ] as $skey => $sval ) { if ( $skey == $current ) { $this->title = $sval[ 'title' ]; $this->description = $sval[ 'description' ]; $this->icon = $val[ 'icon' ]; $cancel = true; } } } } $method = $args[ $current ][ 'tmp' ]; if ( method_exists( $this, $method ) or function_exists( $method ) ) { ob_start(); $this->renderTmpl( $method ); $content = ob_get_contents(); ob_end_clean(); $this->createTpl( $content ); return true; } return false; } /** * List Menu * @return mixed|string */ public function currentItemMenu() { $page = isset( $_GET[ 'czpage' ] ) ? $_GET[ 'czpage' ] : ''; if ( !$page ) { $list = $this->listMenu(); $page = key( $list ); } return $page; } /** * Admin form before */ private function header() { ?>
sidebar() ?>

title ?>

description ?>
createMenu() ?>
currentItemMenu(); $items = $this->listMenu(); if ( count( $items ) == 0 ) return false; $return = ""; return $return; } /** * List of admin menu * @return array */ public function listMenu() { return array(); } public function textField( $name = '', $args = array(), $layout = false ) { $defaults = array( 'label' => '', 'placeholder' => '', 'id' => '', 'value' => $this->{$name}, 'description' => '', 'readonly' => false, 'class' => 'large-text', 'maxlength' => '', 'screen' => '', ); $args = wp_parse_args( $args, $defaults ); $readonly = ( $args[ 'readonly' ] ) ? 'readonly="readonly"' : ''; $result = '
'; $args[ 'label' ] .= $args[ 'label' ] ? ':' : ''; $result .= ''; $result .= ''; $result .= '

' . $args[ 'description' ] . '

'; $result .= '
'; if ( $args[ 'screen' ] ) { $result .= '
'; } if ( $layout ) echo $result; return $result; } public function textTextArea( $name = '', $args = array(), $layout = false ) { $defaults = array( 'label' => '', 'id' => '', 'value' => $this->{$name}, 'description' => '', 'readonly' => false, 'rows' => 2, 'class' => 'large-text', 'screen' => '' ); $args = wp_parse_args( $args, $defaults ); $readonly = ( $args[ 'readonly' ] ) ? 'readonly="readonly"' : ''; $result = '
'; $args[ 'label' ] .= $args[ 'label' ] ? ':' : ''; $result .= ''; $result .= '

' . $args[ 'description' ] . '

'; $result .= '
'; if ( $args[ 'screen' ] ) { $result .= '
'; } if ( $layout ) echo $result; return $result; } public function tinymce( $name = '', $args = array(), $layout = false ) { $defaults = array( 'label' => '', 'id' => '', 'value' => $this->{$name}, 'description' => '', 'readonly' => false, 'rows' => 20, 'class' => 'large-text', 'screen' => '' ); $args = wp_parse_args( $args, $defaults ); $readonly = ( $args[ 'readonly' ] ) ? 'readonly="readonly"' : ''; ob_start(); $args[ 'id' ] = $args[ 'id' ] ? $args[ 'id' ] : $name; wp_editor( $args[ 'value' ], $args[ 'id' ], array( 'wpautop' => 1, 'media_buttons' => 1, 'textarea_name' => $name, 'textarea_rows' => $args[ 'rows' ], 'tabindex' => null, 'editor_css' => '', 'editor_class' => $args[ 'class' ], 'teeny' => 0, 'dfw' => 0, 'tinymce' => 1, 'quicktags' => 1, 'drag_drop_upload' => true ) ); $wp_editor = ob_get_contents(); ob_end_clean(); $result = '
'; $args[ 'label' ] .= $args[ 'label' ] ? ':' : ''; $result .= ''; $result .= '

' . $args[ 'description' ] . '

'; $result .= $wp_editor; $result .= '
'; if ( $args[ 'screen' ] ) { $result .= '
'; } if ( $layout ) echo $result; return $result; } public function uploadImgField( $name = '', $args = array(), $layout = false ) { $defaults = array( 'label' => '', 'id' => '', 'value' => $this->{$name}, 'description' => '', 'readonly' => false, 'class' => '', 'width' => '', 'height' => '', 'screen' => '', 'align' => 'left' ); $args = wp_parse_args( $args, $defaults ); $default = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA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X99Qnq6RSz2pByTs1IXbnWJtLhpZmmqkSmVh3kSv0B01btYkoumC90v1VrYX2Kx25Ncmbe37h1yNSkwj8GBnVJut51VjZGIF8zpY+u/2Oi3z/sy+hJr3X/e95ZIsP7GEK47CnLDF0wbq/UtOsdsw21LJv2Vq6uZW2GTGm1CeDrRF8j6bmvZXV//8BaVhtCaJ80EPT01iEIIABBAAEIAghAEEAAggCCAAIQBBCAIIAABAEEIAggCCAAQQABCAIIQBBAAIIAggACEAQQgCCA/Df6OQBwPL9lwL3p8AAAAABJRU5ErkJggg=='; if ( $args[ 'value' ] ) { $image_attributes = wp_get_attachment_image_src( $args[ 'value' ], array( $args[ 'width' ], $args[ 'height' ] ) ); if ( $image_attributes ) { $src = $args[ 'value' ] = $image_attributes[ 0 ]; } else { $src = $args[ 'value' ]; } } else { $src = $default; } $src = aliprice_is_url( $src ) ? $src : $default; $result = '
'; $result .= ''; $result .= '

' . $args[ 'description' ] . '

'; $result .= '
'; $result .= '
'; if ( $args[ 'screen' ] ) { $result .= '
'; } if ( $layout ) echo $result; return $result; } public function colorField( $name = '', $args = array(), $layout = false ) { $defaults = array( 'label' => '', 'placeholder' => '', 'id' => '', 'value' => $this->{$name}, 'description' => '', 'readonly' => false, 'class' => 'large-text', 'maxlength' => '', 'screen' => '', 'inline' => true ); $args = wp_parse_args( $args, $defaults ); $readonly = ( $args[ 'readonly' ] ) ? 'readonly="readonly"' : ''; $result = '

' . $args[ 'description' ] . '

'; $inline = $args[ 'inline' ] ? 'inline' : ''; $result = '
'; $result .= ' '; if ( $args[ 'description' ] ) $result .= '

' . $args[ 'description' ] . '

'; $result .= '
'; if ( $args[ 'screen' ] ) { $result .= '
'; } if ( $layout ) echo $result; return $result; } public function Button( $name = '', $args = array(), $layout = false ) { $defaults = array( 'label' => '', 'id' => '', 'value' => $this->{$name}, 'description' => '', 'readonly' => false, 'rows' => 2, 'class' => 'large-text', 'text' => '', 'screen' => '', 'inline' => true ); $args = wp_parse_args( $args, $defaults ); $inline = $args[ 'inline' ] ? 'inline' : ''; $result = '
'; $result .= ' '; if ( $args[ 'description' ] ) $result .= '

' . $args[ 'description' ] . '

'; $result .= '
'; if ( $args[ 'screen' ] ) { $result .= '
'; } if ( $layout ) echo $result; return $result; } public function checkboxField( $name = '', $args = array(), $layout = false ) { $defaults = array( 'label' => '', 'id' => '', 'value' => $this->{$name}, 'description' => '', 'readonly' => false, 'rows' => 2, 'class' => '', 'screen' => '', 'inline' => true ); $args = wp_parse_args( $args, $defaults ); $checked = $args[ 'value' ] ? 'checked' : ''; $inline = $args[ 'inline' ] ? 'inline' : ''; $result = '
'; $result .= ' '; if ( $args[ 'description' ] ) $result .= '

' . $args[ 'description' ] . '

'; $result .= '
'; if ( $args[ 'screen' ] ) { $result .= '
'; } if ( $layout ) echo $result; return $result; } public function dropDownField( $name = '', $args = array(), $layout = false ) { $defaults = array( 'label' => '', 'selected' => $this->{$name}, 'id' => '', //'value' => $this->{$name} ? $this->{$name} : array(), 'value' => array(), 'description' => '', 'readonly' => false, 'class' => 'large-select', ); $args = wp_parse_args( $args, $defaults ); $readonly = ( $args[ 'readonly' ] ) ? 'readonly="readonly"' : ''; $result = '

' . $args[ 'description' ] . '

'; if ( $layout ) echo $result; return $result; } /* * settings fields * */ public function Title( $text = '', $args = array() ) { $defaults = array( 'text' => $text, 'id' => '', 'rows' => 2, 'class' => '', 'colspan' => 2, ); $args = wp_parse_args( $args, $defaults ); $result = '

' . $args[ 'text' ] . '

'; echo $result; } public function Line( $args = array(), $layout = false ) { $defaults = array( 'type' => 'solid', 'color' => '#eee', 'id' => '', 'rows' => 2, 'class' => '', 'colspan' => 2, ); $args = wp_parse_args( $args, $defaults ); $result = '
'; if ( $layout ) echo $result; return $result; } public function row( $fields = array(), $args = array(), $layout = false ) { if ( !$fields ) return; if ( !is_array( $fields ) ) $fields = array( $fields ); $defaults = array( 'class' => '' ); $args = wp_parse_args( $args, $defaults ); $width = round( 100 / count( $fields ), 6 ); $result = ''; foreach ( $fields as $field ) { $result .= ''; } $result .= '
'; $result .= $field; $result .= '
'; if ( $layout ) echo $result; return $result; } public function col( $fields = array(), $args = array(), $layout = false ) { if ( !$fields ) return; if ( !is_array( $fields ) ) $fields = array( $fields ); $defaults = array( 'class' => '' ); $args = wp_parse_args( $args, $defaults ); $result = '
'; foreach ( $fields as $field ) { $result .= '
'; $result .= $field; $result .= '
'; } $result .= '
'; if ( $layout ) echo $result; return $result; } public function block( $rows = array(), $args = array(), $layout = true ) { if ( !$rows ) return; if ( !is_array( $rows ) ) $rows = array( $rows ); $defaults = array( 'class' => '', 'title' => '' ); $args = wp_parse_args( $args, $defaults ); $result = ''; $result .= ''; $result .= '
'; if ( !empty( $args[ 'title' ] ) ) { $result .= '

' . $args[ 'title' ] . '

'; } $result .= '
'; foreach ( $rows as $row ) { $result .= $row; } $result .= '
'; if ( $layout ) echo $result; return $result; } //TODO в разработке public function tabs( $tabs = array(), $args = array(), $layout = true ) { if ( !$tabs ) return; if ( !is_array( $tabs ) ) $tabs = array( $tabs ); $defaults = array( 'class' => '', 'title' => '' ); $args = wp_parse_args( $args, $defaults ); $result = ''; $result .= ''; $result .= '
'; if ( !empty( $args[ 'title' ] ) ) { $result .= '

' . $args[ 'title' ] . '

'; } $result .= '
'; foreach ( $tabs as $tab ) { $result .= $tab; } $result .= '
'; if ( $layout ) echo $result; return $result; } function renderTmpl( $method ) { echo $this->message; ?>
$method(); else $method(); ?>
message = '
'; } //@TODO save stripslashes public function __set( $name, $value ) { if ( array_key_exists( $name, $this->get_defaults() ) ) { $this->data[ $name ] = htmlspecialchars_decode( $value, ENT_QUOTES ); } else { throw new \Exception( 'Setting unknown property:' . $name ); } } public function save() { return update_option( $this->field_options, $this->data ); } /** * Catch request from form */ public function catch_request() { if ( isset( $_POST[ 'cz_setting' ] ) && wp_verify_nonce( $_POST[ 'cz_setting' ], 'cz_setting_action' ) ) { $options = $this->get_defaults(); foreach ( $options as $k => $v ) { if ( isset( $_POST[ $k ] ) ) $this->$k = isset( $_POST[ 'default' ] ) ? $v : $this->clearData( $_POST[ $k ] ); } if ( $this->save() ) { $this->setMessage( __( 'The сhanges have been saved' ) ); } else { $this->setMessage( __( 'No changes have been made' ), 1 ); } } } private function clearData( $str ) { $str = trim( $str ); return $str; } }