Last active
June 1, 2020 21:35
-
-
Save Rupashdas/6aef1f442aa0d9c0e5b6563d7c5df6a0 to your computer and use it in GitHub Desktop.
Boilerplate Extension for creating new widget plugin for elementor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Timer element for elementor | |
Plugin URI: https://devrupash.com | |
Description: Timer element for elementor | |
Version: 1.0 | |
Author: Rupash Das | |
Author URI: https://devrupash.com | |
License: GPLV2 or later | |
Text Domain: timerelement | |
Domain Path: /Languages/ | |
*/ | |
if ( !defined( 'ABSPATH' ) ) { | |
die( __( "Direct Access is not allowd", "timerelement" ) ); | |
} | |
final class TimerElementExtension { | |
const VERSION = '1.0'; | |
const MINIMUM_ELEMENTOR_VERSION = '2.0.0'; | |
const MINIMUM_PHP_VERSION = '5.4'; | |
private static $_instance = null; | |
public static function instance() { | |
if ( is_null( self::$_instance ) ) { | |
self::$_instance = new self(); | |
} | |
return self::$_instance; | |
} | |
public function __construct() { | |
add_action( 'plugins_loaded', [$this, 'init'] ); | |
} | |
public function init() { | |
load_plugin_textdomain( 'timerelement' ); | |
// Check if Elementor installed and activated | |
if ( !did_action( 'elementor/loaded' ) ) { | |
add_action( 'admin_notices', [$this, 'admin_notice_missing_main_plugin'] ); | |
return; | |
} | |
// Check for required Elementor version | |
if ( ! version_compare( ELEMENTOR_VERSION, self::MINIMUM_ELEMENTOR_VERSION, '>=' ) ) { | |
add_action( 'admin_notices', [ $this, 'admin_notice_minimum_elementor_version' ] ); | |
return; | |
} | |
// Check for required PHP version | |
if ( version_compare( PHP_VERSION, self::MINIMUM_PHP_VERSION, '<' ) ) { | |
add_action( 'admin_notices', [ $this, 'admin_notice_minimum_php_version' ] ); | |
return; | |
} | |
// Add Plugin actions | |
add_action( 'elementor/widgets/widgets_registered', [ $this, 'init_widgets' ] ); | |
//hook for enqueue CSS in elementor frontend mode | |
add_action("elementor/frontend/after_enqueue_styles", [$this, 'widget_styles']); | |
//hook for enqueue js in elementor editor mode | |
add_action("elementor/editor/after_enqueue_scripts", [$this, 'pricing_editor_assets']); | |
//hook for enqueue js in elementor frontend mode | |
add_action("elementor/frontend/after_enqueue_scripts", [$this, 'progressbar_asets']); | |
//Register new widget category | |
add_action( 'elementor/elements/categories_registered', [$this, 'add_elementor_widget_categories'] ); | |
} | |
function progressbar_asets(){ | |
//Enqueue js in editor mode | |
wp_enqueue_script("progressbar-js", plugins_url("/assets/js/progressbar.min.js",__FILE__), array('jquery'), time(), true); | |
} | |
function pricing_editor_assets(){ | |
//Enqueue js | |
wp_enqueue_script("pricing-editor-js", plugins_url("/assets/js/main.js",__FILE__), array('jquery'), '1.0', true); | |
} | |
function widget_styles(){ | |
//Enqueue css | |
wp_enqueue_style("froala-css", "//cdnjs.cloudflare.com/ajax/libs/froala-design-blocks/2.0.1/css/froala_blocks.min.css"); | |
} | |
//Custom widget register | |
public function add_elementor_widget_categories( $elements_manager ) { | |
$elements_manager->add_category( | |
'custom', | |
[ | |
'title' => __( 'Custom', 'timerelement' ), | |
'icon' => 'fa fa-plug', | |
] | |
); | |
} | |
public function admin_notice_missing_main_plugin() { | |
if ( isset( $_GET['activate'] ) ) { | |
unset( $_GET['activate'] ); | |
} | |
$message = sprintf( | |
/* translators: 1: Plugin name 2: Elementor */ | |
esc_html__( '"%1$s" requires "%2$s" to be installed and activated.', 'timerelement' ), | |
'<strong>' . esc_html__( 'Elementor Test Extension', 'timerelement' ) . '</strong>', | |
'<strong>' . esc_html__( 'Elementor', 'timerelement' ) . '</strong>' | |
); | |
printf( '<div class="notice notice-warning is-dismissible"><p>%1$s</p></div>', $message ); | |
} | |
public function admin_notice_minimum_elementor_version() { | |
if ( isset( $_GET['activate'] ) ) unset( $_GET['activate'] ); | |
$message = sprintf( | |
/* translators: 1: Plugin name 2: Elementor 3: Required Elementor version */ | |
esc_html__( '"%1$s" requires "%2$s" version %3$s or greater.', 'timerelement' ), | |
'<strong>' . esc_html__( 'Elementor Test Extension', 'timerelement' ) . '</strong>', | |
'<strong>' . esc_html__( 'Elementor', 'timerelement' ) . '</strong>', | |
self::MINIMUM_ELEMENTOR_VERSION | |
); | |
printf( '<div class="notice notice-warning is-dismissible"><p>%1$s</p></div>', $message ); | |
} | |
public function admin_notice_minimum_php_version() { | |
if ( isset( $_GET['activate'] ) ) unset( $_GET['activate'] ); | |
$message = sprintf( | |
/* translators: 1: Plugin name 2: PHP 3: Required PHP version */ | |
esc_html__( '"%1$s" requires "%2$s" version %3$s or greater.', 'timerelement' ), | |
'<strong>' . esc_html__( 'Elementor Test Extension', 'timerelement' ) . '</strong>', | |
'<strong>' . esc_html__( 'PHP', 'timerelement' ) . '</strong>', | |
self::MINIMUM_PHP_VERSION | |
); | |
printf( '<div class="notice notice-warning is-dismissible"><p>%1$s</p></div>', $message ); | |
} | |
public function init_widgets() { | |
//All widget file will go here | |
require_once( __DIR__ . '/widgets/progressbar-widget.php' ); | |
//instance all widget classes | |
\Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \Elementor_Progressbar_Widget() ); | |
} | |
} | |
TimerElementExtension::instance(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment