PluginProbe
Post Grid Gutenberg Blocks – PostX / 1.1.3
Post Grid Gutenberg Blocks – PostX v1.1.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.1.3, at classes/Initialization.php

265 lines 12.0 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.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 wp_localize_script('ultp-blocks-editor-script', 'ultp_data', array(
74 'url' => ULTP_URL,
75 'ajax' => admin_url('admin-ajax.php'),
76 'security' => wp_create_nonce('ultp-nonce')
77 ));
78 }
79
80 // Fire When Plugin First Install
81 public function install_hook() {
82 if (!get_option('ultp_options')) {
83 $option_data = array(
84 'css_save_as' => 'wp_head',
85 'preloader_style' => 'style1',
86 'preloader_color' => '#1740f5',
87 'container_width' => '1140'
88 );
89 update_option('ultp_options', $option_data);
90 }
91
92 }
93
94 public function activation_redirect($plugin) {
95 if( $plugin == 'ultimate-post/ultimate-post.php' ) {
96 exit(wp_redirect(admin_url('admin.php?page=ultp-settings')));
97 }
98 }
99
100 public function ultp_pagination_callback() {
101 if (!wp_verify_nonce($_REQUEST['wpnonce'], 'ultp-nonce') && $local) {
102 return ;
103 }
104
105 $paged = sanitize_text_field($_POST['paged']);
106 $blockId = sanitize_text_field($_POST['blockId']);
107 $postId = sanitize_text_field($_POST['postId']);
108 $blockRaw = sanitize_text_field($_POST['blockName']);
109 $blockName = str_replace('_','/', $blockRaw);
110
111 if($paged) {
112 $post = get_post($postId);
113 if (has_blocks($post->post_content)) {
114 $blocks = parse_blocks($post->post_content);
115 $this->block_return($blocks, $paged, $blockId, $blockRaw, $blockName);
116 }
117 }
118 }
119
120 public function block_return($blocks, $paged, $blockId, $blockRaw, $blockName) {
121 foreach ($blocks as $key => $value) {
122 if($blockName == $value['blockName']) {
123 if($value['attrs']['blockId'] == $blockId) {
124 $attr = $this->all_blocks[$blockRaw]->get_attributes(true);
125 $attr['paged'] = $paged;
126 $attr = array_merge($attr, $value['attrs']);
127 echo $this->all_blocks[$blockRaw]->content($attr, true);
128 die();
129 }
130 }
131 if(!empty($value['innerBlocks'])){
132 $this->block_return($value['innerBlocks'], $paged, $blockId, $blockRaw, $blockName);
133 }
134 }
135 }
136
137
138 public function ultp_filter_callback() {
139 if (!wp_verify_nonce($_REQUEST['wpnonce'], 'ultp-nonce') && $local){
140 return ;
141 }
142
143 $taxtype = sanitize_text_field($_POST['taxtype']);
144 $blockId = sanitize_text_field($_POST['blockId']);
145 $postId = sanitize_text_field($_POST['postId']);
146 $taxonomy = sanitize_text_field($_POST['taxonomy']);
147 $blockRaw = sanitize_text_field($_POST['blockName']);
148 $blockName = str_replace('_','/', $blockRaw);
149
150 if( $taxtype ) {
151 $post = get_post($postId);
152 if (has_blocks($post->post_content)) {
153 $blocks = parse_blocks($post->post_content);
154 $this->filter_block_return($blocks, $blockId, $blockRaw, $blockName, $taxtype, $taxonomy);
155 }
156 }
157 }
158
159 public function filter_block_return($blocks, $blockId, $blockRaw, $blockName, $taxtype, $taxonomy) {
160 foreach ($blocks as $key => $value) {
161 if($blockName == $value['blockName']) {
162 if($value['attrs']['blockId'] == $blockId) {
163 $attr = $this->all_blocks[$blockRaw]->get_attributes(true);
164 $attr['queryTax'] = $taxtype == 'category' ? 'category' : 'tag';
165 if($taxtype == 'category' && $taxonomy) {
166 $attr['queryCat'] = json_encode(array($taxonomy));
167 }
168 if($taxtype == 'post_tag' && $taxonomy) {
169 $attr['queryTag'] = json_encode(array($taxonomy));
170 }
171 $attr = array_merge($attr, $value['attrs']);
172 echo $this->all_blocks[$blockRaw]->content($attr, true);
173 die();
174 }
175 }
176 if(!empty($value['innerBlocks'])){
177 $this->filter_block_return($value['innerBlocks'], $blockId, $blockRaw, $blockName, $taxtype, $taxonomy);
178 }
179 }
180 }
181
182
183 public function ultp_next_prev_callback() {
184 if (!wp_verify_nonce($_REQUEST['wpnonce'], 'ultp-nonce') && $local){
185 return ;
186 }
187
188 $paged = sanitize_text_field($_POST['paged']);
189 $blockId = sanitize_text_field($_POST['blockId']);
190 $postId = sanitize_text_field($_POST['postId']);
191 $blockRaw = sanitize_text_field($_POST['blockName']);
192 $blockName = str_replace('_','/', $blockRaw);
193
194 if( $paged && $blockId && $postId && $blockName ) {
195 $post = get_post($postId);
196 if (has_blocks($post->post_content)) {
197 $blocks = parse_blocks($post->post_content);
198 $this->block_return($blocks, $paged, $blockId, $blockRaw, $blockName);
199 }
200 }
201 }
202
203 // Require Blocks
204 public function blocks() {
205 require_once ULTP_PATH.'blocks/Post_List_1.php';
206 require_once ULTP_PATH.'blocks/Post_List_2.php';
207 require_once ULTP_PATH.'blocks/Post_List_3.php';
208 require_once ULTP_PATH.'blocks/Post_Slider_1.php';
209 require_once ULTP_PATH.'blocks/Post_Grid_1.php';
210 require_once ULTP_PATH.'blocks/Post_Grid_2.php';
211 require_once ULTP_PATH.'blocks/Post_Grid_3.php';
212 require_once ULTP_PATH.'blocks/Post_Grid_4.php';
213 require_once ULTP_PATH.'blocks/Heading.php';
214 require_once ULTP_PATH.'blocks/Image.php';
215 //require_once ULTP_PATH.'blocks/Taxonomy.php';
216 $this->all_blocks['ultimate-post_post-list-1'] = new \ULTP\blocks\Post_List_1();
217 $this->all_blocks['ultimate-post_post-list-2'] = new \ULTP\blocks\Post_List_2();
218 $this->all_blocks['ultimate-post_post-list-3'] = new \ULTP\blocks\Post_List_3();
219 $this->all_blocks['ultimate-post_post-slider-1'] = new \ULTP\blocks\Post_Slider_1();
220 $this->all_blocks['ultimate-post_post-grid-1'] = new \ULTP\blocks\Post_Grid_1();
221 $this->all_blocks['ultimate-post_post-grid-2'] = new \ULTP\blocks\Post_Grid_2();
222 $this->all_blocks['ultimate-post_post-grid-3'] = new \ULTP\blocks\Post_Grid_3();
223 $this->all_blocks['ultimate-post_post-grid-4'] = new \ULTP\blocks\Post_Grid_4();
224 $this->all_blocks['ultimate-post_heading'] = new \ULTP\blocks\Heading();
225 //$this->all_blocks['ultimate-post_taxonomy'] = new \ULTP\blocks\Taxonomy();
226 $this->all_blocks['ultimate-post_image'] = new \ULTP\blocks\Image();
227 }
228
229 // Require Categories
230 public function requires() {
231 require_once ULTP_PATH.'classes/Styles.php';
232 require_once ULTP_PATH.'classes/Options.php';
233 require_once ULTP_PATH.'classes/REST_API.php';
234
235 new \ULTP\Options();
236 new \ULTP\Styles();
237 }
238
239 // Block Categories
240 public function register_category_callback( $categories, $post ) {
241 return array_merge(
242 array(
243 array(
244 'slug' => 'ultimate-post',
245 'title' => __( 'Ultimate Post', 'ultimate-post' )
246 )
247 ), $categories
248 );
249 }
250
251 public function popular_posts_tracker_callback($post_id) {
252 if (!is_single()){ return; }
253 global $post;
254 if (empty($post_id)) { $post_id = $post->ID; }
255 $count = (int)get_post_meta( $post_id, '__post_views_count', true );
256 update_post_meta($post_id, '__post_views_count', $count ? (int)$count + 1 : 1 );
257 }
258
259 public function add_image_size() {
260 add_image_size('ultp_layout_landscape_large', 1200, 800, true);
261 add_image_size('ultp_layout_landscape', 870, 570, true);
262 add_image_size('ultp_layout_portrait', 600, 900, true);
263 add_image_size('ultp_layout_square', 600, 600, true);
264 }
265 }