PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/cnhk-slideshow/cnhk-plugin.php

https://bitbucket.org/antonyravel/cape-resorts
PHP | 685 lines | 635 code | 34 blank | 16 comment | 45 complexity | e5a60f67d4ec9f69f11daad3a2456d9e MD5 | raw file
  1. <?php
  2. /*
  3. Plugin Name: Cnhk Slideshow
  4. Description: Cnhk Slideshow is a simple jQuery slideshow plugin with a drag and drop system for editing slides order. Images files are stored in the plugin directory ( Not mixed with WordPress's Media Library ).
  5. Version: 1.1.1
  6. Author: Rija Rajaonah
  7. License: GLPv2 or later
  8. */
  9. if ( '' == session_id() ) {
  10. session_start();
  11. }
  12. $cnhk_token_snap = md5( uniqid( rand (), true ) );
  13. $_SESSION['cnhk']['token_snap'] = $cnhk_token_snap;
  14. register_activation_hook( __FILE__, 'cnhk_install' );
  15. register_deactivation_hook( __FILE__, 'cnhk_uninstall' );
  16. define( 'CNHK_DIR', dirname( realpath( __FILE__ ) ) );
  17. define( 'CNHK_ADMIN_SLIDE', admin_url('admin.php?page=cnhk-slide') );
  18. define( 'CNHK_ADMIN_MAIN', admin_url('admin.php?page=cnhk_mainpage') );
  19. define( 'CNHK_ADMIN_SETTINGS', admin_url('admin.php?page=cnhk-settings') );
  20. define( 'CNHK_URL', plugin_dir_url( __FILE__ ) );
  21. add_action( 'admin_menu', 'cnhk_admin_menu' );
  22. add_action( 'admin_init', 'cnhk_init' );
  23. add_action( 'admin_enqueue_scripts', 'cnhk_admin_script' );
  24. add_action( 'wp_enqueue_scripts', 'cnhk_front_script' );
  25. add_action( 'widgets_init', 'cnhk_widget' );
  26. add_filter( 'plugin_action_links', 'cnhk_add_link', 10, 2 );
  27. /**
  28. * Add setting link in the plugin list
  29. */
  30. include_once( CNHK_DIR . DIRECTORY_SEPARATOR . 'ajax.php' );
  31. function cnhk_add_link( $lnk, $file ) {
  32. static $cnhk_this_plugin;
  33. if ( ! isset( $cnhk_this_plugin ) ) $cnhk_this_plugin = plugin_basename( __FILE__ );
  34. if ( $file == $cnhk_this_plugin ) {
  35. $settings_link = '<a href="admin.php?page=cnhk-settings">Settings</a>';
  36. array_unshift($lnk, $settings_link);
  37. }
  38. return $lnk;
  39. }
  40. /**
  41. * Avoid re-sending form after refreshing a page
  42. */
  43. function cnhk_redirect() {
  44. if ( isset( $_POST['del_bgr'] ) || ! empty( $_POST['token_del_ss'] ) || ! empty( $_POST['cnhk_token_setting'] ) || ! empty( $_POST['cnhk_token_new_ss'] ) || ! empty( $_POST['cnhk_token_update'] ) || ! empty( $_POST['cnhk_token_delete'] ) || ! empty( $_POST['cnhk_token_edit'] ) || ! empty( $_POST['cnhk_token_ss'] ) ) {
  45. $_SESSION['cnhk']['savepost'] = $_POST;
  46. $running = $_SERVER['PHP_SELF'];
  47. if ( ! empty( $_SERVER['QUERY_STRING'] ) )
  48. {
  49. $running .= '?' . $_SERVER['QUERY_STRING'];
  50. }
  51. wp_redirect( $running );
  52. die();
  53. }
  54. if ( isset( $_POST['cnhk_token_file'] ) && trim( $_POST['cnhk_token_file'] ) == $_SESSION['cnhk']['token_file'] ) {
  55. $referer = admin_url ('admin.php?page=cnhk-slide');
  56. if ( isset ( $_SERVER['HTTP_REFERER'] ) && $_SERVER['HTTP_REFERER'] == $referer ) {
  57. $has_error = false;
  58. if ( $_FILES['cnhk_file']['error'] > 0 ) {
  59. $_SESSION['cnhk']['upload_error_upload'] = 'err';
  60. $has_error = true;
  61. }
  62. $valid_extensions = array( 'image/jpeg', 'image/png' );
  63. if ( ! in_array( $_FILES['cnhk_file']['type'], $valid_extensions ) ) {
  64. $_SESSION['cnhk']['upload_error_extension'] = 'err';
  65. $has_error = true;
  66. }
  67. if ( 2*1024*1024 < $_FILES['cnhk_file']['size'] ) {
  68. $_SESSION['cnhk']['upload_error_size'] = 'err';
  69. $has_error = true;
  70. }
  71. if ( ! $has_error ) {
  72. $nb = get_file_number();
  73. $ext_list = array( 'image/jpeg' => '.jpg', 'image/png' => '.png' );
  74. $res = CNHK_DIR . DIRECTORY_SEPARATOR .'uploads'. DIRECTORY_SEPARATOR .'slide' . $nb . $ext_list[$_FILES['cnhk_file']['type']];
  75. if ( move_uploaded_file( $_FILES['cnhk_file']['tmp_name'], $res ) ) {
  76. $_SESSION['cnhk']['upload_error_upload'] = '';
  77. $_SESSION['cnhk']['upload_error_extension'] = '';
  78. $_SESSION['cnhk']['upload_error_size'] = '';
  79. $opt = get_option( 'cnhk_options_slides' );
  80. array_push( $opt[0], "slide{$nb}" . $ext_list[$_FILES['cnhk_file']['type']] );
  81. array_push( $opt[1], -1 );
  82. array_push( $opt[2], "slide{$nb}" );
  83. array_push( $opt[3], rawurlencode("No link") );
  84. array_push( $opt[4], time() );
  85. update_option( 'cnhk_options_slides', $opt );
  86. }
  87. }
  88. $running = $_SERVER['PHP_SELF'];
  89. if ( ! empty( $_SERVER['QUERY_STRING'] ) )
  90. {
  91. $running .= '?' . $_SERVER['QUERY_STRING'];
  92. }
  93. wp_redirect( $running );
  94. die();
  95. }
  96. }
  97. if ( isset( $_POST['cnhk_token_background'] ) && $_POST['cnhk_token_background'] == $_SESSION['cnhk']['token_background'] ) {
  98. $referer = admin_url( 'admin.php?page=cnhk-settings' );
  99. if ( isset( $_SERVER['HTTP_REFERER'] ) && false !== stripos( $_SERVER['HTTP_REFERER'], $referer ) ) {
  100. $has_error = false;
  101. if ( $_FILES['background-upload']['error'] > 0 ) {
  102. $_SESSION['cnhk']['upload_error_upload'] = 'err';
  103. $has_error = true;
  104. }
  105. $valid_types = array( 'image/jpeg', 'image/png' );
  106. $ext_list = array( 'image/jpeg' => '.jpg', 'image/png' => '.png' );
  107. $extension = '.xxx';
  108. if ( ! in_array( $_FILES['background-upload']['type'], $valid_types ) ) {
  109. $_SESSION['cnhk']['upload_error_extension'] = 'err';
  110. $has_error = true;
  111. } else {
  112. $extension = $ext_list[$_FILES['background-upload']['type']];
  113. }
  114. if ( 2*1024*1024 < $_FILES['background-upload']['size'] ) {
  115. $_SESSION['cnhk']['upload_error_size'] = 'err';
  116. $has_error = true;
  117. }
  118. $destination = CNHK_DIR . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'bgr' . DIRECTORY_SEPARATOR;
  119. $target = intval( trim( $_POST['target'] ) );
  120. if ( ! $has_error ) {
  121. unlink( $destination . "bgr-$target" . '.jpg' );
  122. unlink( $destination . "bgr-$target" . '.jpeg' );
  123. unlink( $destination . "bgr-$target" . '.png' );
  124. if ( move_uploaded_file( $_FILES['background-upload']['tmp_name'], $destination . "bgr-$target" . $extension ) ) {
  125. $_SESSION['cnhk']['upload_error_upload'] = '';
  126. $_SESSION['cnhk']['upload_error_extension'] = '';
  127. $_SESSION['cnhk']['upload_error_size'] = '';
  128. $opt = get_option( 'cnhk_options' );
  129. $opt[10][$target] = true;
  130. update_option( 'cnhk_options', $opt );
  131. }
  132. }
  133. $running = $_SERVER['PHP_SELF'];
  134. if ( ! empty( $_SERVER['QUERY_STRING'] ) )
  135. {
  136. $running .= '?' . $_SERVER['QUERY_STRING'];
  137. }
  138. wp_redirect( $running );
  139. die();
  140. }
  141. }
  142. if ( isset( $_SESSION['cnhk']['savepost'] ) )
  143. {
  144. $_POST = $_SESSION['cnhk']['savepost'];
  145. unset( $_SESSION['cnhk']['savepost']);
  146. }
  147. }
  148. function get_file_number() {
  149. $i = 1;
  150. $slide_path = CNHK_DIR . DIRECTORY_SEPARATOR . "uploads". DIRECTORY_SEPARATOR;
  151. while ( file_exists( $slide_path . "slide{$i}.jpg" ) || file_exists( $slide_path . "slide{$i}.png" ) ) {
  152. $i++;
  153. }
  154. return $i;
  155. }
  156. /**
  157. * Contextual Help
  158. */
  159. function cnhk_admin_help() {
  160. $screen = get_current_screen();
  161. global $cnhk_main_page;
  162. global $cnhk_settings_page;
  163. global $cnhk_slides_page;
  164. if ( $cnhk_main_page == $screen->id ) {
  165. $help = "<h4>Slideshow :</h4>";
  166. $help .= "Just drag and drop snapshots to put or reoder slides within the slideshow. ";
  167. $help .= "You can also drag a slide out of the slideshow, without permanently deleting it.<br />";
  168. $help .= "<strong><em>Do not forget to select the right slideshow.</em></strong>";
  169. $screen->add_help_tab( array(
  170. 'id' => 'Overview',
  171. 'title' => 'Overview',
  172. 'content' => $help
  173. ) );
  174. }
  175. if ( $cnhk_settings_page == $screen->id ) {
  176. $new = "<h4>Adding new slideshow</h4>";
  177. $new .= "You can create multiples slideshow but each slideshow name must be unique(case sensitive).<br /> When you delete a slideshow, all slides used in it are set to 'Unused'.";
  178. $select = "<h4>Selecting slideshow</h4>";
  179. $select .= "This dropdown list contains all existing slideshow. It allows you to select a slideshow to edit (or delete). When you delete a slideshow, all slides used in it will be set as unused.<br />";
  180. $settings = "<h4>Settings</h4>";
  181. $settings .= "<ul>";
  182. $settings .= "<li>By default, all slideshow has a fluid width. It means that the slideshow will take all available width and is resized each time the page is resized (even partially) ";
  183. $settings .= "in accordance with the <em>width/height 's ratio</em> that you defined in this screen. ";
  184. $settings .= "In other terms, all images that you use in this slideshow will be stretched to fit this ratio.</li>";
  185. $settings .= "<li>If you choose the fix width, the slideshow will be wrapped inside a container with the fixed with that you defined. ";
  186. $settings .= "You can also choose an alignment for this container to make the slideshow look better when inserted in a post for example.</li>";
  187. $settings .= "</ul>";
  188. $display = "<h4>Displaying slideshow</h4>";
  189. $display .= "You can display slideshows by:";
  190. $display .= "<ul>";
  191. $display .= "<li><h5>Using widget</h5>";
  192. $display .= "Go to the widget menu. Add a <em>cnhk slideshow</em> widget to a sidebar. Then select one of the slideshow you've created and save changes.</li>";
  193. $display .= "<li><h5>Using a shortcode</h5>";
  194. $display .= "In your post you can use a shortcode like <code>[cnhk_slideshow name='my_slideshow' width='300' align='float-left']</code> where :<ul>";
  195. $display .= "<li><strong><em>my_slideshow</em></strong> is the <em>name parameter</em> that you defined on the settings page.</li>";
  196. $display .= "<li><strong><em>300</em></strong> is the fixed width in pixels of the container (if you want it to be a fixed width slideshow).";
  197. $display .= " If set, this value will override the one you've entered in the settings page for this slideshow. Then a fluid width slideshow will have locally a fixed width</li>";
  198. $display .= "<li><strong><em>align-left</em></strong> is the alignment of the container (only for fixed width). If set, this value will override the one you've entered in the settings page. ";
  199. $display .= "<br />Possible values : <strong>'left', 'right', 'center', 'float-left', 'float-right'.</strong></li></ul><strong><em>Only the name parameter is required.</em></strong></li>";
  200. $display .= "<li><h5>Using template tag</h5>";
  201. $display .= "Directly in your theme's files with <code>&lt;?php if ( function_exists( 'cnhk_slideshow' ) ) cnhk_slideshow( 'my_slideshow', 300, 'align-left' ); ?&gt;</code><br />";
  202. $display .= "Meanings of all parameters are the same as for shortcode and <strong><em>only the name parameter is required.</em></strong> Pay attention on the width parameter which is passed ";
  203. $display .= "as an integer to the function (no wrapping quotes).</li></ul>";
  204. $bgr = "<h4>Optional Background</h4>";
  205. $bgr .= "You can add a background image for each slideshow. This image will fit the ratio defined in this page and will be visible through transparent areas of '.png' slides";
  206. $screen->add_help_tab( array(
  207. 'id' => 'Overview',
  208. 'title' => 'Overview',
  209. 'content' => $settings
  210. ) );
  211. $screen->add_help_tab( array(
  212. 'id' => 'Creating',
  213. 'title' => 'Creating Slideshow',
  214. 'content' => $new
  215. ) );
  216. $screen->add_help_tab( array(
  217. 'id' => 'Select',
  218. 'title' => 'Selecting Slideshow',
  219. 'content' => $select
  220. ) );
  221. $screen->add_help_tab( array(
  222. 'id' => 'Display',
  223. 'title' => 'Displaying Slideshow',
  224. 'content' => $display
  225. ) );
  226. $screen->add_help_tab( array(
  227. 'id' => 'Background',
  228. 'title' => 'Optional Background',
  229. 'content' => $bgr
  230. ) );
  231. }
  232. if ( $cnhk_slides_page == $screen->id ) {
  233. $upload = "<h4>Uploading new slide:</h4>";
  234. $upload .= "All uploaded images stay inside the plugin's directory, and will not mix with images in the WordPress Media Library.<br />";
  235. $delete = "<h4>Deleting slide:</h4>";
  236. $delete .= "This will delete the image file (from the plugin's directory) and the corresponding data in WordPress's database.<br />";
  237. $delete .= "If you mean to reuse the slide, just drop it out of the slideshow in the <a href='" . CNHK_ADMIN_MAIN . "'><em>\" slideshow \"</em></a> screen instead of permanently deleting it.<br />";
  238. $help = "<h4>Overview :</h4>";
  239. $help .= "You can edit each slide's title, link and usage in this screen. You can use each slide in only one slideshow.<br />";
  240. $help .= "You can click on values listed below to edit them or click on the 'Edit' link.";
  241. $screen->add_help_tab( array(
  242. 'id' => 'Overview',
  243. 'title' => 'Overview',
  244. 'content' => $help
  245. ) );
  246. $screen->add_help_tab( array(
  247. 'id' => 'Upload',
  248. 'title' => 'Uploading new file',
  249. 'content' => $upload
  250. ) );
  251. $screen->add_help_tab( array(
  252. 'id' => 'Delete',
  253. 'title' => 'Deleting slide',
  254. 'content' => $delete
  255. ) );
  256. }
  257. }
  258. /**
  259. * Add stylesheet and scripts for the front end
  260. */
  261. function cnhk_front_script() {
  262. wp_register_style( 'cnhk_front_css', plugins_url( 'css/cnhk-front.css', __FILE__ ) );
  263. wp_enqueue_style( 'cnhk_front_css' );
  264. wp_enqueue_script( 'jquery' );
  265. wp_enqueue_script( 'jquery-cycle', plugins_url( 'js/jquery.cycle.all.js', __FILE__ ), array( 'jquery' ) );
  266. wp_enqueue_script( 'cnhk_front_js', plugins_url( 'js/cnhk-front.js', __FILE__ ), array( 'jquery-cycle' ) );
  267. }
  268. /**
  269. * Add stylesheets and scripts for admin pages
  270. */
  271. function cnhk_admin_script() {
  272. global $pagenow;
  273. if ( 'admin.php' == $pagenow ) {
  274. $page_list = array( 'cnhk_mainpage', 'cnhk-slide', 'cnhk-settings' );
  275. if ( isset( $_GET['page'] ) && in_array( $_GET['page'], $page_list ) ) {
  276. wp_register_style( 'cnhk-slideshow_css', plugins_url( 'css/cnhk-ss.css', __FILE__ ) );
  277. wp_register_style( 'cnhk-uploadify_css', plugins_url( 'uploadify/uploadify.css', __FILE__ ) );
  278. wp_enqueue_style( 'cnhk-slideshow_css' );
  279. wp_enqueue_style( 'cnhk-uploadify_css' );
  280. wp_enqueue_script( 'jquery' );
  281. wp_enqueue_script( 'jquery-ui-core' );
  282. wp_enqueue_script( 'jquery-ui-widget' );
  283. wp_enqueue_script( 'jquery-ui-mouse' );
  284. wp_enqueue_script( 'jquery-ui-sortable' );
  285. wp_enqueue_script( 'cnhk-uploadify', plugins_url( 'uploadify/jquery.uploadify-3.1.min.js', __FILE__ ), array( 'jquery' ) );
  286. wp_enqueue_script( 'cnhk_admin_js', plugins_url( 'js/cnhk-admin.js', __FILE__ ), array( 'jquery', 'jquery-ui-sortable' ) );
  287. wp_enqueue_script( 'cnhk-ajax', plugins_url( 'js/ajax.js', __FILE__ ), array( 'jquery' ) );
  288. }
  289. }
  290. }
  291. /**
  292. * Makes some variables accessible by javascript
  293. */
  294. function cnhk_admin_head_callback() {
  295. if ( isset( $_GET['page'] ) ) {
  296. $_SESSION['cnhk']['ajaxnonce'] = md5( uniqid( rand (), true ) );
  297. global $pagenow;
  298. $root = preg_replace( "#[\\/]#", DIRECTORY_SEPARATOR, $_SERVER['DOCUMENT_ROOT'] );
  299. $cnhk_url = CNHK_URL . '/uploads';
  300. ?>
  301. <script type="text/javascript">
  302. /* <![CDATA[ */
  303. var cnhk_ajax_settings = {
  304. url :"<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>",
  305. sid : "<?php echo session_id(); ?>",
  306. imgurl :"<?php echo esc_url( CNHK_URL . 'images' ); ?>",
  307. cnhk_dir :"<?php echo rawurlencode( CNHK_DIR ); ?>",
  308. cnhk_url :"<?php echo CNHK_URL; ?>",
  309. admin_slides_url : "<?php echo CNHK_ADMIN_SLIDE; ?>",
  310. nonce :'<?php echo $_SESSION['cnhk']['ajaxnonce']; ?>',
  311. pagenow :'<?php echo $pagenow; ?>',
  312. page :'<?php echo $_GET['page']; ?>',
  313. pending :false
  314. }
  315. /* ]]> */
  316. </script>
  317. <?php
  318. }
  319. }
  320. /**
  321. * Initialisation
  322. */
  323. function cnhk_init() {
  324. cnhk_redirect();
  325. }
  326. /**
  327. * Activation
  328. */
  329. function cnhk_install() {
  330. if ( ! get_option( 'cnhk_options' ) || ! get_option( 'cnhk_options_slides' ) || ! get_option( 'cnhk_options_ss' ) ) {
  331. cnhk_default();
  332. }
  333. }
  334. /**
  335. * Deactivation
  336. */
  337. function cnhk_uninstall() {
  338. unregister_widget( 'Cnhk_Ss_Widget' );
  339. }
  340. /**
  341. * Default values for options
  342. */
  343. function cnhk_default(){
  344. $name = array();
  345. $effect = array();
  346. $transition_speed = array();
  347. $slide_duration = array();
  348. $height = array();
  349. $width = array();
  350. $nav = array();
  351. $fixed = array();
  352. $fixed_width = array();
  353. $fixed_align = array();
  354. $background = array();
  355. $image = array();
  356. $use = array();
  357. $title = array();
  358. $link = array();
  359. $date = array();
  360. $arr = array( $name, $effect, $transition_speed, $slide_duration, $height, $width, $nav, $fixed, $fixed_width, $fixed_align, $background );
  361. $arr2 = array( $image, $use, $title, $link, $date );
  362. $ss = array( 'ss' => array(), 'elem' => array() );
  363. update_option( 'cnhk_options', $arr );
  364. update_option( 'cnhk_options_slides', $arr2 );
  365. update_option( 'cnhk_options_ss', $ss );
  366. }
  367. /**
  368. * Creates the admin menu
  369. */
  370. function cnhk_admin_menu() {
  371. global $cnhk_main_page;
  372. global $cnhk_settings_page;
  373. global $cnhk_slides_page;
  374. $cnhk_main_page = add_menu_page( 'Slideshow', 'Slideshow', 'manage_options', 'cnhk_mainpage', 'cnhk_main_menu_fn', '', 14 );
  375. $cnhk_settings_page = add_submenu_page( 'cnhk_mainpage', 'Settings', 'Settings', 'manage_options', 'cnhk-settings', 'cnhk_setting_fn' );
  376. $cnhk_slides_page = add_submenu_page( 'cnhk_mainpage', 'Slides', 'Slides', 'manage_options', 'cnhk-slide', 'cnhk_slide_fn' );
  377. add_action( 'admin_head-' . $cnhk_slides_page, 'cnhk_admin_head_callback' );
  378. add_action( 'admin_head-' . $cnhk_settings_page, 'cnhk_admin_head_callback' );
  379. add_action( 'admin_head-' . $cnhk_main_page, 'cnhk_admin_head_callback' );
  380. add_action( 'load-' . $cnhk_slides_page, 'cnhk_admin_help' );
  381. add_action( 'load-' . $cnhk_settings_page, 'cnhk_admin_help' );
  382. add_action( 'load-' . $cnhk_main_page, 'cnhk_admin_help' );
  383. }
  384. /**
  385. * Display "Slideshow" page in admin panel
  386. */
  387. function cnhk_main_menu_fn() {
  388. include_once( 'cnhk-slideshow.php' );
  389. }
  390. /**
  391. * Display "Slides" page in admin panel
  392. */
  393. function cnhk_slide_fn() {
  394. if ( ! current_user_can( 'manage_options' ) ) {
  395. wp_die( 'You do not have sufficient permissions to access this page.' );
  396. }
  397. include_once( 'cnhk-slides.php' );
  398. }
  399. /**
  400. * Display slideshow in the front end
  401. */
  402. function cnhk_slideshow( $instance, $width = null, $align = null ) {
  403. $index;
  404. $fixed_width = $width;
  405. $fixed_align = $align;
  406. $ss = get_option( 'cnhk_options_ss' );
  407. $setting = get_option( 'cnhk_options' );
  408. if ( count( $ss['ss'] ) > 0 ) {
  409. if ( is_string( $instance ) ) {
  410. $index = array_search( $instance, $ss['ss'] );
  411. } else {
  412. $index = false;
  413. }
  414. if ( is_int( $index ) && $setting[7][$index] ) {
  415. if ( ! is_int( $fixed_width ) ) {
  416. $fixed_width = $setting[8][$index];
  417. }
  418. if ( ! is_string( $fixed_align ) ) {
  419. $fixed_align = $setting[9][$index];
  420. }
  421. }
  422. include ( 'cnhk-front.php' );
  423. }
  424. }
  425. /**
  426. * Display "Settings" page
  427. */
  428. function cnhk_setting_fn() {
  429. if ( ! current_user_can( 'manage_options' ) ) {
  430. wp_die( 'You do not have sufficient permissions to access this page.' );
  431. }
  432. require_once( 'settings.php' );
  433. }
  434. /**
  435. * Creates widget
  436. */
  437. function cnhk_widget() {
  438. register_widget( 'Cnhk_Ss_Widget' );
  439. }
  440. class Cnhk_Ss_Widget extends WP_Widget {
  441. function Cnhk_Ss_Widget() {
  442. $widget_ops = array( 'classname' => 'cnhk-ss-widget', 'description' => 'Use this widget to add one slideshow to one of your sidebar.' );
  443. $control_ops = array( 'id_base' => 'cnhk-ss-widget' );
  444. $this->WP_Widget( 'cnhk-ss-widget', 'Cnhk Slideshow', $widget_ops, $control_ops );
  445. }
  446. function widget( $args, $instance ) {
  447. extract( $args );
  448. $title = apply_filters( 'widget_title', $instance['title'] );
  449. $slideshow = $instance['slideshow'];
  450. echo $before_widget;
  451. if ( $title ) {
  452. echo $before_title . $title . $after_title;
  453. }
  454. if ( '' != $slideshow ) {
  455. cnhk_slideshow( $slideshow );
  456. }
  457. echo $after_widget;
  458. }
  459. function form( $instance ) {
  460. $defaults = array( 'title' => '', 'slideshow' => '' );
  461. $instance = wp_parse_args( ( array ) $instance, $defaults );
  462. $opt = get_option( 'cnhk_options' );
  463. if ( 0 < count( $opt[0] ) ) {
  464. echo '<p><label for="' . $this->get_field_id( 'title' ) . '">Title</label>
  465. <input type="text" class="widefat" id="' . $this->get_field_id( 'title' ) . '" name="' . $this->get_field_name( 'title' ) . '" value="' . $instance['title'] . '" /></p>';
  466. echo "<p><label for='" . $this->get_field_id( 'slideshow' ) . "'>Slideshow to display</label></p>";
  467. echo "<select name='" . $this->get_field_name( 'slideshow' ) . "' id='" .$this->get_field_id( 'slideshow' ) . "'>";
  468. echo "<option value=''";
  469. if ( !in_array( $instance['slideshow'], $opt[0] ) ) echo " selected='selected'";
  470. echo"></option>";
  471. for ( $i = 0; $i < count( $opt[0] ); $i++ ) {
  472. $selected = ( '' != $instance['slideshow'] && $instance['slideshow'] == $opt[0][$i] ) ? " selected='selected'" : '';
  473. echo "<option value='{$opt[0][$i]}'$selected>" . $opt[0][$i] . "</option>";
  474. }
  475. echo "</select>";
  476. } else {
  477. echo "<p class='error'>You need to create at least one slideshow. <a href='" . CNHK_ADMIN_SETTINGS . "'>Create one ?</a></p>";
  478. }
  479. }
  480. function update( $new_instance, $old_instance ) {
  481. $instance = $old_instance;
  482. $instance['title'] = strip_tags( $new_instance['title'] );
  483. $instance['slideshow'] = $new_instance['slideshow'];
  484. return $instance;
  485. }
  486. }
  487. /**
  488. * Add Shortcode
  489. */
  490. function cnhk_shortcode_fn( $atts, $content = null ) {
  491. extract( shortcode_atts( array( 'name' => '', 'width' => null, 'align' => null ), $atts ) );
  492. if ( ! empty( $width ) ) $width = intval( $width );
  493. if ( ! empty( $content ) ) {
  494. ob_start();
  495. cnhk_slideshow( $content, $width, $align );
  496. $output = ob_get_clean();
  497. return $output;
  498. } else {
  499. ob_start();
  500. cnhk_slideshow( $name, $width, $align );
  501. $output = ob_get_clean();
  502. return $output;
  503. }
  504. }
  505. add_shortcode( 'cnhk_slideshow', 'cnhk_shortcode_fn' );
  506. /**
  507. * Utility functions
  508. */
  509. function cnhk_get_snapsize( $slide_index ) {
  510. $slides = get_option( 'cnhk_options_slides' );
  511. $settings = get_option( 'cnhk_options' );
  512. $usage = $slides[1][$slide_index];
  513. $setting_height;
  514. $setting_width;
  515. if ( -1 != $usage ) {
  516. $setting_height = intval( $settings[4][$usage] );
  517. $setting_width = intval( $settings[5][$usage] );
  518. } else {
  519. list( $setting_width, $setting_height ) = getimagesize( CNHK_DIR . DIRECTORY_SEPARATOR . "uploads" . DIRECTORY_SEPARATOR . $slides[0][$slide_index] );
  520. }
  521. $snap_width;
  522. $snap_height;
  523. $setting_ratio = $setting_width / $setting_height;
  524. if ( $setting_height < $setting_width ) {
  525. $snap_width = 300;
  526. $snap_height = round( 300 / $setting_ratio );
  527. } else {
  528. $snap_width = 200;
  529. $snap_height = round( 200 / $setting_ratio );
  530. }
  531. $wideclass = ( 200 == $snap_width ) ? ' wide-200' : ' wide-300';
  532. $result = array( 'width' => $snap_width, 'height' => $snap_height, 'class' => $wideclass );
  533. return $result;
  534. }
  535. function cnhk_get_backgroundsize ( $slideshow_index ) {
  536. $settings = get_option( 'cnhk_options' );
  537. $setting_height;
  538. $setting_width;
  539. $setting_height = intval( $settings[4][$slideshow_index] );
  540. $setting_width = intval( $settings[5][$slideshow_index] );
  541. $bgr_width;
  542. $bgr_height;
  543. $setting_ratio = $setting_width / $setting_height;
  544. if ( $setting_height < $setting_width ) {
  545. $bgr_width = 300;
  546. $bgr_height = round( 300 / $setting_ratio );
  547. } else {
  548. $bgr_width = 200;
  549. $bgr_height = round( 200 / $setting_ratio );
  550. }
  551. $wideclass = ( 200 == $bgr_width ) ? ' wide-200' : ' wide-300';
  552. $result = array( 'width' => $bgr_width, 'height' => $bgr_height, 'class' => $wideclass );
  553. return $result;
  554. }
  555. function cnhk_background_image_url( $slideshow_index ) {
  556. $bgr_dir = CNHK_DIR . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'bgr' . DIRECTORY_SEPARATOR;
  557. $bgr_dir_url = CNHK_URL . 'uploads/bgr/';
  558. if ( file_exists( $bgr_dir . "bgr-{$slideshow_index}.jpg" ) ) {
  559. return $bgr_dir_url . "bgr-{$slideshow_index}.jpg";
  560. } elseif ( file_exists( $bgr_dir . "bgr-{$slideshow_index}.png" ) ) {
  561. return $bgr_dir_url . "bgr-{$slideshow_index}.png";
  562. }
  563. return false;
  564. }
  565. function cnhk_get_background_image_path( $slideshow_index ) {
  566. $bgr_dir = CNHK_DIR . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'bgr' . DIRECTORY_SEPARATOR;
  567. if ( file_exists( $bgr_dir . "bgr-{$slideshow_index}.jpg" ) ) {
  568. return $bgr_dir . "bgr-{$slideshow_index}.jpg";
  569. } elseif ( file_exists( $bgr_dir . "bgr-{$slideshow_index}.png" ) ) {
  570. return $bgr_dir . "bgr-{$slideshow_index}.png";
  571. }
  572. return false;
  573. }
  574. function cnhk_is_valid_title( $title ) {
  575. $pattern = "#^[\p{L}0-9?\!\. _-]{2,}$#iu";
  576. if ( preg_match( $pattern, $title ) == 1 ) {
  577. return true;
  578. } else {
  579. return false;
  580. }
  581. }
  582. function cnhk_make_slug( $value ) {
  583. $sch = array( "#(\s){1,}#iu", "#[éêèë]#iu", "#[îï]#iu", "#[öô]#iu", "#[ùüû]#iu", "#[àâä]#iu", "#[ç]#iu", "#[^éêèëïîöôùüûàâäç0-9a-zA-Z_ \.-]#iu" );
  584. $rpl = array( "_", "e", "i", "o", "u", "a", "c", "-" );
  585. return preg_replace( $sch, $rpl, $value );
  586. }