| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Builder\Widgets; |
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; // Exit if accessed directly. |
| 6 |
} |
| 7 |
|
| 8 |
use Elementor\Controls_Manager; |
| 9 |
use Elementor\Core\Kits\Documents\Tabs\Global_Colors; |
| 10 |
use Elementor\Core\Kits\Documents\Tabs\Global_Typography; |
| 11 |
use Elementor\Group_Control_Typography; |
| 12 |
use Elementor\Plugin; |
| 13 |
use Elementor\Utils; |
| 14 |
use Elementor\Widget_Base; |
| 15 |
|
| 16 |
class Post_Title extends Widget_Base { |
| 17 |
|
| 18 |
public function get_name() { |
| 19 |
return 'tl-post-title'; |
| 20 |
} |
| 21 |
|
| 22 |
public function get_title() { |
| 23 |
return esc_html__( 'Post Title', 'templately' ); |
| 24 |
} |
| 25 |
|
| 26 |
public function get_icon() { |
| 27 |
return 'eicon-t-letter templately-widget-icon'; |
| 28 |
} |
| 29 |
|
| 30 |
public function get_categories() { |
| 31 |
return [ 'theme-elements-single' ]; |
| 32 |
} |
| 33 |
|
| 34 |
public function get_keywords() { |
| 35 |
return [ 'title', 'post title', 'page title', 'heading' ]; |
| 36 |
} |
| 37 |
|
| 38 |
protected function register_controls() { |
| 39 |
$this->start_controls_section( |
| 40 |
'section_title', |
| 41 |
[ |
| 42 |
'label' => esc_html__( 'Heading', 'templately' ), |
| 43 |
] |
| 44 |
); |
| 45 |
|
| 46 |
$this->add_control( |
| 47 |
'html_tag', |
| 48 |
[ |
| 49 |
'label' => esc_html__( 'HTML Tag', 'templately' ), |
| 50 |
'type' => Controls_Manager::SELECT, |
| 51 |
'options' => [ |
| 52 |
'h1' => 'H1', |
| 53 |
'h2' => 'H2', |
| 54 |
'h3' => 'H3', |
| 55 |
'h4' => 'H4', |
| 56 |
'h5' => 'H5', |
| 57 |
'h6' => 'H6', |
| 58 |
'div' => 'div', |
| 59 |
'span' => 'span', |
| 60 |
'p' => 'p', |
| 61 |
], |
| 62 |
'default' => 'h2', |
| 63 |
] |
| 64 |
); |
| 65 |
|
| 66 |
$this->end_controls_section(); |
| 67 |
|
| 68 |
$this->start_controls_section( |
| 69 |
'section_title_style', |
| 70 |
[ |
| 71 |
'label' => esc_html__( 'Heading', 'templately' ), |
| 72 |
'tab' => Controls_Manager::TAB_STYLE, |
| 73 |
] |
| 74 |
); |
| 75 |
|
| 76 |
$this->add_control( |
| 77 |
'title_color', |
| 78 |
[ |
| 79 |
'label' => esc_html__( 'Text Color', 'templately' ), |
| 80 |
'type' => Controls_Manager::COLOR, |
| 81 |
'global' => [ |
| 82 |
'default' => Global_Colors::COLOR_PRIMARY, |
| 83 |
], |
| 84 |
'selectors' => [ |
| 85 |
'{{WRAPPER}} .templately-heading-title' => 'color: {{VALUE}};', |
| 86 |
], |
| 87 |
] |
| 88 |
); |
| 89 |
|
| 90 |
$this->add_group_control( |
| 91 |
Group_Control_Typography::get_type(), |
| 92 |
[ |
| 93 |
'name' => 'typography', |
| 94 |
'global' => [ |
| 95 |
'default' => Global_Typography::TYPOGRAPHY_PRIMARY, |
| 96 |
], |
| 97 |
'selector' => '{{WRAPPER}} .templately-heading-title', |
| 98 |
] |
| 99 |
); |
| 100 |
|
| 101 |
$this->end_controls_section(); |
| 102 |
} |
| 103 |
|
| 104 |
protected function get_dynamic_post_ID() { |
| 105 |
$post_type = 'post'; |
| 106 |
$type = get_post_meta( get_the_ID(), '_templately_template_type', true ); |
| 107 |
if($type == 'single') { |
| 108 |
$post_type = 'post'; |
| 109 |
} |
| 110 |
else if($type == 'page_single') { |
| 111 |
$post_type = 'page'; |
| 112 |
} |
| 113 |
else if($type == 'product_single') { |
| 114 |
$post_type = 'product'; |
| 115 |
} |
| 116 |
$latest_cpt = get_posts( "post_type=$post_type&numberposts=1" ); |
| 117 |
|
| 118 |
return Plugin::$instance->editor->is_edit_mode() || isset($_GET['templately_library'], $_GET['preview_id']) ? $latest_cpt[0]->ID : get_the_ID(); |
| 119 |
} |
| 120 |
|
| 121 |
protected function render() { |
| 122 |
$settings = $this->get_settings_for_display(); |
| 123 |
$title = get_the_title( $this->get_dynamic_post_ID() ); |
| 124 |
|
| 125 |
$this->add_render_attribute( 'title', 'class', 'templately-heading-title' ); |
| 126 |
|
| 127 |
printf( '<%1$s %2$s>%3$s</%1$s>', Utils::validate_html_tag( $settings['html_tag'] ), $this->get_render_attribute_string( 'title' ), $title ); |
| 128 |
} |
| 129 |
} |
| 130 |
|