-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtailor-extension.php
executable file
·376 lines (324 loc) · 9.5 KB
/
tailor-extension.php
1
<?php/** * Plugin Name: Tailor - Typography extension * Plugin URI: http://www.gettailor.com * Description: Typography Tailor extension. * Version: 1.1.7 * Author: Ivan Karatayev * Author URI: * Text Domain: typography-tailor-extension */if ( ! defined('WPINC')) { die;}if ( ! class_exists('Tailor_Extension')) { /** * Tailor Extension class. */ class Tailor_Extension { /** * Tailor Extension instance. * * @access private * @var Tailor_Extension */ private static $instance; /** * The plugin version number. * * @access private * @var string */ private static $version; /** * The plugin basename. * * @access private * @var string */ private static $plugin_basename; /** * The plugin name. * * @access private * @var string */ private static $plugin_name; /** * The plugin directory. * * @access private * @var string */ private static $plugin_dir; /** * The plugin URL. * * @access private * @var string */ private static $plugin_url; /** * Returns the Tailor Extension instance. * * @return Tailor_Extension */ public static function instance() { if (is_null(self::$instance)) { self::$instance = new self(); } return self::$instance; } /** * Constructor. */ public function __construct() { $plugin_data = get_file_data(__FILE__, array('Plugin Name', 'Version')); self::$plugin_basename = plugin_basename(__FILE__); self::$plugin_name = array_shift($plugin_data); self::$version = array_shift($plugin_data); self::$plugin_dir = trailingslashit(plugin_dir_path(__FILE__)); self::$plugin_url = trailingslashit(plugin_dir_url(__FILE__)); add_action('plugins_loaded', array($this, 'init')); } /** * Initializes the plugin. */ public function init() { if ( ! class_exists('Tailor') || // Tailor is not active, or ! version_compare(tailor()->version(), '1.7.0', '>=') // An unsupported version is being used ) { add_action('admin_notices', array($this, 'display_version_notice')); return; } $this->add_actions(); $this->includes(); } /** * Displays an admin notice if an unsupported version of Tailor is being used. * * @since 1.1.0 */ public function display_version_notice() { printf( '<div class="notice notice-warning is-dismissible"><p>%s</p></div>', __('Please ensure that Tailor 1.7.0 (or newer) is active to use the sample extension.') ); } /** * Includes required plugin files. * * @access protected */ protected function includes() { } /** * Adds required action hooks. * * @access protected */ protected function add_actions() { // Load element definitions add_action('tailor_load_elements', array($this, 'load_elements'), 20); // Register custom elements add_action('tailor_register_elements', array($this, 'register_elements'), 99); // Register custom template partials director add_filter('tailor_plugin_partial_paths', array($this, 'register_partial_path')); // Enqueue scripts and styles add_filter('tailor_enqueue_sidebar_styles', array($this, 'add_sidebar_styles')); add_filter('tailor_enqueue_canvas_styles', array($this, 'add_canvas_styles')); add_action('wp_enqueue_scripts', array($this, 'enqueue_styles')); add_action('wp_enqueue_scripts', array($this, 'enqueue_frontend_scripts')); add_action('tailor_canvas_enqueue_scripts', array($this, 'enqueue_scripts'), 99); add_action('tailor_enqueue_sidebar_scripts', array($this, 'enqueue_sidebar_scripts')); } /** * Loads and registers the new Tailor elements and shortcodes. */ public function load_elements() { require_once $this->plugin_dir() . 'includes/controls/class-typography-group.php'; require_once $this->plugin_dir() . 'includes/helpers-typography.php'; } /* * Loads sidebar styles * */ public function add_sidebar_styles() { wp_enqueue_style( 'tailor-typography-sidebar-styles', $this->plugin_url() . 'assets/css/dist/sidebar.min.css', array(), $this->version() ); } /* * Loads sidebar styles * */ public function add_canvas_styles() { wp_enqueue_style( 'tailor-typography-canvas-styles', $this->plugin_url() . 'assets/css/dist/canvas.min.css', array(), $this->version() ); } /** * Loads and registers the new Tailor elements and shortcodes. * * @param $element_manager Tailor_Elements */ public function register_elements($element_manager) { } /** * Enqueues frontend styles. */ public function enqueue_styles() { } /** * Enqueues canvas scripts. */ public function enqueue_scripts() { wp_enqueue_script( 'tailor-custom-canvas', $this->plugin_url() . 'assets/js/dist/canvas.min.js', array('tailor-canvas'), $this->version(), true ); wp_localize_script('tailor-custom-canvas', 'typography_google_fonts', $this->get_typography_google_fonts()); } /** * Enqueues sidebar scripts. */ public function enqueue_sidebar_scripts() { wp_enqueue_script( 'tailor-custom-sidebar', $this->plugin_url() . 'assets/js/dist/sidebar.min.js', array('tailor-sidebar'), $this->version(), true ); } /** * Enqueues frontend scripts. */ public function enqueue_frontend_scripts() { wp_enqueue_script( 'tailor-typography-frontend', $this->plugin_url() . 'assets/js/dist/frontend.min.js', array('tailor-frontend'), $this->version(), true ); wp_localize_script('tailor-typography-frontend', 'typography_google_fonts', $this->get_typography_google_fonts()); } /** * @return array of google fonts */ private function get_typography_google_fonts() { $google_fonts = typo_get_google_fonts(); $fonts = array(); if (isset($google_fonts->items)) { foreach ($google_fonts->items as $item) { if (isset($item->files->regular)) { $fonts[] = array( 'name' => $item->family, 'src' => $item->files->regular ); } } } return $fonts; } /** * Registers the partial directory for this extension plugin. * * @param $paths * * @return array */ public function register_partial_path($paths) { $paths[] = $this->plugin_dir() . 'partials/'; return $paths; } /** * Returns the version number of the plugin. * * @return string */ public function version() { return self::$version; } /** * Returns the plugin basename. * * @return string */ public function plugin_basename() { return self::$plugin_basename; } /** * Returns the plugin name. * * @return string */ public function plugin_name() { return self::$plugin_name; } /** * Returns the plugin directory. * * @return string */ public function plugin_dir() { return self::$plugin_dir; } /** * Returns the plugin URL. * * @return string */ public function plugin_url() { return self::$plugin_url; } }}if ( ! function_exists('tailor_extension')) { /** * Returns the Tailor Extension instance. * * @return Tailor_Extension */ function tailor_extension() { return Tailor_Extension::instance(); }}/** * Initializes the Tailor Extension. */tailor_extension();