extension.class.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /**
  3. * Slider Revolution
  4. *
  5. * @package Essential_Grid
  6. * @author ThemePunch <info@themepunch.com>
  7. * @link http://revolution.themepunch.com/
  8. * @copyright 2015 ThemePunch
  9. */
  10. /**
  11. * @package RevSliderExtension
  12. * @author ThemePunch <info@themepunch.com>
  13. */
  14. if( !defined( 'ABSPATH') ) exit();
  15. class RevSliderExtension {
  16. public function __construct() {
  17. $this->init_essential_grid_extensions();
  18. }
  19. /***************************
  20. * Setup part for Revslider inclusion into Essential Grid
  21. ***************************/
  22. /**
  23. * Do all initializations for RevSlider integration
  24. */
  25. public function init_essential_grid_extensions(){
  26. if(!class_exists('Essential_Grid')) return false; //only add if Essential Grid is installed
  27. add_filter('essgrid_set_ajax_source_order', array($this, 'add_slider_to_eg_ajax'));
  28. add_filter('essgrid_handle_ajax_content', array($this, 'set_slider_values_to_eg_ajax'), 10, 4);
  29. add_action('essgrid_add_meta_options', array($this, 'add_eg_additional_meta_field'));
  30. add_action('essgrid_save_meta_options', array($this, 'save_eg_additional_meta_field'), 10, 2);
  31. //only do on frontend
  32. add_action('admin_head', array($this, 'add_eg_additional_inline_javascript'));
  33. add_action('wp_head', array($this, 'add_eg_additional_inline_javascript'));
  34. }
  35. /**
  36. * Add Slider to the List of choosable media
  37. */
  38. public function add_slider_to_eg_ajax($media){
  39. $media['revslider'] = array('name' => __('Slider Revolution', 'revslider'), 'type' => 'ccw');
  40. return $media;
  41. }
  42. /**
  43. * Add Slider to the List of choosable media
  44. */
  45. public function set_slider_values_to_eg_ajax($handle, $media_sources, $post, $grid_id){
  46. if($handle !== 'revslider') return false;
  47. $slider_source = '';
  48. $values = get_post_custom($post['ID']);
  49. if(isset($values['eg_sources_revslider'])){
  50. if(isset($values['eg_sources_revslider'][0]))
  51. $slider_source = (isset($values['eg_sources_revslider'][0])) ? $values['eg_sources_revslider'][0] : '';
  52. else
  53. $slider_source = (isset($values['eg_sources_revslider'])) ? $values['eg_sources_revslider'] : '';
  54. }
  55. if($slider_source === ''){
  56. return false;
  57. }else{
  58. return ' data-ajaxtype="'.$handle.'" data-ajaxsource="'.$slider_source.'"';
  59. }
  60. }
  61. /**
  62. * Adds custom meta field into the essential grid meta box for post/pages
  63. */
  64. public function add_eg_additional_meta_field($values){
  65. $sld = new RevSlider();
  66. $sliders = $sld->getArrSliders();
  67. $shortcodes = array();
  68. if(!empty($sliders)){
  69. $first = true;
  70. foreach($sliders as $slider){
  71. $name = $slider->getParam('shortcode','false');
  72. if($name != 'false'){
  73. $shortcodes[$slider->getID()] = $name;
  74. $first = false;
  75. }
  76. }
  77. }
  78. $selected_slider = (isset($values['eg_sources_revslider'])) ? $values['eg_sources_revslider'] : '';
  79. if($selected_slider == ''){
  80. $selected_slider = array();
  81. $selected_slider[0] = '';
  82. }
  83. ?>
  84. <p>
  85. <strong style="font-size:14px"><?php _e('Choose Revolution Slider', 'revslider'); ?></strong>
  86. </p>
  87. <p>
  88. <select name="eg_sources_revslider" id="eg_sources_revslider">
  89. <option value=""<?php selected($selected_slider[0], ''); ?>><?php _e('--- Choose Slider ---', 'revslider'); ?></option>
  90. <?php
  91. if(!empty($shortcodes)){
  92. foreach($shortcodes as $id => $name){
  93. ?>
  94. <option value="<?php echo $id; ?>"<?php selected($selected_slider[0], $id); ?>><?php echo $name; ?></option>
  95. <?php
  96. }
  97. }
  98. ?>
  99. </select>
  100. </p>
  101. <?php
  102. }
  103. /**
  104. * Adds custom meta field into the essential grid meta box for post/pages
  105. */
  106. public function save_eg_additional_meta_field($metas, $post_id){
  107. if(isset($metas['eg_sources_revslider']))
  108. update_post_meta($post_id, 'eg_sources_revslider', $metas['eg_sources_revslider']);
  109. }
  110. /**
  111. * Adds needed javascript to the DOM
  112. */
  113. public function add_eg_additional_inline_javascript(){
  114. ?>
  115. <script type="text/javascript">
  116. var ajaxRevslider;
  117. jQuery(document).ready(function() {
  118. // CUSTOM AJAX CONTENT LOADING FUNCTION
  119. ajaxRevslider = function(obj) {
  120. // obj.type : Post Type
  121. // obj.id : ID of Content to Load
  122. // obj.aspectratio : The Aspect Ratio of the Container / Media
  123. // obj.selector : The Container Selector where the Content of Ajax will be injected. It is done via the Essential Grid on Return of Content
  124. var content = "";
  125. data = {};
  126. data.action = 'revslider_ajax_call_front';
  127. data.client_action = 'get_slider_html';
  128. data.token = '<?php echo wp_create_nonce("RevSlider_Front"); ?>';
  129. data.type = obj.type;
  130. data.id = obj.id;
  131. data.aspectratio = obj.aspectratio;
  132. // SYNC AJAX REQUEST
  133. jQuery.ajax({
  134. type:"post",
  135. url:"<?php echo admin_url('admin-ajax.php'); ?>",
  136. dataType: 'json',
  137. data:data,
  138. async:false,
  139. success: function(ret, textStatus, XMLHttpRequest) {
  140. if(ret.success == true)
  141. content = ret.data;
  142. },
  143. error: function(e) {
  144. console.log(e);
  145. }
  146. });
  147. // FIRST RETURN THE CONTENT WHEN IT IS LOADED !!
  148. return content;
  149. };
  150. // CUSTOM AJAX FUNCTION TO REMOVE THE SLIDER
  151. var ajaxRemoveRevslider = function(obj) {
  152. return jQuery(obj.selector+" .rev_slider").revkill();
  153. };
  154. // EXTEND THE AJAX CONTENT LOADING TYPES WITH TYPE AND FUNCTION
  155. var extendessential = setInterval(function() {
  156. if (jQuery.fn.tpessential != undefined) {
  157. clearInterval(extendessential);
  158. if(typeof(jQuery.fn.tpessential.defaults) !== 'undefined') {
  159. jQuery.fn.tpessential.defaults.ajaxTypes.push({type:"revslider",func:ajaxRevslider,killfunc:ajaxRemoveRevslider,openAnimationSpeed:0.3});
  160. // type: Name of the Post to load via Ajax into the Essential Grid Ajax Container
  161. // func: the Function Name which is Called once the Item with the Post Type has been clicked
  162. // killfunc: function to kill in case the Ajax Window going to be removed (before Remove function !
  163. // openAnimationSpeed: how quick the Ajax Content window should be animated (default is 0.3)
  164. }
  165. }
  166. },30);
  167. });
  168. </script>
  169. <?php
  170. }
  171. }
  172. ?>