Advertisement
adekherry

ModuleElementor.php

Sep 12th, 2018
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.29 KB | None | 0 0
  1. <?php
  2. /**
  3. * @author : Jegtheme
  4. */
  5. namespace JNews\Elementor;
  6.  
  7. use Elementor\Plugin;
  8. use Elementor\Element_Column;
  9. use Elementor\Controls_Manager;
  10. use JNews\Elementor\Normal\SocialIcon;
  11. use JNews\Elementor\Normal\SocialCounter;
  12. use JNews\Module\ModuleManager;
  13. use JNews\Widget\Normal\RegisterNormalWidget;
  14.  
  15. /**
  16. * Class JNews VC Integration
  17. */
  18. Class ModuleElementor
  19. {
  20. /**
  21. * @var ModuleElementor
  22. */
  23. private static $instance;
  24.  
  25. private $module_manager;
  26.  
  27. /**
  28. * @return ModuleElementor
  29. */
  30. public static function getInstance()
  31. {
  32. if (null === static::$instance)
  33. {
  34. static::$instance = new static();
  35. }
  36. return static::$instance;
  37. }
  38.  
  39. /**
  40. * ModuleVC constructor.
  41. */
  42. private function __construct()
  43. {
  44. $this->setup_hook();
  45. $this->setup_post_type();
  46. }
  47.  
  48. protected function get_module_instance()
  49. {
  50. if ( ! $this->module_manager )
  51. {
  52. $this->module_manager = ModuleManager::getInstance();
  53. }
  54.  
  55. return $this->module_manager;
  56. }
  57.  
  58. protected function setup_post_type()
  59. {
  60. add_post_type_support('footer', 'elementor');
  61. add_post_type_support('custom-post-template', 'elementor');
  62. add_post_type_support('custom-mega-menu', 'elementor');
  63. }
  64.  
  65. public function setup_hook()
  66. {
  67. // load script & style editor
  68. add_action( 'elementor/editor/after_enqueue_styles', array( $this, 'editor_style' ) );
  69. add_action( 'elementor/editor/after_enqueue_scripts', array( $this, 'editor_script') );
  70.  
  71. // load script & style frontend
  72. add_action( 'elementor/frontend/after_enqueue_scripts', array( $this, 'frontend_script') );
  73.  
  74. // register module, element, group & custom control
  75. add_action( 'elementor/controls/controls_registered', array( $this, 'register_control') );
  76. add_action( 'elementor/widgets/widgets_registered', array( $this, 'register_module') );
  77. add_action( 'elementor/widgets/widgets_registered', array( $this, 'register_normal') );
  78. add_action( 'elementor/init', array( $this, 'register_group' ) );
  79.  
  80. // register sticky sidebar option on column
  81. add_action( 'elementor/element/column/layout/before_section_end', array( $this, 'register_sticky_option' ) );
  82. add_action( 'elementor/element/column/section_advanced/before_section_end', array( $this, 'register_column_padding' ) );
  83.  
  84. // register custom css option for single page / post
  85. add_action( 'elementor/element/page-settings/section_page_settings/before_section_end', array( $this, 'register_custom_css_option' ) );
  86. add_action( 'wp_head', array( $this, 'render_custom_css' ) );
  87.  
  88. // modify widget list on elementor
  89. add_action('jnews_get_normal_widget_list', array($this, 'modify_normal_widget_list'));
  90.  
  91. // check width of section
  92. add_action('elementor/frontend/before_render', array($this, 'register_width'));
  93. add_action('elementor/frontend/element/before_render', array($this, 'register_width'));
  94. add_action('elementor/editor/element/before_raw_data', array($this, 'register_width'));
  95. add_action('elementor/init', array($this, 'register_element'));
  96.  
  97. // change elementor script
  98. add_action( 'elementor/editor/before_enqueue_scripts', array($this, 'elementor_editor_script'), 99 );
  99.  
  100. add_action( 'wp_ajax_elementor_get_category', array($this, 'elementor_get_category'));
  101. add_action( 'wp_ajax_elementor_get_author', array($this, 'elementor_get_author'));
  102. add_action( 'wp_ajax_elementor_get_tag', array($this, 'elementor_get_tag'));
  103. }
  104.  
  105. public function elementor_get_tag()
  106. {
  107. if ( isset( $_REQUEST[ 'string' ] ) && ! empty( $_REQUEST[ 'string' ] ) )
  108. {
  109. $value = $_REQUEST[ 'string' ];
  110. } else {
  111. return false;
  112. }
  113.  
  114. $data = array();
  115. $values = explode(',', $value);
  116.  
  117. foreach( $values as $val )
  118. {
  119. if ( ! empty( $val ) )
  120. {
  121. $term = get_term( $val, 'post_tag' );
  122. $data[] = array(
  123. 'value' => $val,
  124. 'text' => $term->name
  125. );
  126. }
  127. }
  128.  
  129. wp_send_json($data);
  130. }
  131.  
  132. public function elementor_get_author()
  133. {
  134. if ( isset( $_REQUEST[ 'string' ] ) && ! empty( $_REQUEST[ 'string' ] ) )
  135. {
  136. $value = $_REQUEST[ 'string' ];
  137. } else {
  138. return false;
  139. }
  140.  
  141. $data = array();
  142. $values = explode(',', $value);
  143.  
  144. foreach( $values as $val )
  145. {
  146. if ( ! empty( $val ) )
  147. {
  148. $user = get_userdata( $val );
  149. $data[] = array(
  150. 'value' => $val,
  151. 'text' => $user->display_name
  152. );
  153. }
  154. }
  155.  
  156. wp_send_json($data);
  157. }
  158.  
  159. public function elementor_get_category()
  160. {
  161. if ( isset( $_REQUEST[ 'string' ] ) && ! empty( $_REQUEST[ 'string' ] ) )
  162. {
  163. $value = $_REQUEST[ 'string' ];
  164. } else {
  165. return false;
  166. }
  167.  
  168. $data = array();
  169. $values = explode(',', $value);
  170.  
  171. foreach( $values as $val )
  172. {
  173. if ( ! empty( $val ) )
  174. {
  175. $term = get_term( $val, 'category' );
  176. $data[] = array(
  177. 'value' => $val,
  178. 'text' => $term->name
  179. );
  180. }
  181. }
  182.  
  183. wp_send_json($data);
  184. }
  185.  
  186. public function elementor_editor_script()
  187. {
  188. if ( defined('ELEMENTOR_VERSION') )
  189. {
  190. $script = get_parent_theme_file_uri('assets/js');
  191. $version = (int) str_replace('.', '', ELEMENTOR_VERSION);
  192. $available = array(212,216,221);
  193.  
  194. if ( in_array($version , $available) ) {
  195. wp_deregister_script('elementor-editor');
  196.  
  197. wp_register_script(
  198. 'elementor-editor',
  199. $script . '/elementor/editor-' . $version . '.min.js',
  200. [
  201. 'wp-auth-check',
  202. 'jquery-ui-sortable',
  203. 'jquery-ui-resizable',
  204. 'backbone-marionette',
  205. 'backbone-radio',
  206. 'perfect-scrollbar',
  207. 'nprogress',
  208. 'tipsy',
  209. 'imagesloaded',
  210. 'heartbeat',
  211. 'jquery-elementor-select2',
  212. 'flatpickr',
  213. 'elementor-dialog',
  214. 'ace',
  215. 'ace-language-tools',
  216. 'jquery-hover-intent',
  217. ],
  218. ELEMENTOR_VERSION,
  219. true
  220. );
  221. }
  222. }
  223. }
  224.  
  225. public function register_element()
  226. {
  227. Plugin::$instance->elements_manager->register_element_type(new \JNews\Elementor\Element\Section());
  228. Plugin::$instance->elements_manager->register_element_type(new \Elementor\Element_Column());
  229. }
  230.  
  231. private function get_column_width($width)
  232. {
  233. if($width < 34) {
  234. $column = 4;
  235. } else if($width < 67) {
  236. $column = 8;
  237. } else {
  238. $column = 12;
  239. }
  240.  
  241. return $column;
  242. }
  243.  
  244. /**
  245. * @param Element_Column $object
  246. */
  247. public function register_width($object)
  248. {
  249. if($object instanceof Element_Column)
  250. {
  251. $setting = $object->get_settings();
  252. $column = $this->get_column_width($setting['_column_size']);
  253. ModuleManager::getInstance()->force_set_width($column);
  254. }
  255. }
  256.  
  257. public function modify_normal_widget_list($widgets)
  258. {
  259. $new_widgets = array();
  260. $excluded = array('Social_Widget', 'Social_Counter_Widget');
  261.  
  262. if(isset($_REQUEST['action']) && $_REQUEST['action'] === 'elementor')
  263. {
  264. foreach($widgets as $widget)
  265. {
  266. if(!in_array($widget, $excluded)) {
  267. $new_widgets[] = $widget;
  268. }
  269. }
  270.  
  271. return $new_widgets;
  272. }
  273.  
  274. return $widgets;
  275.  
  276. }
  277.  
  278. public function frontend_script()
  279. {
  280. wp_enqueue_script( 'selectize', get_parent_theme_file_uri ( '/assets/js/admin/elementor-frontend.js' ), array( 'jquery' ), null, null );
  281. }
  282.  
  283. public function editor_style()
  284. {
  285. wp_enqueue_style( 'jnews-admin', get_parent_theme_file_uri( '/assets/css/admin/admin-style.css' ), null, null );
  286. wp_enqueue_style( 'selectize', get_parent_theme_file_uri( '/assets/css/admin/selectize.default.css' ), null );
  287. wp_enqueue_style( 'jnews-elementor-css', get_parent_theme_file_uri( '/assets/css/elementor-backend.css' ), null, null);
  288. }
  289.  
  290. public function editor_script()
  291. {
  292. wp_enqueue_script( 'jquery-ui-spinner' );
  293. wp_enqueue_script( 'selectize', get_parent_theme_file_uri( '/assets/js/vendor/selectize.js' ), array( 'jquery' ), null, null );
  294. wp_enqueue_script( 'jnews-elementor-widget-js', get_parent_theme_file_uri( '/assets/js/admin/widget.js' ), null, null );
  295. wp_enqueue_script( 'jnews-elementor-js', get_parent_theme_file_uri( '/assets/js/admin/elementor-backend.js' ), null, null );
  296.  
  297. wp_localize_script( 'jnews-elementor-js', 'jnews_elementor', $this->localize_script() );
  298. }
  299.  
  300. public function localize_script()
  301. {
  302. $option = array();
  303.  
  304. $option['widgets'] = $this->populate_widget();
  305.  
  306. return $option;
  307. }
  308.  
  309. public function populate_widget()
  310. {
  311. // Module Widget
  312. $modules = $this->get_module_instance()->populate_module();
  313. $widgets = array();
  314.  
  315. foreach ( $modules as $module )
  316. {
  317. if ( $module['widget'] )
  318. {
  319. $widgets[] = str_replace('jnews', 'jnews_module', strtolower($module['name']));
  320. }
  321. }
  322.  
  323. // Normal Widget
  324. $normal_modules = RegisterNormalWidget::getInstance()->get_normal_widget();
  325.  
  326. foreach ( $normal_modules as $module )
  327. {
  328. $widgets[] = 'jnews_' . str_replace('_widget', '', strtolower($module));
  329. }
  330.  
  331. return $widgets;
  332. }
  333.  
  334. /**
  335. * @param \Elementor\Widgets_Manager $widgets_manager
  336. */
  337. public function register_module( $widgets_manager )
  338. {
  339. include get_parent_theme_file_path('class/Elementor/module-elementor.php');
  340.  
  341. $modules = $this->get_module_instance()->populate_module();
  342.  
  343. $exclude = array( 'social_icon_wrapper', 'social_icon_item', 'social_counter_wrapper', 'social_counter_item', 'widget' );
  344.  
  345. foreach ( $modules as $module )
  346. {
  347. if ( in_array( $module['type'], $exclude ) ) continue;
  348.  
  349. $classname = '\\' . $module['name'] . '_Elementor';
  350. $widgets_manager->register_widget_type( new $classname() );
  351. }
  352. }
  353.  
  354. /**
  355. * @param \Elementor\Widgets_Manager $widgets_manager
  356. */
  357. public function register_normal( $widgets_manager )
  358. {
  359. $widgets_manager->register_widget_type( new SocialIcon() );
  360. $widgets_manager->register_widget_type( new SocialCounter() );
  361. }
  362.  
  363. public function register_group()
  364. {
  365. $groups = array(
  366. 'jnews-module' => esc_html__( 'JNews - Module', 'jnews' ),
  367. 'jnews-hero' => esc_html__( 'JNews - Hero', 'jnews' ),
  368. 'jnews-slider' => esc_html__( 'JNews - Slider', 'jnews' ),
  369. 'jnews-element' => esc_html__( 'JNews - Element', 'jnews' ),
  370. 'jnews-carousel' => esc_html__( 'JNews - Carousel', 'jnews' ),
  371. 'jnews-footer' => esc_html__( 'JNews - Footer', 'jnews' ),
  372. 'jnews-post' => esc_html__( 'JNews - Post', 'jnews' )
  373. );
  374.  
  375. foreach ( $groups as $key => $value )
  376. {
  377. \Elementor\Plugin::$instance->elements_manager->add_category( $key, [ 'title' => $value ], 1 );
  378. }
  379. }
  380.  
  381. public function register_control()
  382. {
  383. $controls = array(
  384. 'multicategory' => 'JNews\Elementor\Control\Multicategory',
  385. 'multitag' => 'JNews\Elementor\Control\Multitag',
  386. 'multipost' => 'JNews\Elementor\Control\Multipost',
  387. 'multiauthor' => 'JNews\Elementor\Control\Multiauthor',
  388. 'multiproduct' => 'JNews\Elementor\Control\Multiproduct',
  389. 'multiselect' => 'JNews\Elementor\Control\Multiselect',
  390. 'alert' => 'JNews\Elementor\Control\Alert'
  391. );
  392.  
  393. foreach ( $controls as $type => $classname )
  394. {
  395. \Elementor\Plugin::instance()->controls_manager->register_control( $type, new $classname() );
  396. }
  397. }
  398.  
  399. public function register_sticky_option( $element )
  400. {
  401. $element->add_control(
  402. 'sticky_sidebar',
  403. [
  404. 'label' => esc_html__( 'Enable Sticky Sidebar', 'jnews' ),
  405. 'type' => \Elementor\Controls_Manager::SWITCHER,
  406. 'default' => '',
  407. 'description' => esc_html__( 'Set this column as sticky sidebar. Note: Sticky Sidebar will disabled when on the editor or you should refresh the editor to see the result. It will works fine on the frontend.', 'jnews' ),
  408. 'prefix_class' => ' jeg_sticky_sidebar '
  409. ]
  410. );
  411. }
  412.  
  413. public function register_custom_css_option( $element )
  414. {
  415. $element->add_control(
  416. 'custom_css',
  417. [
  418. 'label' => esc_html__( 'Custom CSS Setting', 'jnews' ),
  419. 'type' => \Elementor\Controls_Manager::CODE,
  420. 'default' => '',
  421. 'description' => esc_html__( 'Enter custom CSS (Note: it will be outputted only on this particular page).', 'jnews' ),
  422. ]
  423. );
  424. }
  425.  
  426. public function render_custom_css( $post_id = null )
  427. {
  428. if ( ! is_singular() ) return;
  429.  
  430. if ( ! $post_id ) $post_id = get_the_ID();
  431.  
  432. if ( $post_id )
  433. {
  434. $settings = get_post_meta( $post_id, '_elementor_page_settings', true );
  435.  
  436. if ( ! empty( $settings['custom_css'] ) )
  437. {
  438. echo '<style type="text/css" data-type="elementor_custom-css">';
  439. echo strip_tags( $settings['custom_css'] );
  440. echo '</style>';
  441. }
  442. }
  443. }
  444.  
  445. public function register_column_padding( $element )
  446. {
  447. $element->update_responsive_control(
  448. 'padding',
  449. [
  450. 'label' => __( 'Padding', 'elementor' ),
  451. 'type' => Controls_Manager::DIMENSIONS,
  452. 'size_units' => [ 'px', 'em', '%' ],
  453. 'selectors' => [
  454. '{{WRAPPER}} .elementor-element-populated' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
  455. ],
  456. ]
  457. );
  458. }
  459. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement