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