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

260 lines 11.5 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_ultp_filter', array($this, 'ultp_filter_callback')); // Next Previous AJAX Call
25 add_action('wp_ajax_ultp_pagination', array($this, 'ultp_pagination_callback')); // Page Number AJAX Call
26
27 register_activation_hook(ULTP_PATH.'ultimate-post.php', array($this, 'install_hook'));
28
29 add_action( 'activated_plugin', array($this, 'activation_redirect'));
30 }
31
32 public function register_scripts_option_panel_callback(){
33 if( null !== ( $screen = get_current_screen() ) && 'toplevel_page_ultp-settings' !== $screen->id ) {
34 return;
35 }
36 wp_enqueue_style('wp-color-picker');
37 wp_enqueue_script('wp-color-picker');
38 wp_enqueue_script('ultp-option-script', ULTP_URL.'assets/js/ultp-option.js', array('jquery'), ULTP_VER, true);
39 }
40
41 public function register_scripts_common(){
42 wp_enqueue_style('dashicons');
43 wp_enqueue_style('ultp-slick-style', ULTP_URL.'assets/css/slick.css', array(), ULTP_VER);
44 wp_enqueue_style('ultp-slick-theme-style', ULTP_URL.'assets/css/slick-theme.css', array(), ULTP_VER);
45 wp_enqueue_style('ultp-style', ULTP_URL.'assets/css/blocks.style.css', array(), ULTP_VER );
46 if(is_rtl()){ wp_enqueue_style('ultp-blocks-rtl-css', ULTP_URL.'assets/css/rtl.css', array(), ULTP_VER); }
47 wp_enqueue_script('ultp-flexmenu-script', ULTP_URL.'assets/js/flexmenu.js', array('jquery'), ULTP_VER, true);
48 wp_enqueue_script('ultp-slick-script', ULTP_URL.'assets/js/slick.min.js', array('jquery'), ULTP_VER, true);
49 wp_enqueue_script('ultp-script', ULTP_URL.'assets/js/ultp.js', array('jquery','ultp-flexmenu-script'), ULTP_VER, true);
50 wp_localize_script('ultp-script', 'ultp_data', array(
51 'url' => ULTP_URL,
52 'ajax' => admin_url('admin-ajax.php'),
53 'security' => wp_create_nonce('ultp-nonce')
54 ));
55 }
56
57 // Backend and Frontend Load script
58 public function register_scripts_front_callback() {
59 if ('yes' == get_post_meta(get_the_ID(), '_ultp_active', true)) {
60 $this->register_scripts_common();
61 }
62 }
63
64 // Only Backend
65 public function register_scripts_back_callback() {
66 $this->register_scripts_common();
67 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);
68 wp_enqueue_style('ultp-blocks-editor-css', ULTP_URL.'assets/css/blocks.editor.css', array(), ULTP_VER);
69 if(is_rtl()){ wp_enqueue_style('ultp-blocks-editor-rtl-css', ULTP_URL.'assets/css/rtl.css', array(), ULTP_VER); }
70 wp_localize_script('ultp-blocks-editor-script', 'ultp_data', array(
71 'url' => ULTP_URL,
72 'ajax' => admin_url('admin-ajax.php'),
73 'security' => wp_create_nonce('ultp-nonce')
74 ));
75 }
76
77 // Fire When Plugin First Install
78 public function install_hook() {
79 if (!get_option('ultp_options')) {
80 $option_data = array(
81 'css_save_as' => 'wp_head',
82 'preloader_style' => 'style1',
83 'preloader_color' => '#1740f5',
84 'container_width' => '1140'
85 );
86 update_option('ultp_options', $option_data);
87 }
88
89 }
90
91 public function activation_redirect($plugin) {
92 if( $plugin == 'ultimate-post/ultimate-post.php' ) {
93 exit(wp_redirect(admin_url('admin.php?page=ultp-settings')));
94 }
95 }
96
97 public function ultp_pagination_callback() {
98 if (!wp_verify_nonce($_REQUEST['wpnonce'], 'ultp-nonce') && $local) {
99 return ;
100 }
101
102 $paged = sanitize_text_field($_POST['paged']);
103 $blockId = sanitize_text_field($_POST['blockId']);
104 $postId = sanitize_text_field($_POST['postId']);
105 $blockRaw = sanitize_text_field($_POST['blockName']);
106 $blockName = str_replace('_','/', $blockRaw);
107
108 if($paged) {
109 $post = get_post($postId);
110 if (has_blocks($post->post_content)) {
111 $blocks = parse_blocks($post->post_content);
112 $this->block_return($blocks, $paged, $blockId, $blockRaw, $blockName);
113 }
114 }
115 }
116
117 public function block_return($blocks, $paged, $blockId, $blockRaw, $blockName) {
118 foreach ($blocks as $key => $value) {
119 if($blockName == $value['blockName']) {
120 if($value['attrs']['blockId'] == $blockId) {
121 $attr = $this->all_blocks[$blockRaw]->get_attributes(true);
122 $attr['paged'] = $paged;
123 $attr = array_merge($attr, $value['attrs']);
124 echo $this->all_blocks[$blockRaw]->content($attr, true);
125 die();
126 }
127 }
128 if(!empty($value['innerBlocks'])){
129 $this->block_return($value['innerBlocks'], $paged, $blockId, $blockRaw, $blockName);
130 }
131 }
132 }
133
134
135 public function ultp_filter_callback() {
136 if (!wp_verify_nonce($_REQUEST['wpnonce'], 'ultp-nonce') && $local){
137 return ;
138 }
139
140 $taxtype = sanitize_text_field($_POST['taxtype']);
141 $blockId = sanitize_text_field($_POST['blockId']);
142 $postId = sanitize_text_field($_POST['postId']);
143 $taxonomy = sanitize_text_field($_POST['taxonomy']);
144 $blockRaw = sanitize_text_field($_POST['blockName']);
145 $blockName = str_replace('_','/', $blockRaw);
146
147 if( $taxtype ) {
148 $post = get_post($postId);
149 if (has_blocks($post->post_content)) {
150 $blocks = parse_blocks($post->post_content);
151 $this->filter_block_return($blocks, $blockId, $blockRaw, $blockName, $taxtype, $taxonomy);
152 }
153 }
154 }
155
156 public function filter_block_return($blocks, $blockId, $blockRaw, $blockName, $taxtype, $taxonomy) {
157 foreach ($blocks as $key => $value) {
158 if($blockName == $value['blockName']) {
159 if($value['attrs']['blockId'] == $blockId) {
160 $attr = $this->all_blocks[$blockRaw]->get_attributes(true);
161 $attr['queryTax'] = $taxtype == 'category' ? 'category' : 'tag';
162 if($taxtype == 'category' && $taxonomy) {
163 $attr['queryCat'] = json_encode(array($taxonomy));
164 }
165 if($taxtype == 'post_tag' && $taxonomy) {
166 $attr['queryTag'] = json_encode(array($taxonomy));
167 }
168 $attr = array_merge($attr, $value['attrs']);
169 echo $this->all_blocks[$blockRaw]->content($attr, true);
170 die();
171 }
172 }
173 if(!empty($value['innerBlocks'])){
174 $this->filter_block_return($value['innerBlocks'], $blockId, $blockRaw, $blockName, $taxtype, $taxonomy);
175 }
176 }
177 }
178
179
180 public function ultp_next_prev_callback() {
181 if (!wp_verify_nonce($_REQUEST['wpnonce'], 'ultp-nonce') && $local){
182 return ;
183 }
184
185 $paged = sanitize_text_field($_POST['paged']);
186 $blockId = sanitize_text_field($_POST['blockId']);
187 $postId = sanitize_text_field($_POST['postId']);
188 $blockRaw = sanitize_text_field($_POST['blockName']);
189 $blockName = str_replace('_','/', $blockRaw);
190
191 if( $paged && $blockId && $postId && $blockName ) {
192 $post = get_post($postId);
193 if (has_blocks($post->post_content)) {
194 $blocks = parse_blocks($post->post_content);
195 $this->block_return($blocks, $paged, $blockId, $blockRaw, $blockName);
196 }
197 }
198 }
199
200 // Require Blocks
201 public function blocks() {
202 require_once ULTP_PATH.'blocks/Post_List_1.php';
203 require_once ULTP_PATH.'blocks/Post_List_2.php';
204 require_once ULTP_PATH.'blocks/Post_List_3.php';
205 require_once ULTP_PATH.'blocks/Post_Slider_1.php';
206 require_once ULTP_PATH.'blocks/Post_Grid_1.php';
207 require_once ULTP_PATH.'blocks/Post_Grid_2.php';
208 require_once ULTP_PATH.'blocks/Post_Grid_3.php';
209 require_once ULTP_PATH.'blocks/Post_Grid_4.php';
210 require_once ULTP_PATH.'blocks/Heading.php';
211 require_once ULTP_PATH.'blocks/Taxonomy.php';
212 $this->all_blocks['ultimate-post_post-list-1'] = new \ULTP\blocks\Post_List_1();
213 $this->all_blocks['ultimate-post_post-list-2'] = new \ULTP\blocks\Post_List_2();
214 $this->all_blocks['ultimate-post_post-list-3'] = new \ULTP\blocks\Post_List_3();
215 $this->all_blocks['ultimate-post_post-slider-1'] = new \ULTP\blocks\Post_Slider_1();
216 $this->all_blocks['ultimate-post_post-grid-1'] = new \ULTP\blocks\Post_Grid_1();
217 $this->all_blocks['ultimate-post_post-grid-2'] = new \ULTP\blocks\Post_Grid_2();
218 $this->all_blocks['ultimate-post_post-grid-3'] = new \ULTP\blocks\Post_Grid_3();
219 $this->all_blocks['ultimate-post_post-grid-4'] = new \ULTP\blocks\Post_Grid_4();
220 $this->all_blocks['ultimate-post_heading'] = new \ULTP\blocks\Heading();
221 $this->all_blocks['ultimate-post_taxonomy'] = new \ULTP\blocks\Taxonomy();
222 }
223
224 // Require Categories
225 public function requires() {
226 require_once ULTP_PATH.'classes/Styles.php';
227 require_once ULTP_PATH.'classes/Options.php';
228 require_once ULTP_PATH.'classes/REST_API.php';
229
230 new \ULTP\Options();
231 new \ULTP\Styles();
232 }
233
234 // Block Categories
235 public function register_category_callback( $categories, $post ) {
236 return array_merge(
237 array(
238 array(
239 'slug' => 'ultimate-post',
240 'title' => __( 'Ultimate Post', 'ultimate-post' )
241 )
242 ), $categories
243 );
244 }
245
246 public function popular_posts_tracker_callback($post_id) {
247 if (!is_single()){ return; }
248 global $post;
249 if (empty($post_id)) { $post_id = $post->ID; }
250 $count = (int)get_post_meta( $post_id, '__post_views_count', true );
251 update_post_meta($post_id, '__post_views_count', $count ? (int)$count + 1 : 1 );
252 }
253
254 public function add_image_size() {
255 add_image_size('ultp_layout_landscape_large', 1200, 800, true);
256 add_image_size('ultp_layout_landscape', 870, 570, true);
257 add_image_size('ultp_layout_portrait', 600, 900, true);
258 add_image_size('ultp_layout_square', 600, 600, true);
259 }
260 }