PluginProbe
Toggle Content / 1.1.0
Toggle Content v1.1.0
trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.8 1.3.0
toggle-content / lib / style-handler / style-handler.php

style-handler.php in Toggle Content 1.1.0, at lib/style-handler/style-handler.php

140 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // Exit if accessed directly.
3 if (!defined('ABSPATH')) {
4 exit;
5 }
6
7 if(!class_exists('EbStyleHandler')){
8 final class EbStyleHandler
9 {
10 private static $instance;
11
12 private $media_desktop = [
13 'name' => 'desktop',
14 'screen_size' => ''
15 ];
16 private $media_tab = [
17 'name' => 'tab',
18 'screen_size' => 1024
19 ];
20 private $media_mobile = [
21 'name' => 'mobile',
22 'screen_size' => 767
23 ];
24
25
26 public static function init()
27 {
28 if (null === self::$instance) {
29 self::$instance = new self;
30 }
31 return self::$instance;
32 }
33
34 public function __construct()
35 {
36 add_action('admin_enqueue_scripts', [$this, 'essential_blocks_edit_post']);
37 add_action('wp_ajax_eb_write_block_css', [$this, 'write_block_css']);
38 add_action('wp_enqueue_scripts', [$this, 'enqueue_frontend_css']);
39 }
40
41 /**
42 * Enqueue a script in the WordPress admin on edit.php.
43 * @param int $hook Hook suffix for the current admin page.
44 */
45 public function essential_blocks_edit_post($hook)
46 {
47 global $post;
48 $dir = dirname( __FILE__ );
49 if ($hook == 'post-new.php' || $hook == 'post.php') {
50 $frontend_js = "style-handler.js";
51 wp_enqueue_script(
52 'essential-blocks-edit-post',
53 plugins_url($frontend_js, __FILE__),
54 array("jquery", "wp-editor"),
55 filemtime( $dir . "/" . $frontend_js),
56 true
57 );
58 wp_localize_script('essential-blocks-edit-post','eb_style_handler',[
59 'sth_nonce' => wp_create_nonce('eb_style_handler_nonce')
60 ]);
61 }
62 }
63
64 /**
65 * Ajax callback to write css in upload directory
66 * @retun void
67 * @since 1.0.2
68 */
69 public function write_block_css()
70 {
71 if(!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'],'eb_style_handler_nonce') || !current_user_can('manage_options')){
72 echo 'Invalid request';
73 wp_die();
74 }
75 if (!empty($css = $this->build_css($_POST['data']))) {
76 $upload_dir = wp_upload_dir()['basedir'] . '/eb-style/';
77 if (!file_exists($upload_dir)) {
78 mkdir($upload_dir);
79 }
80 file_put_contents($upload_dir . 'eb-style-' . abs($_POST['id']) . '.min.css', $css);
81 }
82
83 wp_die();
84 }
85
86 /**
87 * Enqueue frontend css for post if have one
88 * @return void
89 * @since 1.0.2
90 */
91 public function enqueue_frontend_css()
92 {
93 global $post;
94 if(!empty($post) && !empty($post->ID)){
95 $upload_dir = wp_upload_dir();
96 if (file_exists($upload_dir['basedir'] . '/eb-style/eb-style-' . $post->ID . '.min.css')) {
97 wp_enqueue_style('eb-block-style-' . $post->ID, $upload_dir['baseurl'] . '/eb-style/eb-style-' . $post->ID . '.min.css', [], substr(md5(microtime(true)), 0, 10));
98 }
99 }
100 }
101
102 /**
103 * Enqueue frontend css for post if have one
104 * @param string
105 * @return string
106 * @since 1.0.2
107 */
108 private function build_css($style_object)
109 {
110 $block_styles = (array)json_decode(stripslashes($style_object));
111
112 $css = '';
113 foreach ($block_styles as $block_style) {
114 if (!empty($block_css = (array) $block_style)) {
115 foreach ($block_css as $media => $style) {
116 switch ($media){
117 case $this->media_desktop['name'] :
118 $css .= preg_replace('/\s+/', ' ', $style);
119 break;
120 case $this->media_tab['name']:
121 $css .= ' @media(max-width: 1024px){';
122 $css .= preg_replace('/\s+/', ' ', $style);
123 $css .= '}';
124 break;
125 case $this->media_mobile['name']:
126 $css .= ' @media(max-width: 767px){';
127 $css .= preg_replace('/\s+/', ' ', $style);
128 $css .= '}';
129 break;
130 }
131 }
132 }
133 }
134 return trim($css);
135 }
136 }
137 EbStyleHandler::init();
138 }
139
140