| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; |
| 4 |
} |
| 5 |
|
| 6 |
if(!class_exists('animated_headlines_settings')) |
| 7 |
{ |
| 8 |
class animated_headlines_settings |
| 9 |
{ |
| 10 |
private $elementor_widget_registered = false; |
| 11 |
|
| 12 |
public function __construct() |
| 13 |
{ |
| 14 |
add_action('init', array($this, 'register_assets'), 5); |
| 15 |
add_action('init', array($this, 'register_gutenberg_block')); |
| 16 |
add_action('admin_menu', array($this, 'add_menu')); |
| 17 |
add_action('wp_enqueue_scripts', array($this, 'enqueue_assets')); |
| 18 |
add_action('admin_enqueue_scripts', array($this, 'admin_assets')); |
| 19 |
add_action('vc_before_init', array($this, 'register_wpbakery_element')); |
| 20 |
add_action('elementor/widgets/register', array($this, 'register_elementor_widget')); |
| 21 |
add_action('elementor/widgets/widgets_registered', array($this, 'register_elementor_widget')); |
| 22 |
|
| 23 |
add_shortcode('animated-headline', array($this, 'render_shortcode')); |
| 24 |
add_filter('widget_text', 'shortcode_unautop'); |
| 25 |
add_filter('widget_text', 'do_shortcode'); |
| 26 |
} |
| 27 |
|
| 28 |
/* ------------------------------------------------------- |
| 29 |
* ANIMATION OPTIONS |
| 30 |
* ----------------------------------------------------- */ |
| 31 |
public static function get_animation_options() |
| 32 |
{ |
| 33 |
return array( |
| 34 |
'rotate-1' => 'Rotate 1', |
| 35 |
'rotate-2' => 'Rotate 2', |
| 36 |
'rotate-3' => 'Rotate 3', |
| 37 |
'type' => 'Type', |
| 38 |
'scale' => 'Scale', |
| 39 |
'loading-bar' => 'Loading Bar', |
| 40 |
'slide' => 'Slide', |
| 41 |
'clip' => 'Clip', |
| 42 |
'zoom' => 'Zoom', |
| 43 |
'push' => 'Push', |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
/* ------------------------------------------------------- |
| 48 |
* REGISTER (not enqueue) CSS + JS globally |
| 49 |
* Gutenberg block style/view_script uses these handles |
| 50 |
* ----------------------------------------------------- */ |
| 51 |
public function register_assets() |
| 52 |
{ |
| 53 |
wp_register_style( |
| 54 |
'animated-headline', |
| 55 |
AH_URL . 'css/style.css', |
| 56 |
array(), |
| 57 |
AH_VERSION |
| 58 |
); |
| 59 |
wp_register_script( |
| 60 |
'animated-headline', |
| 61 |
AH_URL . 'js/main.js', |
| 62 |
array('jquery'), |
| 63 |
AH_VERSION, |
| 64 |
true |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
/* ------------------------------------------------------- |
| 69 |
* PERFORMANCE: Only enqueue on pages that need it |
| 70 |
* ----------------------------------------------------- */ |
| 71 |
public function enqueue_assets() |
| 72 |
{ |
| 73 |
global $post; |
| 74 |
$load = false; |
| 75 |
|
| 76 |
if(is_singular() && is_a($post, 'WP_Post')){ |
| 77 |
// Shortcode check |
| 78 |
if(has_shortcode($post->post_content, 'animated-headline')){ |
| 79 |
$load = true; |
| 80 |
} |
| 81 |
// Gutenberg block check |
| 82 |
if(!$load && function_exists('has_block') && has_block('animated-headline/headline', $post)){ |
| 83 |
$load = true; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
// Allow themes/plugins to force-load assets |
| 88 |
$load = apply_filters('animated_headline_load_assets', $load); |
| 89 |
|
| 90 |
if(!$load) return; |
| 91 |
|
| 92 |
wp_enqueue_style('animated-headline'); |
| 93 |
wp_enqueue_script('animated-headline'); |
| 94 |
} |
| 95 |
|
| 96 |
/* ------------------------------------------------------- |
| 97 |
* ADMIN ASSETS: Only on settings page |
| 98 |
* ----------------------------------------------------- */ |
| 99 |
public function admin_assets($hook) |
| 100 |
{ |
| 101 |
if('settings_page_animated-headlines' !== $hook) return; |
| 102 |
|
| 103 |
wp_enqueue_style('animated-headline', AH_URL . 'css/style.css', array(), AH_VERSION); |
| 104 |
wp_enqueue_script('animated-headline', AH_URL . 'js/main.js', array('jquery'), AH_VERSION, true); |
| 105 |
wp_enqueue_style('animated-headline-admin', AH_URL . 'css/admin.css', array(), AH_VERSION); |
| 106 |
wp_enqueue_script('animated-headline-admin', AH_URL . 'js/admin.js', array('jquery', 'animated-headline'), AH_VERSION, true); |
| 107 |
} |
| 108 |
|
| 109 |
/* ------------------------------------------------------- |
| 110 |
* ADMIN MENU |
| 111 |
* ----------------------------------------------------- */ |
| 112 |
public function add_menu() |
| 113 |
{ |
| 114 |
add_options_page( |
| 115 |
'Animated Headline', |
| 116 |
'Animated Headline', |
| 117 |
'manage_options', |
| 118 |
'animated-headlines', |
| 119 |
array($this, 'plugin_detail_page') |
| 120 |
); |
| 121 |
} |
| 122 |
|
| 123 |
public function plugin_detail_page() |
| 124 |
{ |
| 125 |
if(!current_user_can('manage_options')){ |
| 126 |
wp_die(__('You do not have sufficient permissions to access this page.', 'animated-headline')); |
| 127 |
} |
| 128 |
$animations = self::get_animation_options(); |
| 129 |
include(sprintf('%s/inc/settings.php', dirname(__FILE__))); |
| 130 |
} |
| 131 |
|
| 132 |
/* ------------------------------------------------------- |
| 133 |
* GUTENBERG BLOCK |
| 134 |
* ----------------------------------------------------- */ |
| 135 |
public function register_gutenberg_block() |
| 136 |
{ |
| 137 |
if(!function_exists('register_block_type')) return; |
| 138 |
|
| 139 |
wp_register_script( |
| 140 |
'animated-headline-block', |
| 141 |
AH_URL . 'js/block.js', |
| 142 |
array('wp-blocks', 'wp-element', 'wp-components', 'wp-block-editor', 'wp-i18n'), |
| 143 |
AH_VERSION, |
| 144 |
true |
| 145 |
); |
| 146 |
|
| 147 |
wp_localize_script('animated-headline-block', 'AnimatedHeadlineBlock', array( |
| 148 |
'animations' => self::get_animation_options(), |
| 149 |
)); |
| 150 |
|
| 151 |
register_block_type('animated-headline/headline', array( |
| 152 |
'editor_script' => 'animated-headline-block', |
| 153 |
'style' => 'animated-headline', |
| 154 |
'render_callback' => array($this, 'render_shortcode'), |
| 155 |
'attributes' => array( |
| 156 |
'title' => array('type' => 'string', 'default' => 'Hello my friend'), |
| 157 |
'animated_text' => array('type' => 'string', 'default' => 'Awesome,Amazing,Cool'), |
| 158 |
'animation' => array('type' => 'string', 'default' => 'rotate-1'), |
| 159 |
'tag' => array('type' => 'string', 'default' => 'h1'), |
| 160 |
'delay' => array('type' => 'string', 'default' => ''), |
| 161 |
'speed' => array('type' => 'string', 'default' => ''), |
| 162 |
), |
| 163 |
)); |
| 164 |
} |
| 165 |
|
| 166 |
/* ------------------------------------------------------- |
| 167 |
* ELEMENTOR WIDGET |
| 168 |
* ----------------------------------------------------- */ |
| 169 |
public function register_elementor_widget($widgets_manager = null) |
| 170 |
{ |
| 171 |
if($this->elementor_widget_registered) return; |
| 172 |
if(!did_action('elementor/loaded') || !class_exists('Elementor\Widget_Base')) return; |
| 173 |
|
| 174 |
$this->elementor_widget_registered = true; |
| 175 |
require_once AH_PATH . 'inc/class-elementor-widget.php'; |
| 176 |
$widget = new Animated_Headline_Elementor_Widget(); |
| 177 |
|
| 178 |
if($widgets_manager && method_exists($widgets_manager, 'register')){ |
| 179 |
$widgets_manager->register($widget); |
| 180 |
} elseif(isset(\Elementor\Plugin::$instance->widgets_manager) && method_exists(\Elementor\Plugin::$instance->widgets_manager, 'register_widget_type')){ |
| 181 |
\Elementor\Plugin::$instance->widgets_manager->register_widget_type($widget); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
/* ------------------------------------------------------- |
| 186 |
* WPBAKERY ELEMENT |
| 187 |
* ----------------------------------------------------- */ |
| 188 |
public function register_wpbakery_element() |
| 189 |
{ |
| 190 |
if(!function_exists('vc_map')) return; |
| 191 |
|
| 192 |
$anim_values = array(); |
| 193 |
foreach(self::get_animation_options() as $val => $label){ |
| 194 |
$anim_values[$label] = $val; |
| 195 |
} |
| 196 |
|
| 197 |
vc_map(array( |
| 198 |
'name' => __('Animated Headline', 'animated-headline'), |
| 199 |
'base' => 'animated-headline', |
| 200 |
'category' => __('Content', 'animated-headline'), |
| 201 |
'icon' => 'icon-wpb-ui-custom_heading', |
| 202 |
'params' => array( |
| 203 |
array( |
| 204 |
'type' => 'textfield', |
| 205 |
'heading' => __('Title', 'animated-headline'), |
| 206 |
'param_name' => 'title', |
| 207 |
), |
| 208 |
array( |
| 209 |
'type' => 'textfield', |
| 210 |
'heading' => __('Animated Text', 'animated-headline'), |
| 211 |
'param_name' => 'animated_text', |
| 212 |
'description' => __('Comma separated words e.g. Awesome,Amazing,Cool', 'animated-headline'), |
| 213 |
), |
| 214 |
array( |
| 215 |
'type' => 'dropdown', |
| 216 |
'heading' => __('Animation', 'animated-headline'), |
| 217 |
'param_name' => 'animation', |
| 218 |
'value' => $anim_values, |
| 219 |
'std' => 'rotate-1', |
| 220 |
), |
| 221 |
array( |
| 222 |
'type' => 'dropdown', |
| 223 |
'heading' => __('HTML Tag', 'animated-headline'), |
| 224 |
'param_name' => 'tag', |
| 225 |
'value' => array('H1'=>'h1','H2'=>'h2','H3'=>'h3','H4'=>'h4','H5'=>'h5','H6'=>'h6','DIV'=>'div'), |
| 226 |
'std' => 'h1', |
| 227 |
), |
| 228 |
array( |
| 229 |
'type' => 'textfield', |
| 230 |
'heading' => __('Delay (ms)', 'animated-headline'), |
| 231 |
'param_name' => 'delay', |
| 232 |
'description' => __('Time between word changes. Default: 2500', 'animated-headline'), |
| 233 |
), |
| 234 |
array( |
| 235 |
'type' => 'textfield', |
| 236 |
'heading' => __('Speed (ms)', 'animated-headline'), |
| 237 |
'param_name' => 'speed', |
| 238 |
'description' => __('Animation speed in ms. Default: 600', 'animated-headline'), |
| 239 |
), |
| 240 |
), |
| 241 |
)); |
| 242 |
} |
| 243 |
|
| 244 |
/* ------------------------------------------------------- |
| 245 |
* SHORTCODE RENDER (backward compatible) |
| 246 |
* ----------------------------------------------------- */ |
| 247 |
public function render_shortcode($atts) |
| 248 |
{ |
| 249 |
$atts = shortcode_atts( |
| 250 |
array( |
| 251 |
'title' => '', |
| 252 |
'animation' => 'rotate-1', |
| 253 |
'animated_text' => '', |
| 254 |
'tag' => 'h1', |
| 255 |
'delay' => '', |
| 256 |
'speed' => '', |
| 257 |
), |
| 258 |
$atts, |
| 259 |
'animated-headline' |
| 260 |
); |
| 261 |
|
| 262 |
$allowed = array_keys(self::get_animation_options()); |
| 263 |
$title = sanitize_text_field($atts['title']); |
| 264 |
$anim = sanitize_key($atts['animation']); |
| 265 |
|
| 266 |
if(!in_array($anim, $allowed, true)) $anim = 'rotate-1'; |
| 267 |
|
| 268 |
$words = $this->parse_animated_text($atts['animated_text']); |
| 269 |
|
| 270 |
// Add letters class for letter-by-letter animations |
| 271 |
if(in_array($anim, array('rotate-2','rotate-3','type','scale'), true)){ |
| 272 |
$anim .= ' letters'; |
| 273 |
} |
| 274 |
|
| 275 |
$tag = strtolower(sanitize_key($atts['tag'])); |
| 276 |
if(!in_array($tag, array('h1','h2','h3','h4','h5','h6','div'), true)) $tag = 'h1'; |
| 277 |
|
| 278 |
$data = ''; |
| 279 |
$delay = absint($atts['delay']); |
| 280 |
$speed = absint($atts['speed']); |
| 281 |
if($delay > 0) $data .= ' data-delay="' . esc_attr($delay) . '"'; |
| 282 |
if($speed > 0) $data .= ' data-speed="' . esc_attr($speed) . '"'; |
| 283 |
|
| 284 |
$out = '<' . tag_escape($tag) . ' class="cd-headline ' . esc_attr($anim) . '"' . $data . '>'; |
| 285 |
$out .= '<span> ' . esc_html($title) . ' </span> '; |
| 286 |
$out .= '<span class="cd-words-wrapper">'; |
| 287 |
foreach($words as $i => $word){ |
| 288 |
$cls = ($i === 0) ? 'is-visible' : ''; |
| 289 |
$out .= '<b class="' . esc_attr($cls) . '">' . esc_html($word) . '</b>' . "\n"; |
| 290 |
} |
| 291 |
$out .= '</span></' . tag_escape($tag) . '>'; |
| 292 |
|
| 293 |
return $out; |
| 294 |
} |
| 295 |
|
| 296 |
private function parse_animated_text($input) |
| 297 |
{ |
| 298 |
$parts = explode(',', (string)$input); |
| 299 |
$parts = array_map('sanitize_text_field', $parts); |
| 300 |
$parts = array_map('trim', $parts); |
| 301 |
return array_values(array_filter($parts, 'strlen')); |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
|