创建无限的健壮和灵活的响应式图像滑块。在任何帖子,自定义帖子类型或页面上,通过文本编辑器通过简码插入它们,或添加小部件。自动移动触摸式滑动和完全可自定义的皮肤。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

431 lines
22 KiB

  1. <?php
  2. /* "Copyright 2012 A3 Revolution Web Design" This software is distributed under the terms of GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 */
  3. /**
  4. * A3 Responsive Slider Shortcode
  5. *
  6. * Table Of Contents
  7. *
  8. * A3_Responsive_Slider_Shortcode()
  9. * init()
  10. * add_rslider_button()
  11. * rslider_generator_popup()
  12. * parse_shortcode_a3_responsive_slider()
  13. */
  14. namespace A3Rev\RSlider;
  15. if ( ! defined( 'ABSPATH' ) ) {
  16. exit; // Exit if accessed directly
  17. }
  18. class Shortcode
  19. {
  20. public function __construct () {
  21. $this->init();
  22. }
  23. public function init () {
  24. add_action( 'media_buttons', array( $this, 'add_rslider_button'), 100 );
  25. add_action( 'admin_footer', array( $this, 'rslider_generator_popup') );
  26. add_shortcode( 'a3_responsive_slider', array( $this, 'parse_shortcode_a3_responsive_slider') );
  27. // Make track a3_responsive_slider shortcode used in content when save post
  28. add_action( 'save_post', array( $this, 'track_shortcode_is_used' ), 11, 3 );
  29. }
  30. public function track_shortcode_is_used( $post_ID, $post, $update ) {
  31. $is_post_edit_page = in_array( basename( $_SERVER['PHP_SELF'] ), array( 'post.php', 'page.php', 'page-new.php', 'post-new.php' ) );
  32. if ( ! $is_post_edit_page ) return;
  33. if ( empty( $post_ID ) || empty( $post ) || empty( $_POST ) ) return;
  34. if ( is_int( wp_is_post_revision( $post ) ) ) return;
  35. if ( ! current_user_can( 'edit_post', $post_ID ) ) return;
  36. $post_type = get_post_type( $post_ID );
  37. $have_shortcode = false;
  38. $our_shortcode = 'a3_responsive_slider';
  39. // Remove old data for this post ID
  40. delete_post_meta( $post_ID, '_a3_slider_is_used' );
  41. delete_post_meta( $post_ID, '_a3_slider_is_used_' . $post_type );
  42. // Check if a3_responsive_slider shortcode is in the content
  43. if ( has_shortcode( $post->post_content, $our_shortcode ) ) {
  44. preg_match_all( '/' . get_shortcode_regex() . '/s', $post->post_content, $matches, PREG_SET_ORDER );
  45. if ( ! empty( $matches ) && is_array( $matches ) && count( $matches ) > 0 ) {
  46. foreach ( $matches as $shortcode ) {
  47. if ( $our_shortcode === $shortcode[2] ) {
  48. $attr = shortcode_parse_atts( $shortcode[3] );
  49. $my_attr = shortcode_atts( array(
  50. 'id' => 0
  51. ), $attr );
  52. $slider_id = $my_attr['id'];
  53. if ( $slider_id > 0 ) {
  54. $have_shortcode = true;
  55. $a3_slider_is_used = get_post_meta( $post_ID, '_a3_slider_is_used', false );
  56. if ( is_array( $a3_slider_is_used ) && in_array( $slider_id, $a3_slider_is_used ) ) continue;
  57. add_post_meta( $post_ID, '_a3_slider_is_used', $slider_id );
  58. add_post_meta( $post_ID, '_a3_slider_is_used_' . $post_type , $slider_id );
  59. }
  60. }
  61. }
  62. }
  63. }
  64. }
  65. public function add_rslider_button() {
  66. $is_post_edit_page = in_array( basename( $_SERVER['PHP_SELF'] ), array( 'post.php', 'page.php', 'page-new.php', 'post-new.php' ) );
  67. if ( ! $is_post_edit_page ) return;
  68. echo '<a href="#TB_inline?width=640&height=500&inlineId=a3-rslider-wrap" class="thickbox button a3-rslider-add-shortcode" title="' . __( 'Insert shortcode', 'a3-responsive-slider' ) . '"><span class="a3-rslider-add-shortcode_icon"></span>'.__( 'Sliders', 'a3-responsive-slider' ).'</a>';
  69. }
  70. public function rslider_generator_popup() {
  71. $is_post_edit_page = in_array( basename( $_SERVER['PHP_SELF'] ), array( 'post.php', 'page.php', 'page-new.php', 'post-new.php' ) );
  72. if ( ! $is_post_edit_page ) return;
  73. $list_sliders = get_posts( array(
  74. 'posts_per_page' => -1,
  75. 'orderby' => 'title',
  76. 'order' => 'ASC',
  77. 'post_type' => 'a3_slider',
  78. 'post_status' => 'publish',
  79. 'meta_query' => array(
  80. array(
  81. 'key' => '_a3_slider_id',
  82. 'value' => 1,
  83. 'compare' => '>=',
  84. 'type' => 'NUMERIC',
  85. )
  86. ),
  87. ));
  88. ?>
  89. <div id="a3-rslider-wrap" style="display:none">
  90. <fieldset style="border:1px solid #DFDFDF; padding:0 20px; background: #FFF; margin-top:15px;"><legend style="font-weight:bold; font-size:14px;"><?php _e( 'Insert Responsive Slider', 'a3-responsive-slider' ); ?></legend>
  91. <div id="a3-rslider-content" class="a3-rslider-content a3-rslider-shortcode-popup-container" style="text-align:left;">
  92. <p><label for="rslider_id"><?php _e( 'Select Slider', 'a3-responsive-slider' ); ?>:</label>
  93. <select style="width:300px" id="rslider_id" name="rslider_id">
  94. <?php
  95. echo '<option value="">'.__( 'Please select...', 'a3-responsive-slider' ).'</option>';
  96. if ( is_array( $list_sliders ) && count( $list_sliders ) > 0 ) {
  97. foreach ( $list_sliders as $slider ) {
  98. ?>
  99. <option value="<?php echo esc_attr( $slider->ID ); ?>" ><?php echo $slider->post_title; ?></option>
  100. <?php
  101. }
  102. }
  103. wp_reset_postdata();
  104. ?>
  105. </select>
  106. </p>
  107. <p><label for="rslider_description"><?php _e( 'Description', 'a3-responsive-slider' ); ?>:</label> <textarea style="width:100%; height:60px" id="rslider_description" name="rslider_description" placeholder="<?php _e( 'Enter the description that show at bottom of Slider', 'a3-responsive-slider' ); ?>"></textarea></p>
  108. <p><label for="rslider_align"><?php _e( 'Slider Alignment', 'a3-responsive-slider' ); ?>:</label>
  109. <select style="width:120px" id="rslider_align" name="rslider_align">
  110. <option value="none" selected="selected"><?php _e( 'None', 'a3-responsive-slider' ); ?></option>
  111. <option value="left-wrap"><?php _e( 'Left - wrap', 'a3-responsive-slider' ); ?></option>
  112. <option value="left"><?php _e( 'Left - no wrap', 'a3-responsive-slider' ); ?></option>
  113. <option value="center"><?php _e( 'Center', 'a3-responsive-slider' ); ?></option>
  114. <option value="right-wrap"><?php _e( 'Right - wrap', 'a3-responsive-slider' ); ?></option>
  115. <option value="right"><?php _e( 'Right - no wrap', 'a3-responsive-slider' ); ?></option>
  116. </select> <span class="description"><?php _e( 'Wrap is text wrap like images', 'a3-responsive-slider' ); ?></span></p>
  117. <p><label for="rslider_width"><?php _e( 'Slider Width', 'a3-responsive-slider' ); ?>:</label> <input style="width:50px;" size="10" id="rslider_width" name="rslider_width" type="text" value="300" />
  118. <select style="width:60px" id="rslider_width_type" name="rslider_width_type">
  119. <option value="px" selected="selected">px</option>
  120. <option value="%">%</option>
  121. </select>
  122. </p>
  123. <p><label for="rslider_tall_type"><?php _e( 'Slider Tall Type', 'a3-responsive-slider' ); ?>:</label>
  124. <label style="width:auto; float:none"><input type="radio" name="rslider_tall_type" class="rslider_tall_type" id="rslider_tall_type_fixed" value="fixed" checked="checked" /> <?php _e( 'Fixed', 'a3-responsive-slider' ); ?></label> &nbsp;&nbsp;&nbsp;
  125. <label style="width:auto; float:none"><input type="radio" name="rslider_tall_type" class="rslider_tall_type" id="rslider_tall_type_dynamic" value="dynamic" /> <?php _e( 'Dynamic', 'a3-responsive-slider' ); ?></label>
  126. </p>
  127. <p class="rslider_tall_type_fixed_container"><label for="rslider_height"><?php _e( 'Slider Height', 'a3-responsive-slider' ); ?>:</label> <input style="width:50px;" size="10" id="rslider_height" name="rslider_height" type="text" value="250" /> px
  128. </p>
  129. <p><label for=""><strong><?php _e( 'Slider Margin', 'a3-responsive-slider' ); ?></strong>:</label><br />
  130. <label for="rslider_margin_top" style="width:auto; float:none"><?php _e( 'Above', 'a3-responsive-slider' ); ?>:</label><input style="width:50px;" size="10" id="rslider_margin_top" name="rslider_margin_top" type="text" value="10" />px &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  131. <label for="rslider_margin_bottom" style="width:auto; float:none"><?php _e( 'Below', 'a3-responsive-slider' ); ?>:</label> <input style="width:50px;" size="10" id="rslider_margin_bottom" name="rslider_margin_bottom" type="text" value="10" />px &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  132. <label for="rslider_margin_left" style="width:auto; float:none"><?php _e( 'Left', 'a3-responsive-slider' ); ?>:</label> <input style="width:50px;" size="10" id="rslider_margin_left" name="rslider_margin_left" type="text" value="10" />px &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  133. <label for="rslider_margin_right" style="width:auto; float:none"><?php _e( 'Right', 'a3-responsive-slider' ); ?>:</label> <input style="width:50px;" size="10" id="rslider_margin_right" name="rslider_margin_right" type="text" value="10" />px
  134. </p>
  135. <p><label for=""><strong><?php _e( 'Description Container Margin', 'a3-responsive-slider' ); ?></strong>:</label><br />
  136. <label for="rs_desc_margin_top" style="width:auto; float:none"><?php _e( 'Above', 'a3-responsive-slider' ); ?>:</label><input style="width:50px;" size="10" id="rs_desc_margin_top" name="rs_desc_margin_top" type="text" value="10" />px &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  137. <label for="rs_desc_margin_bottom" style="width:auto; float:none"><?php _e( 'Below', 'a3-responsive-slider' ); ?>:</label> <input style="width:50px;" size="10" id="rs_desc_margin_bottom" name="rs_desc_margin_bottom" type="text" value="10" />px &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  138. <label for="rs_desc_margin_left" style="width:auto; float:none"><?php _e( 'Left', 'a3-responsive-slider' ); ?>:</label> <input style="width:50px;" size="10" id="rs_desc_margin_left" name="rs_desc_margin_left" type="text" value="10" />px &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  139. <label for="rs_desc_margin_right" style="width:auto; float:none"><?php _e( 'Right', 'a3-responsive-slider' ); ?>:</label> <input style="width:50px;" size="10" id="rs_desc_margin_right" name="rs_desc_margin_right" type="text" value="10" />px
  140. </p>
  141. </div>
  142. <div style="clear:both;height:0px"></div>
  143. <p><input type="button" class="button button-primary" value="<?php _e( 'Insert Shortcode', 'a3-responsive-slider' ); ?>" onclick="a3_rslider_add_shortcode();"/>
  144. <input type="button" class="button" onclick="tb_remove(); return false;" value="<?php _e('Cancel', 'a3-responsive-slider' ); ?>" />
  145. </p>
  146. </fieldset>
  147. </div>
  148. <script type="text/javascript">
  149. (function($) {
  150. $(document).ready(function() {
  151. $("input.rslider_tall_type").change( function() {
  152. if ( $("input.rslider_tall_type:checked").val() == 'fixed') {
  153. $(".rslider_tall_type_fixed_container").slideDown();
  154. } else {
  155. $(".rslider_tall_type_fixed_container").slideUp();
  156. }
  157. });
  158. });
  159. })(jQuery);
  160. function a3_rslider_add_shortcode(){
  161. var selected_rslider_id = jQuery("#rslider_id").val();
  162. if (selected_rslider_id == '') {
  163. alert('<?php _e( 'Please select Slider', 'a3-responsive-slider' ); ?>');
  164. return false;
  165. }
  166. var rslider_description = jQuery("#rslider_description").val();
  167. var rslider_align = jQuery("#rslider_align").val();
  168. var rslider_width = jQuery("#rslider_width").val();
  169. var rslider_width_type = jQuery("#rslider_width_type").val();
  170. var rslider_tall_type = jQuery(".rslider_tall_type:checked").val();
  171. var rslider_height = jQuery("#rslider_height").val();
  172. var rslider_margin_top = jQuery("#rslider_margin_top").val();
  173. var rslider_margin_bottom = jQuery("#rslider_margin_bottom").val();
  174. var rslider_margin_left = jQuery("#rslider_margin_left").val();
  175. var rslider_margin_right = jQuery("#rslider_margin_right").val();
  176. var rs_desc_margin_top = jQuery("#rs_desc_margin_top").val();
  177. var rs_desc_margin_bottom = jQuery("#rs_desc_margin_bottom").val();
  178. var rs_desc_margin_left = jQuery("#rs_desc_margin_left").val();
  179. var rs_desc_margin_right = jQuery("#rs_desc_margin_right").val();
  180. var win = window.dialogArguments || opener || parent || top;
  181. win.send_to_editor('[a3_responsive_slider id="' + selected_rslider_id
  182. + '" inline_post="true'
  183. + '" description="' + rslider_description
  184. + '" align="' + rslider_align
  185. + '" width="' + rslider_width
  186. + '" width_type="' + rslider_width_type
  187. + '" tall_type="' + rslider_tall_type
  188. + '" height="' + rslider_height
  189. + '" margin_top="' + rslider_margin_top
  190. + '" margin_bottom="' + rslider_margin_bottom
  191. + '" margin_left="' + rslider_margin_left
  192. + '" margin_right="' + rslider_margin_right
  193. + '" desc_margin_top="' + rs_desc_margin_top
  194. + '" desc_margin_bottom="' + rs_desc_margin_bottom
  195. + '" desc_margin_left="' + rs_desc_margin_left
  196. + '" desc_margin_right="' + rs_desc_margin_right
  197. + '"]');
  198. }
  199. </script>
  200. <?php
  201. }
  202. public function parse_shortcode_a3_responsive_slider( $attributes ) {
  203. extract( shortcode_atts( array(
  204. 'id' => '',
  205. 'inline_post' => false,
  206. 'description' => '',
  207. 'align' => 'none',
  208. 'width' => 300,
  209. 'width_type' => 'px',
  210. 'tall_type' => 'fixed',
  211. 'height' => 250,
  212. 'margin_top' => 10,
  213. 'margin_bottom' => 10,
  214. 'margin_left' => 10,
  215. 'margin_right' => 10,
  216. 'desc_margin_top' => 10,
  217. 'desc_margin_bottom'=> 10,
  218. 'desc_margin_left' => 10,
  219. 'desc_margin_right' => 10,
  220. ), $attributes)
  221. );
  222. $slider_id = $id;
  223. $slider_data = get_post( $slider_id );
  224. if ( $slider_data == NULL ) return '';
  225. $have_slider_id = get_post_meta( $slider_id, '_a3_slider_id' , true );
  226. if ( $have_slider_id < 1 ) return '';
  227. $slider_settings = get_post_meta( $slider_id, '_a3_slider_settings', true );
  228. $slide_items = Data::get_all_images_from_slider_client( $id );
  229. global $a3_rslider_template1_global_settings;
  230. $templateid = 'template1';
  231. $slider_template = 'template-1';
  232. global ${'a3_rslider_'.$templateid.'_dimensions_settings'}; // @codingStandardsIgnoreLine // phpcs:ignore
  233. $dimensions_settings = ${'a3_rslider_'.$templateid.'_dimensions_settings'};
  234. $output = '';
  235. $rslider_custom_style = '';
  236. $rslider_inline_style = '';
  237. $rslider_desc_container_style = '';
  238. $description_html = '';
  239. $rslider_shortcode_wrap = false;
  240. if ( $inline_post ) {
  241. if ( $align == 'center' ) $rslider_custom_style .= 'float:none;margin:auto;display:block;';
  242. elseif ( $align == 'left-wrap' ) $rslider_custom_style .= 'float:left;';
  243. elseif ( $align == 'right-wrap' ) $rslider_custom_style .= 'float:right;';
  244. else $rslider_custom_style .= 'float:'. $align .';';
  245. if( $align == 'left-wrap' || $align == 'right-wrap' ) $rslider_shortcode_wrap = true;
  246. if ( $width > 0 ) $rslider_custom_style .= 'width:' . $width . $width_type . ' !important;';
  247. if ( $tall_type == 'fixed' ) $rslider_inline_style .= 'height:'.$height.'px !important;';
  248. else $rslider_inline_style .= 'height:auto;';
  249. // override dimension from current template
  250. if ( $width > 0 ) {
  251. if ( $width_type == 'px' ) {
  252. $dimensions_settings['is_slider_responsive'] = 0;
  253. $dimensions_settings['slider_width'] = $width;
  254. } else {
  255. $dimensions_settings['is_slider_responsive'] = 1;
  256. $dimensions_settings['slider_wide_responsive'] = $width;
  257. }
  258. }
  259. if ( $tall_type == 'fixed' ) {
  260. $dimensions_settings['is_slider_tall_dynamic'] = 0;
  261. $dimensions_settings['slider_height_fixed'] = $height;
  262. } else {
  263. $dimensions_settings['is_slider_tall_dynamic'] = 1;
  264. }
  265. if ( $margin_top >= 0 ) $rslider_inline_style .= 'margin-top:'.$margin_top.'px;';
  266. if ( $margin_bottom >= 0 ) $rslider_inline_style .= 'margin-bottom:'.$margin_bottom.'px;';
  267. if ( $margin_left >= 0 ) $rslider_inline_style .= 'margin-left:'.$margin_left.'px;';
  268. if ( $margin_right >= 0 ) $rslider_inline_style .= 'margin-right:'.$margin_right.'px;';
  269. if ( $desc_margin_top >= 0 ) $rslider_desc_container_style .= 'margin-top:'.$desc_margin_top.'px;';
  270. if ( $desc_margin_bottom >= 0 ) $rslider_desc_container_style .= 'margin-bottom:'.$desc_margin_bottom.'px;';
  271. if ( $desc_margin_left >= 0 ) $rslider_desc_container_style .= 'margin-left:'.$desc_margin_left.'px;';
  272. if ( $desc_margin_right >= 0 ) $rslider_desc_container_style .= 'margin-right:'.$desc_margin_right.'px;';
  273. if ( tr