PluginProbe
Post Grid Gutenberg Blocks – PostX / 1.2.3
Post Grid Gutenberg Blocks – PostX v1.2.3
5.0.39 5.0.38 5.0.37 5.0.36 5.0.35 5.0.34 5.0.33 5.0.32 5.0.31 5.0.30 5.0.29 5.0.28 5.0.27 5.0.26 5.0.25 5.0.23 5.0.24 5.0.22 5.0.21 5.0.20 5.0.19 5.0.18 5.0.17 2.1.1 2.1.2 All 237 releases
ultimate-post / classes / Initialization.php

Initialization.php in Post Grid Gutenberg Blocks – PostX 1.2.3, at classes/Initialization.php

274 lines 12.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace ULTP;
3
4 defined('ABSPATH') || exit;
5
6 class Initialization{
7
8 private $all_blocks;
9 private $api_endpoint = 'https://demo.wpxpo.com/wp-json/restapi/v2/';
10
11 public function __construct(){
12 $this->requires();
13 $this->blocks();
14
15 add_action('wp_head', array($this, 'popular_posts_tracker_callback'));
16 add_filter('block_categories', array($this, 'register_category_callback'), 10, 2); // Block Category Register
17 add_action('after_setup_theme', array($this, 'add_image_size'));
18
19 add_action('enqueue_block_editor_assets', array($this, 'register_scripts_back_callback')); // Only editor
20 add_action('admin_enqueue_scripts', array($this, 'register_scripts_option_panel_callback')); // Option Panel
21 add_action('wp_enqueue_scripts', array($this, 'register_scripts_front_callback')); // Both frontend
22
23 add_action('wp_ajax_ultp_next_prev', array($this, 'ultp_next_prev_callback')); // Next Previous AJAX Call
24 add_action('wp_ajax_nopriv_ultp_next_prev', array($this, 'ultp_next_prev_callback')); // Next Previous AJAX Call Logout User
25 add_action('wp_ajax_ultp_filter', array($this, 'ultp_filter_callback')); // Next Previous AJAX Call
26 add_action('wp_ajax_nopriv_ultp_filter', array($this, 'ultp_filter_callback')); // Next Previous AJAX Call Logout User
27 add_action('wp_ajax_ultp_pagination', array($this, 'ultp_pagination_callback')); // Page Number AJAX Call
28 add_action('wp_ajax_nopriv_ultp_pagination',array($this, 'ultp_pagination_callback')); // Page Number AJAX Call Logout User
29
30 register_activation_hook(ULTP_PATH.'ultimate-post.php', array($this, 'install_hook'));
31
32 add_action( 'activated_plugin', array($this, 'activation_redirect'));
33 }
34
35 public function register_scripts_option_panel_callback(){
36 if( null !== ( $screen = get_current_screen() ) && 'toplevel_page_ultp-settings' !== $screen->id ) {
37 return;
38 }
39 wp_enqueue_style('wp-color-picker');
40 wp_enqueue_script('wp-color-picker');
41 wp_enqueue_script('ultp-option-script', ULTP_URL.'assets/js/ultp-option.js', array('jquery'), ULTP_VER, true);
42 }
43
44 public function register_scripts_common(){
45 wp_enqueue_style('dashicons');
46 wp_enqueue_style('ultp-slick-style', ULTP_URL.'assets/css/slick.css', array(), ULTP_VER);
47 wp_enqueue_style('ultp-slick-theme-style', ULTP_URL.'assets/css/slick-theme.css', array(), ULTP_VER);
48 wp_enqueue_style('ultp-style', ULTP_URL.'assets/css/blocks.style.css', array(), ULTP_VER );
49 if(is_rtl()){ wp_enqueue_style('ultp-blocks-rtl-css', ULTP_URL.'assets/css/rtl.css', array(), ULTP_VER); }
50 wp_enqueue_script('ultp-flexmenu-script', ULTP_URL.'assets/js/flexmenu.js', array('jquery'), ULTP_VER, true);
51 wp_enqueue_script('ultp-slick-script', ULTP_URL.'assets/js/slick.min.js', array('jquery'), ULTP_VER, true);
52 wp_enqueue_script('ultp-script', ULTP_URL.'assets/js/ultp.js', array('jquery','ultp-flexmenu-script'), ULTP_VER, true);
53 wp_localize_script('ultp-script', 'ultp_data', array(
54 'url' => ULTP_URL,
55 'ajax' => admin_url('admin-ajax.php'),
56 'security' => wp_create_nonce('ultp-nonce')
57 ));
58 }
59
60 // Backend and Frontend Load script
61 public function register_scripts_front_callback() {
62 if ('yes' == get_post_meta(get_the_ID(), '_ultp_active', true)) {
63 $this->register_scripts_common();
64 }
65 }
66
67 // Only Backend
68 public function register_scripts_back_callback() {
69 $this->register_scripts_common();
70 wp_enqueue_script('ultp-blocks-editor-script', ULTP_URL.'assets/js/editor.blocks.min.js', array('wp-i18n', 'wp-element', 'wp-blocks', 'wp-components', 'wp-editor' ), ULTP_VER, true);
71 wp_enqueue_style('ultp-blocks-editor-css', ULTP_URL.'assets/css/blocks.editor.css', array(), ULTP_VER);
72 if(is_rtl()){ wp_enqueue_style('ultp-blocks-editor-rtl-css', ULTP_URL.'assets/css/rtl.css', array(), ULTP_VER); }
73
74 $import = '';
75 $options = get_option('ultp_options');
76 if(!$options){
77 $options = ultimate_post()->init_set_data();
78 }
79 if (isset($options['hide_import_btn'])) {
80 if ($options['hide_import_btn']=='yes') {
81 $import = 'yes';
82 }
83 }
84
85 wp_localize_script('ultp-blocks-editor-script', 'ultp_data', array(
86 'url' => ULTP_URL,
87 'ajax' => admin_url('admin-ajax.php'),
88 'security' => wp_create_nonce('ultp-nonce'),
89 'hide_import_btn' => $import
90 ));
91 }
92
93 // Fire When Plugin First Install
94 public function install_hook() {
95 if (!get_option('ultp_options')) {
96 ultimate_post()->init_set_data();
97 }
98 }
99
100 public function activation_redirect($plugin) {
101 if( $plugin == 'ultimate-post/ultimate-post.php' ) {
102 exit(wp_redirect(admin_url('admin.php?page=ultp-settings')));
103 }
104 }
105
106 public function ultp_pagination_callback() {
107 if (!wp_verify_nonce($_REQUEST['wpnonce'], 'ultp-nonce') && $local) {
108 return ;
109 }
110
111 $paged = sanitize_text_field($_POST['paged']);
112 $blockId = sanitize_text_field($_POST['blockId']);
113 $postId = sanitize_text_field($_POST['postId']);
114 $blockRaw = sanitize_text_field($_POST['blockName']);
115 $blockName = str_replace('_','/', $blockRaw);
116
117 if($paged) {
118 $post = get_post($postId);
119 if (has_blocks($post->post_content)) {
120 $blocks = parse_blocks($post->post_content);
121 $this->block_return($blocks, $paged, $blockId, $blockRaw, $blockName);
122 }
123 }
124 }
125
126 public function block_return($blocks, $paged, $blockId, $blockRaw, $blockName) {
127 foreach ($blocks as $key => $value) {
128 if($blockName == $value['blockName']) {
129 if($value['attrs']['blockId'] == $blockId) {
130 $attr = $this->all_blocks[$blockRaw]->get_attributes(true);
131 $attr['paged'] = $paged;
132 $attr = array_merge($attr, $value['attrs']);
133 echo $this->all_blocks[$blockRaw]->content($attr, true);
134 die();
135 }
136 }
137 if(!empty($value['innerBlocks'])){
138 $this->block_return($value['innerBlocks'], $paged, $blockId, $blockRaw, $blockName);
139 }
140 }
141 }
142
143
144 public function ultp_filter_callback() {
145 if (!wp_verify_nonce($_REQUEST['wpnonce'], 'ultp-nonce') && $local){
146 return ;
147 }
148
149 $taxtype = sanitize_text_field($_POST['taxtype']);
150 $blockId = sanitize_text_field($_POST['blockId']);
151 $postId = sanitize_text_field($_POST['postId']);
152 $taxonomy = sanitize_text_field($_POST['taxonomy']);
153 $blockRaw = sanitize_text_field($_POST['blockName']);
154 $blockName = str_replace('_','/', $blockRaw);
155
156 if( $taxtype ) {
157 $post = get_post($postId);
158 if (has_blocks($post->post_content)) {
159 $blocks = parse_blocks($post->post_content);
160 $this->filter_block_return($blocks, $blockId, $blockRaw, $blockName, $taxtype, $taxonomy);
161 }
162 }
163 }
164
165 public function filter_block_return($blocks, $blockId, $blockRaw, $blockName, $taxtype, $taxonomy) {
166 foreach ($blocks as $key => $value) {
167 if($blockName == $value['blockName']) {
168 if($value['attrs']['blockId'] == $blockId) {
169 $attr = $this->all_blocks[$blockRaw]->get_attributes(true);
170 $value['attrs']['queryTax'] = $taxtype == 'category' ? 'category' : 'tag';
171 if($taxtype == 'category' && $taxonomy) {
172 $value['attrs']['queryCat'] = json_encode(array($taxonomy));
173 }
174 if($taxtype == 'post_tag' && $taxonomy) {
175 $value['attrs']['queryTag'] = json_encode(array($taxonomy));
176 }
177 if(isset($value['attrs']['queryNumber'])){
178 $value['attrs']['queryNumber'] = $value['attrs']['queryNumber'];
179 }
180 $attr = array_merge($attr, $value['attrs']);
181 echo $this->all_blocks[$blockRaw]->content($attr, true);
182 die();
183 }
184 }
185 if(!empty($value['innerBlocks'])){
186 $this->filter_block_return($value['innerBlocks'], $blockId, $blockRaw, $blockName, $taxtype, $taxonomy);
187 }
188 }
189 }
190
191
192 public function ultp_next_prev_callback() {
193 if (!wp_verify_nonce($_REQUEST['wpnonce'], 'ultp-nonce') && $local){
194 return ;
195 }
196
197 $paged = sanitize_text_field($_POST['paged']);
198 $blockId = sanitize_text_field($_POST['blockId']);
199 $postId = sanitize_text_field($_POST['postId']);
200 $blockRaw = sanitize_text_field($_POST['blockName']);
201 $blockName = str_replace('_','/', $blockRaw);
202
203 if( $paged && $blockId && $postId && $blockName ) {
204 $post = get_post($postId);
205 if (has_blocks($post->post_content)) {
206 $blocks = parse_blocks($post->post_content);
207 $this->block_return($blocks, $paged, $blockId, $blockRaw, $blockName);
208 }
209 }
210 }
211
212 // Require Blocks
213 public function blocks() {
214 require_once ULTP_PATH.'blocks/Post_List_1.php';
215 require_once ULTP_PATH.'blocks/Post_List_2.php';
216 require_once ULTP_PATH.'blocks/Post_List_3.php';
217 require_once ULTP_PATH.'blocks/Post_Slider_1.php';
218 require_once ULTP_PATH.'blocks/Post_Grid_1.php';
219 require_once ULTP_PATH.'blocks/Post_Grid_2.php';
220 require_once ULTP_PATH.'blocks/Post_Grid_3.php';
221 require_once ULTP_PATH.'blocks/Post_Grid_4.php';
222 require_once ULTP_PATH.'blocks/Heading.php';
223 require_once ULTP_PATH.'blocks/Image.php';
224 //require_once ULTP_PATH.'blocks/Taxonomy.php';
225 $this->all_blocks['ultimate-post_post-list-1'] = new \ULTP\blocks\Post_List_1();
226 $this->all_blocks['ultimate-post_post-list-2'] = new \ULTP\blocks\Post_List_2();
227 $this->all_blocks['ultimate-post_post-list-3'] = new \ULTP\blocks\Post_List_3();
228 $this->all_blocks['ultimate-post_post-slider-1'] = new \ULTP\blocks\Post_Slider_1();
229 $this->all_blocks['ultimate-post_post-grid-1'] = new \ULTP\blocks\Post_Grid_1();
230 $this->all_blocks['ultimate-post_post-grid-2'] = new \ULTP\blocks\Post_Grid_2();
231 $this->all_blocks['ultimate-post_post-grid-3'] = new \ULTP\blocks\Post_Grid_3();
232 $this->all_blocks['ultimate-post_post-grid-4'] = new \ULTP\blocks\Post_Grid_4();
233 $this->all_blocks['ultimate-post_heading'] = new \ULTP\blocks\Heading();
234 //$this->all_blocks['ultimate-post_taxonomy'] = new \ULTP\blocks\Taxonomy();
235 $this->all_blocks['ultimate-post_image'] = new \ULTP\blocks\Image();
236 }
237
238 // Require Categories
239 public function requires() {
240 require_once ULTP_PATH.'classes/Styles.php';
241 require_once ULTP_PATH.'classes/Options.php';
242 require_once ULTP_PATH.'classes/REST_API.php';
243
244 new \ULTP\Options();
245 new \ULTP\Styles();
246 }
247
248 // Block Categories
249 public function register_category_callback( $categories, $post ) {
250 return array_merge(
251 array(
252 array(
253 'slug' => 'ultimate-post',
254 'title' => __( 'Gutenberg Post Blocks', 'ultimate-post' )
255 )
256 ), $categories
257 );
258 }
259
260 public function popular_posts_tracker_callback($post_id) {
261 if (!is_single()){ return; }
262 global $post;
263 if (empty($post_id)) { $post_id = $post->ID; }
264 $count = (int)get_post_meta( $post_id, '__post_views_count', true );
265 update_post_meta($post_id, '__post_views_count', $count ? (int)$count + 1 : 1 );
266 }
267
268 public function add_image_size() {
269 add_image_size('ultp_layout_landscape_large', 1200, 800, true);
270 add_image_size('ultp_layout_landscape', 870, 570, true);
271 add_image_size('ultp_layout_portrait', 600, 900, true);
272 add_image_size('ultp_layout_square', 600, 600, true);
273 }
274 }