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