| 1 |
<?php |
| 2 |
/** |
| 3 |
* Vue.js based Interface Builder module |
| 4 |
* |
| 5 |
* Version: 1.4.10 |
| 6 |
*/ |
| 7 |
|
| 8 |
// If this file is called directly, abort. |
| 9 |
if ( ! defined( 'WPINC' ) ) { |
| 10 |
die; |
| 11 |
} |
| 12 |
|
| 13 |
if ( ! class_exists( 'CX_Vue_UI' ) ) { |
| 14 |
|
| 15 |
/** |
| 16 |
* Class Cherry Interface Builder. |
| 17 |
* |
| 18 |
* @since 1.0.0 |
| 19 |
*/ |
| 20 |
class CX_Vue_UI { |
| 21 |
|
| 22 |
/** |
| 23 |
* Module directory path. |
| 24 |
* |
| 25 |
* @since 1.0.0 |
| 26 |
* @access protected |
| 27 |
* @var srting. |
| 28 |
*/ |
| 29 |
protected $path; |
| 30 |
|
| 31 |
/** |
| 32 |
* Module directory URL. |
| 33 |
* |
| 34 |
* @since 1.0.0 |
| 35 |
* @access protected |
| 36 |
* @var srting. |
| 37 |
*/ |
| 38 |
protected $url; |
| 39 |
|
| 40 |
/** |
| 41 |
* Current instance templates path. |
| 42 |
* |
| 43 |
* @since 1.4.3 |
| 44 |
* @access protected |
| 45 |
* @var srting. |
| 46 |
*/ |
| 47 |
public static $templates_path; |
| 48 |
|
| 49 |
/** |
| 50 |
* Module version |
| 51 |
* |
| 52 |
* @var string |
| 53 |
*/ |
| 54 |
protected $version = '1.4.10'; |
| 55 |
|
| 56 |
/** |
| 57 |
* [$assets_enqueued description] |
| 58 |
* @var boolean |
| 59 |
*/ |
| 60 |
protected $assets_enqueued = false; |
| 61 |
|
| 62 |
/** |
| 63 |
* CX_Vue_UI constructor. |
| 64 |
* |
| 65 |
* @since 1.0.0 |
| 66 |
* @access public |
| 67 |
* @return void |
| 68 |
*/ |
| 69 |
public function __construct( array $args = array() ) { |
| 70 |
|
| 71 |
$this->path = ! empty( $args['path'] ) ? $args['path'] : false; |
| 72 |
$this->url = ! empty( $args['url'] ) ? $args['url'] : false; |
| 73 |
|
| 74 |
if ( ! $this->path || ! $this->url ) { |
| 75 |
wp_die( |
| 76 |
'CX_Vue_UI not initialized. Module URL and Path should be passed into constructor', |
| 77 |
'CX_Vue_UI Error' |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Enqueue builder assets |
| 87 |
* |
| 88 |
* @return void |
| 89 |
*/ |
| 90 |
public function enqueue_assets() { |
| 91 |
|
| 92 |
if ( $this->assets_enqueued ) { |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
wp_enqueue_media(); |
| 97 |
|
| 98 |
$suffix = '.min'; |
| 99 |
|
| 100 |
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) { |
| 101 |
$suffix = ''; |
| 102 |
} |
| 103 |
|
| 104 |
wp_register_script( |
| 105 |
'cx-vue', |
| 106 |
$this->url . 'assets/js/vue' . $suffix . '.js', |
| 107 |
array(), |
| 108 |
'2.6.11', |
| 109 |
true |
| 110 |
); |
| 111 |
|
| 112 |
wp_register_script( |
| 113 |
'cx-vue-components', |
| 114 |
$this->url . 'assets/js/cx-vue-ui-components.js', |
| 115 |
array(), |
| 116 |
$this->version, |
| 117 |
true |
| 118 |
); |
| 119 |
|
| 120 |
wp_enqueue_script( |
| 121 |
'cx-vue-ui', |
| 122 |
$this->url . 'assets/js/cx-vue-ui.js', |
| 123 |
array( 'cx-vue', 'cx-vue-components' ), |
| 124 |
$this->version, |
| 125 |
true |
| 126 |
); |
| 127 |
|
| 128 |
add_action( 'admin_footer', array( $this, 'print_templates' ), 0 ); |
| 129 |
|
| 130 |
$this->assets_enqueued = true; |
| 131 |
|
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Enqueue builder assets |
| 136 |
* |
| 137 |
* @return void |
| 138 |
*/ |
| 139 |
public function enqueue_assets_components() { |
| 140 |
|
| 141 |
if ( $this->assets_enqueued ) { |
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
wp_enqueue_media(); |
| 146 |
|
| 147 |
$suffix = '.min'; |
| 148 |
|
| 149 |
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) { |
| 150 |
$suffix = ''; |
| 151 |
} |
| 152 |
|
| 153 |
wp_enqueue_script( |
| 154 |
'cx-vue-ui-components', |
| 155 |
$this->url . 'assets/js/cx-vue-ui-components.js', |
| 156 |
array(), |
| 157 |
$this->version, |
| 158 |
true |
| 159 |
); |
| 160 |
|
| 161 |
add_action( 'admin_footer', array( $this, 'print_templates' ), 0 ); |
| 162 |
|
| 163 |
$this->assets_enqueued = true; |
| 164 |
|
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Returns registered components list |
| 169 |
* |
| 170 |
* @return array |
| 171 |
*/ |
| 172 |
public function components_list() { |
| 173 |
return apply_filters( 'cx-vue-ui/components-list', array( |
| 174 |
|
| 175 |
// Layout elements |
| 176 |
'title', |
| 177 |
'collapse', |
| 178 |
'component-wrapper', |
| 179 |
'button', |
| 180 |
'repeater', |
| 181 |
'repeater-item', |
| 182 |
'popup', |
| 183 |
'list-table', |
| 184 |
'list-table-heading', |
| 185 |
'list-table-item', |
| 186 |
'tabs', |
| 187 |
'tabs-panel', |
| 188 |
'pagination', |
| 189 |
'notice', |
| 190 |
|
| 191 |
// Form elements |
| 192 |
'input', |
| 193 |
'time', |
| 194 |
'textarea', |
| 195 |
'switcher', |
| 196 |
'iconpicker', |
| 197 |
'select', |
| 198 |
'f-select', |
| 199 |
'checkbox', |
| 200 |
'radio', |
| 201 |
'colorpicker', |
| 202 |
'wp-media', |
| 203 |
'dimensions', |
| 204 |
) ); |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Print components templates |
| 209 |
* |
| 210 |
* @return void |
| 211 |
*/ |
| 212 |
public function print_templates() { |
| 213 |
|
| 214 |
$path = $this->path . 'components/*.php'; |
| 215 |
$whitelisted = $this->components_list(); |
| 216 |
|
| 217 |
foreach ( glob( $path ) as $file ) { |
| 218 |
|
| 219 |
$slug = basename( $file, '.php' ); |
| 220 |
|
| 221 |
if ( ! in_array( $slug, $whitelisted ) ) { |
| 222 |
continue; |
| 223 |
} |
| 224 |
|
| 225 |
ob_start(); |
| 226 |
|
| 227 |
if ( self::$templates_path && file_exists( self::$templates_path . basename( $file ) ) ) { |
| 228 |
include self::$templates_path . basename( $file ); |
| 229 |
} else { |
| 230 |
include $file; |
| 231 |
} |
| 232 |
|
| 233 |
$template = ob_get_clean(); |
| 234 |
|
| 235 |
printf( |
| 236 |
'<script type="text/x-template" id="%2$s">%1$s</script>', |
| 237 |
$template, |
| 238 |
'cx-vui-' . $slug |
| 239 |
); |
| 240 |
|
| 241 |
} |
| 242 |
|
| 243 |
} |
| 244 |
|
| 245 |
} |
| 246 |
|
| 247 |
} |
| 248 |
|