rajudhaka

Create a Elementor extension

Jul 23rd, 2019
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.64 KB | None | 0 0
  1. // create a folder to plugin directory then crate index.php and a folder name widgets in widgets create a file named slider.php
  2. // Paste this to index.php file
  3. <?php
  4. /**
  5.  * Plugin Name: Elementor Test Extension
  6.  * Description: Custom Elementor extension.
  7.  * Plugin URI:  https://elementor.com/
  8.  * Version:     1.0.0
  9.  * Author:      Elementor
  10.  * Author URI:  https://elementor.com/
  11.  * Text Domain: rrf-commerce
  12.  */
  13.  
  14. if ( ! defined( 'ABSPATH' ) ) {
  15.     exit; // Exit if accessed directly.
  16. }
  17.  
  18. /**
  19.  * Main Elementor Test Extension Class
  20.  *
  21.  * The main class that initiates and runs the plugin.
  22.  *
  23.  * @since 1.0.0
  24.  */
  25. final class RRF_Commerce_Extension {
  26.  
  27.     /**
  28.      * Plugin Version
  29.      *
  30.      * @since 1.0.0
  31.      *
  32.      * @var string The plugin version.
  33.      */
  34.     const VERSION = '1.0.0';
  35.  
  36.     /**
  37.      * Minimum Elementor Version
  38.      *
  39.      * @since 1.0.0
  40.      *
  41.      * @var string Minimum Elementor version required to run the plugin.
  42.      */
  43.     const MINIMUM_ELEMENTOR_VERSION = '2.0.0';
  44.  
  45.     /**
  46.      * Minimum PHP Version
  47.      *
  48.      * @since 1.0.0
  49.      *
  50.      * @var string Minimum PHP version required to run the plugin.
  51.      */
  52.     const MINIMUM_PHP_VERSION = '5.6';
  53.  
  54.     /**
  55.      * Instance
  56.      *
  57.      * @since 1.0.0
  58.      *
  59.      * @access private
  60.      * @static
  61.      *
  62.      * @var RRF_Commerce_Extension The single instance of the class.
  63.      */
  64.     private static $_instance = null;
  65.  
  66.     /**
  67.      * Instance
  68.      *
  69.      * Ensures only one instance of the class is loaded or can be loaded.
  70.      *
  71.      * @since 1.0.0
  72.      *
  73.      * @access public
  74.      * @static
  75.      *
  76.      * @return RRF_Commerce_Extension An instance of the class.
  77.      */
  78.     public static function instance() {
  79.  
  80.         if ( is_null( self::$_instance ) ) {
  81.             self::$_instance = new self();
  82.         }
  83.         return self::$_instance;
  84.  
  85.     }
  86.  
  87.     /**
  88.      * Constructor
  89.      *
  90.      * @since 1.0.0
  91.      *
  92.      * @access public
  93.      */
  94.     public function __construct() {
  95.  
  96.         add_action( 'init', [ $this, 'i18n' ] );
  97.         add_action( 'plugins_loaded', [ $this, 'init' ] );
  98.  
  99.     }
  100.  
  101.     /**
  102.      * Load Textdomain
  103.      *
  104.      * Load plugin localization files.
  105.      *
  106.      * Fired by `init` action hook.
  107.      *
  108.      * @since 1.0.0
  109.      *
  110.      * @access public
  111.      */
  112.     public function i18n() {
  113.  
  114.         load_plugin_textdomain( 'rrf-commerce' );
  115.  
  116.     }
  117.  
  118.     /**
  119.      * Initialize the plugin
  120.      *
  121.      * Load the plugin only after Elementor (and other plugins) are loaded.
  122.      * Checks for basic plugin requirements, if one check fail don't continue,
  123.      * if all check have passed load the files required to run the plugin.
  124.      *
  125.      * Fired by `plugins_loaded` action hook.
  126.      *
  127.      * @since 1.0.0
  128.      *
  129.      * @access public
  130.      */
  131.     public function init() {
  132.  
  133.         // Check if Elementor installed and activated
  134.         if ( ! did_action( 'elementor/loaded' ) ) {
  135.             add_action( 'admin_notices', [ $this, 'admin_notice_missing_main_plugin' ] );
  136.             return;
  137.         }
  138.  
  139.         // Check for required Elementor version
  140.         if ( ! version_compare( ELEMENTOR_VERSION, self::MINIMUM_ELEMENTOR_VERSION, '>=' ) ) {
  141.             add_action( 'admin_notices', [ $this, 'admin_notice_minimum_elementor_version' ] );
  142.             return;
  143.         }
  144.  
  145.         // Check for required PHP version
  146.         if ( version_compare( PHP_VERSION, self::MINIMUM_PHP_VERSION, '<' ) ) {
  147.             add_action( 'admin_notices', [ $this, 'admin_notice_minimum_php_version' ] );
  148.             return;
  149.         }
  150.  
  151.         // Add Plugin actions
  152.         add_action( 'elementor/widgets/widgets_registered', [ $this, 'init_widgets' ] );
  153.  
  154.         // activate widget style
  155.         add_action( 'elementor/frontend/after_enqueue_styles', [ $this, 'widget_styles' ] );
  156.     }
  157.  
  158.     /**
  159.      * Admin notice
  160.      *
  161.      * Warning when the site doesn't have Elementor installed or activated.
  162.      *
  163.      * @since 1.0.0
  164.      *
  165.      * @access public
  166.      */
  167.     public function admin_notice_missing_main_plugin() {
  168.  
  169.         if ( isset( $_GET['activate'] ) ) unset( $_GET['activate'] );
  170.  
  171.         $message = sprintf(
  172.             /* translators: 1: Plugin name 2: Elementor */
  173.             esc_html__( '"%1$s" requires "%2$s" to be installed and activated.', 'elementor-test-extension' ),
  174.             '<strong>' . esc_html__( 'Elementor Test Extension', 'elementor-test-extension' ) . '</strong>',
  175.             '<strong>' . esc_html__( 'Elementor', 'elementor-test-extension' ) . '</strong>'
  176.         );
  177.  
  178.         printf( '<div class="notice notice-warning is-dismissible"><p>%1$s</p></div>', $message );
  179.  
  180.     }
  181.  
  182.     /**
  183.      * Admin notice
  184.      *
  185.      * Warning when the site doesn't have a minimum required Elementor version.
  186.      *
  187.      * @since 1.0.0
  188.      *
  189.      * @access public
  190.      */
  191.     public function admin_notice_minimum_elementor_version() {
  192.  
  193.         if ( isset( $_GET['activate'] ) ) unset( $_GET['activate'] );
  194.  
  195.         $message = sprintf(
  196.             /* translators: 1: Plugin name 2: Elementor 3: Required Elementor version */
  197.             esc_html__( '"%1$s" requires "%2$s" version %3$s or greater.', 'elementor-test-extension' ),
  198.             '<strong>' . esc_html__( 'Elementor Test Extension', 'elementor-test-extension' ) . '</strong>',
  199.             '<strong>' . esc_html__( 'Elementor', 'elementor-test-extension' ) . '</strong>',
  200.              self::MINIMUM_ELEMENTOR_VERSION
  201.         );
  202.  
  203.         printf( '<div class="notice notice-warning is-dismissible"><p>%1$s</p></div>', $message );
  204.  
  205.     }
  206.  
  207.     /**
  208.      * Admin notice
  209.      *
  210.      * Warning when the site doesn't have a minimum required PHP version.
  211.      *
  212.      * @since 1.0.0
  213.      *
  214.      * @access public
  215.      */
  216.     public function admin_notice_minimum_php_version() {
  217.  
  218.         if ( isset( $_GET['activate'] ) ) unset( $_GET['activate'] );
  219.  
  220.         $message = sprintf(
  221.             /* translators: 1: Plugin name 2: PHP 3: Required PHP version */
  222.             esc_html__( '"%1$s" requires "%2$s" version %3$s or greater.', 'elementor-test-extension' ),
  223.             '<strong>' . esc_html__( 'Elementor Test Extension', 'elementor-test-extension' ) . '</strong>',
  224.             '<strong>' . esc_html__( 'PHP', 'elementor-test-extension' ) . '</strong>',
  225.              self::MINIMUM_PHP_VERSION
  226.         );
  227.  
  228.         printf( '<div class="notice notice-warning is-dismissible"><p>%1$s</p></div>', $message );
  229.  
  230.     }
  231.  
  232.     /**
  233.      * Init Widgets
  234.      *
  235.      * Include widgets files and register them
  236.      *
  237.      * @since 1.0.0
  238.      *
  239.      * @access public
  240.      */
  241.     public function init_widgets() {
  242.  
  243.         // Include Widget files
  244.         require_once( __DIR__ . '/widgets/slider.php' );
  245.         require_once( __DIR__ . '/widgets/content-block.php' );
  246.  
  247.         // Register widget
  248.         \Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \RRFCommerce_Slider_Widget() );
  249.         \Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \RRFCommerce_contentBlock_Widget() );
  250.  
  251.     }
  252.  
  253.     public function widget_styles() {
  254.  
  255.         wp_enqueue_style( 'rrfcommerce-slider', plugins_url( '/widgets/assets/css/slider.css', __FILE__ ) );
  256.         wp_enqueue_style( 'rrfcommerce-content-block', plugins_url( '/widgets/assets/css/content-box.css', __FILE__ ) );
  257.  
  258.     }
  259.  
  260. }
  261.  
  262. RRF_Commerce_Extension::instance();
  263.  
  264. function rrf_commerce_plugin_scripts() {
  265.     wp_enqueue_style( 'slick', plugins_url( 'assets/css/slick.css', __FILE__ ) );
  266.     wp_enqueue_script( 'slick', plugins_url( 'assets/js/slick.min.js',__FILE__ ), array('jquery'), '155454546', true );
  267. }
  268. add_action( 'wp_enqueue_scripts', 'rrf_commerce_plugin_scripts' );
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284. // paste this to slider.php file to widgets/slider.php file
  285. <?php
  286.  
  287. class RRFCommerce_Slider_Widget extends \Elementor\Widget_Base {
  288.  
  289.  
  290.     public function get_name() {
  291.         return 'rrfcommerce-slider';
  292.     }
  293.  
  294.     public function get_title() {
  295.         return __( 'RRFCommerce Slider', 'plugin-name' );
  296.     }
  297.  
  298.     public function get_icon() {
  299.         return 'fa fa-code';
  300.     }
  301.  
  302.     public function get_categories() {
  303.         return [ 'general' ];
  304.     }
  305.  
  306.     protected function _register_controls() {
  307.  
  308.         $this->start_controls_section(
  309.             'content_section',
  310.             [
  311.                 'label' => __( 'Content', 'plugin-name' ),
  312.                 'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
  313.             ]
  314.         );
  315.  
  316.         $repeater = new \Elementor\Repeater();
  317.  
  318.         $repeater->add_control(
  319.             'slide_title', [
  320.                 'label' => __( 'Title', 'plugin-domain' ),
  321.                 'type' => \Elementor\Controls_Manager::TEXT,
  322.                 'default' => __( 'Slide title' , 'plugin-domain' ),
  323.                 'label_block' => true,
  324.             ]
  325.         );
  326.  
  327.         $repeater->add_control(
  328.             'slide_content', [
  329.                 'label' => __( 'Content', 'plugin-domain' ),
  330.                 'type' => \Elementor\Controls_Manager::WYSIWYG,
  331.                 'default' => __( 'Slide Content' , 'plugin-domain' ),
  332.                 'show_label' => true,
  333.             ]
  334.         );
  335.  
  336.         $repeater->add_control(
  337.             'slide_desc',
  338.             [
  339.                 'label' => __( 'Slide description', 'plugin-domain' ),
  340.                 'type' => \Elementor\Controls_Manager::TEXT,
  341.                 'show_label' => true,
  342.             ]
  343.         );
  344.  
  345.         $repeater->add_control(
  346.             'slide_image',
  347.             [
  348.                 'label' => __( 'Slide image', 'plugin-domain' ),
  349.                 'type' => \Elementor\Controls_Manager::MEDIA,
  350.                 'show_label' => true,
  351.             ]
  352.         );
  353.  
  354.         $this->add_control(
  355.             'slides',
  356.             [
  357.                 'label' => __( 'Slides', 'plugin-domain' ),
  358.                 'type' => \Elementor\Controls_Manager::REPEATER,
  359.                 'fields' => $repeater->get_controls(),
  360.                 'default' => [
  361.                     [
  362.                         'list_title' => __( 'Slide #1', 'plugin-domain' ),
  363.                         'list_content' => __( 'Slide content', 'plugin-domain' ),
  364.                     ],
  365.                 ]
  366.             ]
  367.         );
  368.         $this->end_controls_section();
  369.  
  370.         $this->start_controls_section(
  371.             'setting_section',
  372.             [
  373.                 'label' => __( 'Slider Settings', 'plugin-name' ),
  374.                 'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
  375.             ]
  376.         );
  377.  
  378.         $this->add_control(
  379.             'fade',
  380.             [
  381.                 'label' => __( 'Fade effecct?', 'plugin-domain' ),
  382.                 'type' => \Elementor\Controls_Manager::SWITCHER,
  383.                 'label_on' => __( 'Yes', 'your-plugin' ),
  384.                 'label_off' => __( 'No', 'your-plugin' ),
  385.                 'return_value' => 'yes',
  386.                 'default' => 'no',
  387.             ]
  388.         );
  389.         $this->add_control(
  390.             'loop',
  391.             [
  392.                 'label' => __( 'Loop?', 'plugin-domain' ),
  393.                 'type' => \Elementor\Controls_Manager::SWITCHER,
  394.                 'label_on' => __( 'Yes', 'your-plugin' ),
  395.                 'label_off' => __( 'No', 'your-plugin' ),
  396.                 'return_value' => 'yes',
  397.                 'default' => 'yes',
  398.             ]
  399.         );
  400.         $this->add_control(
  401.             'arrows',
  402.             [
  403.                 'label' => __( 'Show arrows?', 'plugin-domain' ),
  404.                 'type' => \Elementor\Controls_Manager::SWITCHER,
  405.                 'label_on' => __( 'Show', 'your-plugin' ),
  406.                 'label_off' => __( 'Hide', 'your-plugin' ),
  407.                 'return_value' => 'yes',
  408.                 'default' => 'yes',
  409.             ]
  410.         );
  411.  
  412.         $this->add_control(
  413.             'dots',
  414.             [
  415.                 'label' => __( 'Show dots?', 'plugin-domain' ),
  416.                 'type' => \Elementor\Controls_Manager::SWITCHER,
  417.                 'label_on' => __( 'Show', 'your-plugin' ),
  418.                 'label_off' => __( 'Hide', 'your-plugin' ),
  419.                 'return_value' => 'yes',
  420.                 'default' => 'yes',
  421.             ]
  422.         );
  423.  
  424.         $this->add_control(
  425.             'autoplay',
  426.             [
  427.                 'label' => __( 'Autoplay?', 'plugin-domain' ),
  428.                 'type' => \Elementor\Controls_Manager::SWITCHER,
  429.                 'label_on' => __( 'Yes', 'your-plugin' ),
  430.                 'label_off' => __( 'No', 'your-plugin' ),
  431.                 'return_value' => 'yes',
  432.                 'default' => 'yes',
  433.             ]
  434.         );
  435.  
  436.         $this->add_control(
  437.             'autoplay_time',
  438.             [
  439.                 'label' => __( 'Autoplay Time', 'plugin-domain' ),
  440.                 'type' => \Elementor\Controls_Manager::TEXT,
  441.                 'default' => '5000',
  442.                 'condition' => [
  443.                     'autoplay' => 'yes',
  444.                 ],
  445.             ]
  446.         );
  447.  
  448.         $this->end_controls_section();
  449.  
  450.     }
  451.  
  452.     protected function render() {
  453.  
  454.         $settings = $this->get_settings_for_display();
  455.  
  456.         if($settings['slides']) {
  457.             $dynamic_id = rand(78676, 967698);
  458.             if(count($settings['slides']) > 1) {
  459.                 if($settings['fade'] == 'yes') {
  460.                     $fade = 'true';
  461.                 } else {
  462.                     $fade = 'false';
  463.                 }
  464.                 if($settings['arrows'] == 'yes') {
  465.                     $arrows = 'true';
  466.                 } else {
  467.                     $arrows = 'false';
  468.                 }
  469.                 if($settings['dots'] == 'yes') {
  470.                     $dots = 'true';
  471.                 } else {
  472.                     $dots = 'false';
  473.                 }
  474.                 if($settings['autoplay'] == 'yes') {
  475.                     $autoplay = 'true';
  476.                 } else {
  477.                     $autoplay = 'false';
  478.                 }
  479.                 if($settings['loop'] == 'yes') {
  480.                     $loop = 'true';
  481.                 } else {
  482.                     $loop = 'false';
  483.                 }
  484.                 echo '<script>
  485.                     jQuery(document).ready(function($) {
  486.                         $("#slides-'.$dynamic_id.'").slick({
  487.                             arrows: '.$arrows.',
  488.                             prevArrow: "<i class=\'fa fa-angle-left\'></i>",
  489.                             nextArrow: "<i class=\'fa fa-angle-right\'></i>",
  490.                             dots: '.$dots.',
  491.                             fade: '.$fade.',
  492.                             autoplay: '.$autoplay.',
  493.                             loop: '.$loop.',';
  494.  
  495.                             if($autoplay == 'true') {
  496.                                 echo 'autoplaySpeed: '.$settings['autoplay_time'].'';
  497.                             }
  498.                            
  499.  
  500.                             echo '
  501.                         });
  502.                     });
  503.                 </script>';
  504.             }
  505.             echo '<div id="slides-'.$dynamic_id.'" class="slides">';
  506.             foreach($settings['slides'] as $slide) {
  507.                 echo '<div class="single-slide-item" style="background-image:url('.wp_get_attachment_image_url($slide['slide_image']['id'], 'large').')">
  508.                     <div class="row">
  509.                         <div class="col my-auto">
  510.                             '.wpautop($slide['slide_content']).'
  511.                         </div>
  512.                     </div>
  513.                     <div class="slide-info">
  514.                         <h4>'.$slide['slide_title'].'</h4>
  515.                         '.$slide['slide_desc'].'
  516.                     </div>
  517.                 </div>';
  518.             }
  519.             echo '</div>';
  520.         }
  521.        
  522.  
  523.     }
  524.  
  525. }
Advertisement
Add Comment
Please, Sign In to add comment