page.class.php
174 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Admin page class |
| 4 | * |
| 5 | * @author Alex Kovalev <alex.kovalevv@gmail.com> |
| 6 | * @since 1.0.0 |
| 7 | * @package factory-core |
| 8 | * @copyright (c) 2018, Webcraftic Ltd |
| 9 | * |
| 10 | */ |
| 11 | |
| 12 | // Exit if accessed directly |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit; |
| 15 | } |
| 16 | |
| 17 | if ( ! class_exists( 'Wbcr_FactoryPages480_Page' ) ) { |
| 18 | |
| 19 | class Wbcr_FactoryPages480_Page { |
| 20 | |
| 21 | |
| 22 | /** |
| 23 | * Уникальный ID страницы |
| 24 | * |
| 25 | * ID страницы используется для формирования ссылки на страницу. |
| 26 | * |
| 27 | * Ссылки выглядят примерно так: |
| 28 | * http://clearfy-test.loc/wp-admin/admin.php?page=components-wbcr_clearfy |
| 29 | * |
| 30 | * Чтобы не было конфликтов с другими плагинами, используйте префиксы. |
| 31 | * |
| 32 | * @since 1.0.0 |
| 33 | * @see FactoryPages480_AdminPage |
| 34 | * |
| 35 | * @var string |
| 36 | */ |
| 37 | public $id; |
| 38 | |
| 39 | /** |
| 40 | * Current Factory Plugin. |
| 41 | * |
| 42 | * @var Wbcr_Factory480_Plugin |
| 43 | */ |
| 44 | public $plugin; |
| 45 | protected $scripts; |
| 46 | protected $styles; |
| 47 | protected $request; |
| 48 | |
| 49 | /** |
| 50 | * @var string |
| 51 | */ |
| 52 | public $result; |
| 53 | |
| 54 | //private $default_actions = array(); |
| 55 | |
| 56 | /** |
| 57 | * @param Wbcr_Factory480_Plugin $plugin |
| 58 | * |
| 59 | * @throws Exception |
| 60 | */ |
| 61 | public function __construct( Wbcr_Factory480_Plugin $plugin ) { |
| 62 | $this->plugin = $plugin; |
| 63 | |
| 64 | if ( $plugin ) { |
| 65 | $this->scripts = $this->plugin->newScriptList(); |
| 66 | $this->styles = $this->plugin->newStyleList(); |
| 67 | $this->request = $plugin->request; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /*public function __call($name, $arguments) { |
| 72 | |
| 73 | |
| 74 | if(!empty($custom_action)) { |
| 75 | |
| 76 | } |
| 77 | |
| 78 | }*/ |
| 79 | |
| 80 | public function assets( $scripts, $styles ) { |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Shows page. |
| 85 | */ |
| 86 | public function show() { |
| 87 | |
| 88 | if ( $this->result ) { |
| 89 | echo $this->result; |
| 90 | } else { |
| 91 | $action = isset( $_GET['action'] ) ? $_GET['action'] : 'index'; |
| 92 | $this->executeByName( $action ); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @param string $action |
| 98 | * |
| 99 | * @throws Exception |
| 100 | */ |
| 101 | public function executeByName( $action ) { |
| 102 | $raw_action_name = $action; |
| 103 | |
| 104 | if ( preg_match( '/[-_]+/', $action ) ) { |
| 105 | $action = $this->dashesToCamelCase( $action, false ); |
| 106 | } |
| 107 | $actionFunction = $action . 'Action'; |
| 108 | |
| 109 | $cancel = $this->OnActionExecuting( $action ); |
| 110 | |
| 111 | if ( $cancel === false ) { |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | if ( ! method_exists( $this, $actionFunction ) ) { |
| 116 | // todo: продумать и доработать выполнение произвольны� |
| 117 | и глобальны� |
| 118 | дейтсвия для все� |
| 119 | страниц |
| 120 | /*$custom_actions = apply_filters('wbcr/factory_pages_480/custom_actions', array(), $raw_action_name); |
| 121 | |
| 122 | if(isset($custom_actions[$raw_action_name])) { |
| 123 | $custom_actions[$raw_action_name](); |
| 124 | $this->OnActionExected($action); |
| 125 | return; |
| 126 | } else {*/ |
| 127 | $actionFunction = 'indexAction'; |
| 128 | //} |
| 129 | } |
| 130 | |
| 131 | call_user_func_array( [ $this, $actionFunction ], [] ); |
| 132 | $this->OnActionExected( $action ); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * @param string $string |
| 137 | * @param bool $capitalizeFirstCharacter |
| 138 | * |
| 139 | * @return mixed |
| 140 | * @throws Exception |
| 141 | */ |
| 142 | protected function dashesToCamelCase( $string, $capitalizeFirstCharacter = false ) { |
| 143 | $str = str_replace( ' ', '', ucwords( preg_replace( '/[-_]/', ' ', $string ) ) ); |
| 144 | |
| 145 | if ( ! $capitalizeFirstCharacter ) { |
| 146 | $str[0] = strtolower( $str[0] ); |
| 147 | } |
| 148 | |
| 149 | if ( empty( $str ) ) { |
| 150 | throw new Exception( 'Dashed to camelcase parse error.' ); |
| 151 | } |
| 152 | |
| 153 | return $str; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * @param $action |
| 158 | * |
| 159 | * @return bool |
| 160 | */ |
| 161 | protected function OnActionExecuting( $action ) { |
| 162 | } |
| 163 | |
| 164 | protected function OnActionExected( $action ) { |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * @param $path |
| 169 | */ |
| 170 | protected function script( $path ) { |
| 171 | wp_enqueue_script( $path, $path, [ 'jquery' ], false, true ); |
| 172 | } |
| 173 | } |
| 174 | } |