| 1 |
<?php |
| 2 |
defined( 'ABSPATH' ) || exit; |
| 3 |
|
| 4 |
class Gutenberg_Post_Blocks_Widget extends \Elementor\Widget_Base { |
| 5 |
|
| 6 |
public function get_name() { |
| 7 |
return 'gutenberg-post-blocks'; |
| 8 |
} |
| 9 |
|
| 10 |
public function get_title() { |
| 11 |
return __( 'PostX Template', 'ultimate-post' ); |
| 12 |
} |
| 13 |
|
| 14 |
public function get_icon() { |
| 15 |
return 'eicon-posts-grid'; |
| 16 |
} |
| 17 |
|
| 18 |
public function get_categories() { |
| 19 |
return [ 'general' ]; |
| 20 |
} |
| 21 |
|
| 22 |
protected function _all_posts(){ |
| 23 |
$args = array( |
| 24 |
'post_type' => 'ultp_templates', |
| 25 |
'post_status' => 'publish', |
| 26 |
'posts_per_page' => -1 |
| 27 |
); |
| 28 |
$loop = new WP_Query( $args ); |
| 29 |
while ( $loop->have_posts() ) : $loop->the_post(); |
| 30 |
$data[get_the_ID()] = get_the_title(); |
| 31 |
endwhile; |
| 32 |
wp_reset_postdata(); |
| 33 |
return $data; |
| 34 |
} |
| 35 |
|
| 36 |
protected function _register_controls() { |
| 37 |
$this->start_controls_section( |
| 38 |
'content_section', |
| 39 |
[ |
| 40 |
'label' => __( 'Settings', 'ultimate-post' ), |
| 41 |
'tab' => \Elementor\Controls_Manager::TAB_CONTENT, |
| 42 |
] |
| 43 |
); |
| 44 |
$this->add_control( |
| 45 |
'saved_template', |
| 46 |
[ |
| 47 |
'label' => __( 'Saved Template', 'plugin-domain' ), |
| 48 |
'type' => \Elementor\Controls_Manager::SELECT, |
| 49 |
'options' => $this->_all_posts(), |
| 50 |
] |
| 51 |
); |
| 52 |
$this->add_control( |
| 53 |
'important_note', |
| 54 |
[ |
| 55 |
'type' => \Elementor\Controls_Manager::RAW_HTML, |
| 56 |
'raw' => '<a href="'.admin_url('edit.php?post_type=ultp_templates').'" class="ultp-elementor-settings" target="_blank">Edit Template with Gutenberg</a>', |
| 57 |
] |
| 58 |
); |
| 59 |
$this->end_controls_section(); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
protected function render() { |
| 64 |
$settings = $this->get_settings_for_display(); |
| 65 |
$id = $settings['saved_template']; |
| 66 |
ob_start(); |
| 67 |
|
| 68 |
if($id){ |
| 69 |
$this->set_inline_css($id); |
| 70 |
echo '<div class="ultp-shortcode" data-postid="'.$id.'">'; |
| 71 |
$args = array( 'p' => $id, 'post_type' => 'ultp_templates'); |
| 72 |
$the_query = new \WP_Query($args); |
| 73 |
if ($the_query->have_posts()) { |
| 74 |
while ($the_query->have_posts()) { |
| 75 |
$the_query->the_post(); |
| 76 |
the_content(); |
| 77 |
} |
| 78 |
wp_reset_postdata(); |
| 79 |
} |
| 80 |
echo '</div>'; |
| 81 |
}else{ |
| 82 |
echo '<p>'.__('Please Select Your Saved Template.', 'ultimate-post').'</p>'; |
| 83 |
} |
| 84 |
|
| 85 |
echo ob_get_clean(); |
| 86 |
} |
| 87 |
|
| 88 |
public function set_inline_css($id) { |
| 89 |
if(isset($_GET['action']) || isset($_POST['action'])){ |
| 90 |
$upload_dir_url = wp_get_upload_dir(); |
| 91 |
$upload_css_dir_url = trailingslashit( $upload_dir_url['basedir'] ); |
| 92 |
$css_dir_path = $upload_css_dir_url . "ultimate-post/ultp-css-{$id}.css"; |
| 93 |
if ( file_exists( $css_dir_path ) ) { |
| 94 |
$css_dir_url = trailingslashit( $upload_dir_url['baseurl'] ); |
| 95 |
if (is_ssl()) { |
| 96 |
$css_dir_url = str_replace('http://', 'https://', $css_dir_url); |
| 97 |
} |
| 98 |
$css_url = $css_dir_url . "ultimate-post/ultp-css-{$id}.css"; |
| 99 |
echo '<style type="text/css">'.file_get_contents($css_url).'</style>'; |
| 100 |
} else { |
| 101 |
$css = get_post_meta($id, '_ultp_css', true); |
| 102 |
if( $css ) { |
| 103 |
echo '<style type="text/css">'.$css.'</style>'; |
| 104 |
} |
| 105 |
} |
| 106 |
}else{ |
| 107 |
ultimate_post()->set_css_style($id); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
} |