PageRenderTime 64ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/file-gallery/includes/templating.php

https://bitbucket.org/antonyravel/cape-resorts
PHP | 1009 lines | 744 code | 199 blank | 66 comment | 223 complexity | 2c6a258163d4d5744c28a53fd79c0994 MD5 | raw file
  1. <?php
  2. /**
  3. * Collects template names from theme folder
  4. */
  5. function file_gallery_get_templates( $where = NULL )
  6. {
  7. $options = get_option('file_gallery');
  8. if( isset($options['cache']) && true == $options['cache'] )
  9. {
  10. $transient = 'filegallery_templates';
  11. $cache = get_transient($transient);
  12. if( $cache )
  13. return $cache;
  14. }
  15. $file_gallery_templates = array();
  16. // check if file gallery templates folder exists within theme folder
  17. if( is_readable(FILE_GALLERY_THEME_TEMPLATES_ABSPATH) )
  18. {
  19. $opendir = opendir(FILE_GALLERY_THEME_TEMPLATES_ABSPATH);
  20. while( false !== ($files = readdir($opendir)) )
  21. {
  22. if( '.' != $files && '..' != $files )
  23. {
  24. $tf = FILE_GALLERY_THEME_TEMPLATES_ABSPATH . '/' . $files;
  25. if( is_readable($tf . '/gallery.php') && is_readable($tf . '/gallery.css') )
  26. $file_gallery_templates[] = $files;
  27. }
  28. }
  29. closedir($opendir);
  30. }
  31. // check if file gallery templates folder exists within wp-content/file-gallery-templates folder
  32. if( is_readable(FILE_GALLERY_CONTENT_TEMPLATES_ABSPATH) )
  33. {
  34. $opendir = opendir(FILE_GALLERY_CONTENT_TEMPLATES_ABSPATH);
  35. while( false !== ($files = readdir($opendir)) )
  36. {
  37. if( '.' != $files && '..' != $files )
  38. {
  39. $tf = FILE_GALLERY_CONTENT_TEMPLATES_ABSPATH . '/' . $files;
  40. if( is_readable($tf . '/gallery.php') && is_readable($tf . '/gallery.css') )
  41. $file_gallery_templates[] = $files;
  42. }
  43. }
  44. closedir($opendir);
  45. }
  46. $file_gallery_templates = array_unique($file_gallery_templates);
  47. $default_templates = unserialize(FILE_GALLERY_DEFAULT_TEMPLATES);
  48. foreach( $default_templates as $df )
  49. {
  50. $file_gallery_templates[] = $df;
  51. }
  52. if( isset($options['cache']) && true == $options['cache'] )
  53. set_transient($transient, $file_gallery_templates, $options['cache_time']);
  54. return $file_gallery_templates;
  55. }
  56. /**
  57. * Injects CSS links via 'stylesheet_uri' filter, if mobile theme is active
  58. */
  59. function file_gallery_mobile_css( $stylesheet_url )
  60. {
  61. $options = get_option('file_gallery');
  62. if( isset($options['disable_shortcode_handler']) && true == $options['disable_shortcode_handler'] )
  63. return $stylesheet_url;
  64. file_gallery_css_front( true );
  65. $mobiles = maybe_unserialize(FILE_GALLERY_MOBILE_STYLESHEETS);
  66. if( !empty($mobiles) )
  67. {
  68. array_push($mobiles, $stylesheet_url);
  69. $glue = '" type="text/css" media="screen" charset="utf-8" />' . "\n\t" . '<link rel="stylesheet" href="';
  70. return implode($glue, $mobiles);
  71. }
  72. return $stylesheet_url;
  73. }
  74. /**
  75. * Enqueues stylesheets for each gallery template
  76. */
  77. function file_gallery_css_front( $mobile = false )
  78. {
  79. global $wp_query, $file_gallery;
  80. $options = get_option('file_gallery');
  81. if( isset($options['disable_shortcode_handler']) && true == $options['disable_shortcode_handler'] )
  82. return;
  83. // if option to show galleries in excerpts is set to false
  84. if( ! is_singular() && ( ! isset($options['in_excerpt']) || true != $options['in_excerpt']) && false == $mobile )
  85. return;
  86. $gallery_matches = 0;
  87. $galleries = array();
  88. $missing = array();
  89. $mobiles = array();
  90. $columns_required = false;
  91. $default_templates = unserialize(FILE_GALLERY_DEFAULT_TEMPLATES);
  92. // check for gallery shortcode in all posts
  93. if( ! empty($wp_query->posts) )
  94. {
  95. foreach( $wp_query->posts as $post )
  96. {
  97. $m = preg_match_all("#\[gallery([^\]]*)\]#is", $post->post_content, $g);
  98. // if there's a match...
  99. if( false !== $m && 0 < $m )
  100. {
  101. $gallery_matches += $m; // ...add the number of matches to global count...
  102. $galleries = array_merge($galleries, $g[1]); // ...and add the match to galleries array
  103. }
  104. }
  105. }
  106. // no matches...
  107. if( 0 === $gallery_matches )
  108. return;
  109. // automaticaly enqueue predefined scripts and styles
  110. $aqs = explode(',', $options['auto_enqueued_scripts']);
  111. $aqs = array_map('trim', $aqs);
  112. $aq_linkclasses = array();
  113. // collect template names
  114. foreach( $galleries as $gallery )
  115. {
  116. if( false === $columns_required )
  117. {
  118. $zc = preg_match("#\columns=(['\"])0\\1#is", $gallery);
  119. if( false !== $zc && 0 < $zc ) // no error and match found
  120. $columns_required = false;
  121. else
  122. $columns_required = true;
  123. }
  124. $tm = preg_match("#\stemplate=(['\"])([^'\"]+)\\1#is", $gallery, $gm);
  125. if( isset($gm[2]) )
  126. $templates[] = $gm[2];
  127. $gcm = preg_match("#\slinkclass=(['\"])([^'\"]+)\\1#is", $gallery, $gcg);
  128. if( isset($gcg[2]) && '' != $gcg[2] )
  129. {
  130. $glc = explode(' ', $gcg[2]);
  131. foreach( $glc as $glcs )
  132. {
  133. $glcs = trim($glcs);
  134. if( in_array($glcs, $aqs) )//if( false !== strpos( implode(' ', $aqs), $glcs) )
  135. $aq_linkclasses[] = $glcs;
  136. }
  137. }
  138. }
  139. $aq_linkclasses = apply_filters('file_gallery_lightbox_classes', array_unique($aq_linkclasses));
  140. // auto enqueue scripts
  141. if( ! empty($aq_linkclasses) )
  142. {
  143. if( ! defined('FILE_GALLERY_LIGHTBOX_CLASSES') )
  144. define('FILE_GALLERY_LIGHTBOX_CLASSES', serialize($aq_linkclasses));
  145. file_gallery_print_scripts( true );
  146. }
  147. if( empty($templates) )
  148. {
  149. // enqueue only the default stylesheet if no template names are found
  150. if( ! $mobile )
  151. wp_enqueue_style('file_gallery_default', FILE_GALLERY_DEFAULT_TEMPLATE_URL . '/gallery.css', false, FILE_GALLERY_VERSION);
  152. else
  153. $mobiles[] = FILE_GALLERY_DEFAULT_TEMPLATE_URL . '/gallery.css';
  154. }
  155. else
  156. {
  157. if( count($templates) < count($galleries) )
  158. $templates[] = 'default';
  159. // eliminate duplicate entries
  160. $templates = array_unique($templates);
  161. // if none of default templates are needed, don't include the 'columns.css' file
  162. if( array() == array_intersect($templates, $default_templates) )
  163. $columns_required = false;
  164. // walk through template names
  165. foreach($templates as $template)
  166. {
  167. $js_dependencies = isset($aq_linkclasses) ? $aq_linkclasses : array();
  168. // check if file exists in theme's folder
  169. if( is_readable(FILE_GALLERY_THEME_TEMPLATES_ABSPATH . '/' . $template . '/gallery.css') )
  170. {
  171. if( ! $mobile )
  172. wp_enqueue_style('file_gallery_' . str_replace(' ', '-', $template), FILE_GALLERY_THEME_TEMPLATES_URL . '/' . str_replace(' ', '%20', $template) . '/gallery.css', false, FILE_GALLERY_VERSION);
  173. else
  174. $mobiles[] = FILE_GALLERY_THEME_TEMPLATES_URL . '/' . str_replace(' ', '%20', $template) . '/gallery.css';
  175. if( is_readable(FILE_GALLERY_THEME_TEMPLATES_ABSPATH . '/' . $template . '/gallery.js') )
  176. {
  177. $overriding = true;
  178. ob_start();
  179. include(FILE_GALLERY_THEME_TEMPLATES_ABSPATH . '/' . $template . '/gallery.php');
  180. ob_end_clean();
  181. $overriding = false;
  182. wp_enqueue_script('file_gallery_' . str_replace(' ', '-', $template), FILE_GALLERY_THEME_TEMPLATES_URL . '/' . str_replace(' ', '%20', $template) . '/gallery.js', $js_dependencies, FILE_GALLERY_VERSION, true);
  183. }
  184. }
  185. // check if file exists in wp-content/file-gallery-templates folder
  186. elseif( is_readable(FILE_GALLERY_CONTENT_TEMPLATES_ABSPATH . '/' . $template . '/gallery.css') )
  187. {
  188. if( ! $mobile )
  189. wp_enqueue_style('file_gallery_' . str_replace(' ', '-', $template), FILE_GALLERY_CONTENT_TEMPLATES_URL . '/' . str_replace(' ', '%20', $template) . '/gallery.css', false, FILE_GALLERY_VERSION);
  190. else
  191. $mobiles[] = FILE_GALLERY_CONTENT_TEMPLATES_URL . '/' . str_replace(' ', '%20', $template) . '/gallery.css';
  192. if( is_readable(FILE_GALLERY_CONTENT_TEMPLATES_ABSPATH . '/' . $template . '/gallery.js') )
  193. {
  194. $overriding = true;
  195. ob_start();
  196. include(FILE_GALLERY_CONTENT_TEMPLATES_ABSPATH . '/' . $template . '/gallery.php');
  197. ob_end_clean();
  198. $overriding = false;
  199. wp_enqueue_script('file_gallery_' . str_replace(' ', '-', $template), FILE_GALLERY_CONTENT_TEMPLATES_URL . '/' . str_replace(' ', '%20', $template) . '/gallery.js', $js_dependencies, FILE_GALLERY_VERSION, true);
  200. }
  201. }
  202. // check plugin templates folder
  203. elseif( is_readable(FILE_GALLERY_ABSPATH . "/templates/" . $template . "/gallery.css") )
  204. {
  205. if( ! $mobile )
  206. wp_enqueue_style('file_gallery_' . $template, FILE_GALLERY_URL . '/templates/' . $template . '/gallery.css', false, FILE_GALLERY_VERSION);
  207. else
  208. $mobiles[] = FILE_GALLERY_URL . '/templates/' . $template . '/gallery.css';
  209. if( is_readable(FILE_GALLERY_ABSPATH . '/templates/' . $template . '/gallery.js') )
  210. {
  211. $overriding = true;
  212. ob_start();
  213. include(FILE_GALLERY_ABSPATH . '/templates/' . $template . '/gallery.php');
  214. ob_end_clean();
  215. $overriding = false;
  216. wp_enqueue_script('file_gallery_' . str_replace(' ', '-', $template), FILE_GALLERY_URL . '/templates/' . str_replace(' ', '%20', $template) . '/gallery.js', $js_dependencies, FILE_GALLERY_VERSION, true );
  217. }
  218. }
  219. // template does not exist, enqueue default one
  220. else
  221. {
  222. $missing[] = $template;
  223. wp_enqueue_style('file_gallery_default', FILE_GALLERY_URL . '/templates/default/gallery.css', false, FILE_GALLERY_VERSION);
  224. echo "\n<!-- " . __('file does not exist:', 'file-gallery') . ' ' . $template . '/gallery.css - ' . __('using default style', 'file-gallery') . "-->\n";
  225. }
  226. }
  227. }
  228. if( $columns_required )
  229. {
  230. if( ! $mobile )
  231. wp_enqueue_style('file_gallery_columns', FILE_GALLERY_URL . '/templates/columns.css', false, FILE_GALLERY_VERSION);
  232. else
  233. $mobiles[] = FILE_GALLERY_URL . '/templates/columns.css';
  234. }
  235. if( $mobile && ! defined('FILE_GALLERY_MOBILE_STYLESHEETS') )
  236. define('FILE_GALLERY_MOBILE_STYLESHEETS', serialize($mobiles));
  237. }
  238. add_action('wp_print_styles', 'file_gallery_css_front');
  239. add_action('wp_print_scripts', 'file_gallery_css_front');
  240. /**
  241. * prints scripts and styles for auto enqueued linkclasses
  242. */
  243. function file_gallery_print_scripts( $styles = false )
  244. {
  245. $options = get_option('file_gallery');
  246. if( isset($options['disable_shortcode_handler']) && true == $options['disable_shortcode_handler'] )
  247. return;
  248. if( defined('FILE_GALLERY_LIGHTBOX_CLASSES') )
  249. {
  250. $linkclasses = maybe_unserialize(FILE_GALLERY_LIGHTBOX_CLASSES);
  251. if( ! empty($linkclasses) )
  252. {
  253. foreach( $linkclasses as $lc )
  254. {
  255. if( $styles )
  256. {
  257. wp_enqueue_style( $lc );
  258. }
  259. else
  260. {
  261. if( 'thickbox' == $lc )
  262. {
  263. echo "\n" .
  264. '<script type="text/javascript">
  265. var tb_pathToImage = "' . includes_url() . 'js/thickbox/loadingAnimation.gif";
  266. var tb_closeImage = "' . includes_url() . 'js/thickbox/tb-close.png";
  267. </script>'
  268. . "\n";
  269. }
  270. wp_enqueue_script( $lc );
  271. }
  272. }
  273. }
  274. }
  275. }
  276. add_action('wp_print_scripts', 'file_gallery_print_scripts');
  277. /**
  278. * For easy inline overriding of shortcode-set options
  279. *
  280. * @since 1.6.5.1
  281. */
  282. function file_gallery_overrides( $args )
  283. {
  284. global $file_gallery;
  285. if( is_string($args) )
  286. $args = wp_parse_args($args);
  287. $file_gallery->overrides = $args;
  288. }
  289. /**
  290. * Main shortcode function
  291. *
  292. * @since 0.1
  293. */
  294. function file_gallery_shortcode( $content = false, $attr = false )
  295. {
  296. global $file_gallery, $wpdb, $post;
  297. // if the function is called directly, not via shortcode
  298. if( false !== $content && false === $attr )
  299. $attr = wp_parse_args($content);
  300. if( ! isset($file_gallery->gallery_id) )
  301. $file_gallery->gallery_id = 1;
  302. else
  303. $file_gallery->gallery_id++;
  304. $options = get_option('file_gallery');
  305. if( isset($options['cache']) && true == $options['cache'] )
  306. {
  307. if( 'html' == $attr['output_type'] || ( isset($options['cache_non_html_output']) && true == $options['cache_non_html_output'] ) )
  308. {
  309. $transient = 'filegallery_' . md5( $post->ID . "_" . serialize($attr) );
  310. $cache = get_transient($transient);
  311. if( $cache )
  312. return $cache;
  313. }
  314. }
  315. // if option to show galleries in excerpts is set to false...
  316. // ...replace [gallery] with user selected text
  317. if( ! is_singular() && ( ! isset($options['in_excerpt']) || true != $options['in_excerpt']) )
  318. return $options['in_excerpt_replace_content'];
  319. $default_templates = unserialize(FILE_GALLERY_DEFAULT_TEMPLATES);
  320. // We're trusting author input, so let's at least make sure it looks like a valid orderby statement
  321. if( isset($attr['orderby']) )
  322. {
  323. $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
  324. if( ! $attr['orderby'] )
  325. unset($attr['orderby']);
  326. }
  327. $defaults = array(
  328. /* default values: */
  329. 'order' => 'ASC',
  330. 'orderby' => '',
  331. 'id' => $post->ID,
  332. 'columns' => 3,
  333. 'size' => 'thumbnail',
  334. 'link' => 'attachment',
  335. 'include' => '',
  336. 'ids' => '',
  337. 'exclude' => '',
  338. /* added by file gallery: */
  339. 'template' => 'default',
  340. 'linkclass' => '',
  341. 'imageclass' => '',
  342. 'galleryclass' => '',
  343. 'rel' => 1,
  344. 'tags' => '',
  345. 'tags_from' => 'current',
  346. 'output_type' => 'html',
  347. 'output_params' => 1, // needed when outputting html
  348. 'attachment_ids' => '', // old alias of 'include' and 'ids'
  349. 'mimetype' => '',
  350. 'limit' => -1,
  351. 'offset' => -1,
  352. 'paginate' => 0,
  353. 'link_size' => 'full',
  354. 'include_meta' => false
  355. );
  356. if( floatval(get_bloginfo('version')) >= 3.5 ) {
  357. $defaults['link'] = 'post';
  358. }
  359. // extract the defaults...
  360. extract( shortcode_atts($defaults, $attr) );
  361. if( ! in_array($template, $default_templates) )
  362. {
  363. $template_file = FILE_GALLERY_THEME_TEMPLATES_ABSPATH . '/' . $template . '/gallery.php';
  364. if( ! is_readable($template_file) )
  365. $template_file = FILE_GALLERY_CONTENT_TEMPLATES_ABSPATH . '/' . $template . '/gallery.php';
  366. }
  367. else
  368. {
  369. if( 'default' == $template )
  370. {
  371. $template_file = FILE_GALLERY_DEFAULT_TEMPLATE_ABSPATH . '/gallery.php';
  372. $template = FILE_GALLERY_DEFAULT_TEMPLATE_NAME;
  373. }
  374. else
  375. {
  376. $template_file = FILE_GALLERY_ABSPATH . '/templates/' . $template . '/gallery.php';
  377. }
  378. }
  379. // check if template exists and replace with default if it does not
  380. if( ! is_readable($template_file) )
  381. {
  382. $template_file = FILE_GALLERY_ABSPATH . '/templates/default/gallery.php';
  383. $template = 'default';
  384. }
  385. // get overriding variables from the template file
  386. $overriding = true;
  387. ob_start();
  388. include($template_file);
  389. ob_end_clean();
  390. $overriding = false;
  391. if( is_array($file_gallery->overrides) && ! empty($file_gallery->overrides) )
  392. {
  393. extract($file_gallery->overrides);
  394. $file_gallery->overrides = NULL;
  395. }
  396. $limit = (int) $limit;
  397. $offset = (int) $offset;
  398. $page = (int) get_query_var('page');
  399. if( 'false' === $rel || (is_numeric($rel) && 0 === (int) $rel) )
  400. $_rel = false;
  401. elseif( 1 === $rel )
  402. $_rel = true;
  403. else
  404. $_rel = $rel;
  405. if( 'false' === $output_params || (is_numeric($output_params) && 0 === (int) $output_params) )
  406. $output_params = false;
  407. else
  408. $output_params = true;
  409. if( 'false' === $paginate || (is_numeric($paginate) && 0 === (int) $paginate) || 0 > $limit )
  410. {
  411. $paginate = false;
  412. $found_rows = '';
  413. }
  414. else
  415. {
  416. $paginate = true;
  417. $found_rows = 'SQL_CALC_FOUND_ROWS';
  418. if( 0 === $page )
  419. $page = 1;
  420. if( is_singular() && 1 < $page )
  421. $offset = $limit * ($page - 1);
  422. }
  423. $file_gallery->debug_add('pagination', compact('paginate', 'page'));
  424. /**/
  425. $_attachment_ids = explode(',', trim($attachment_ids, ','));
  426. $_include = explode(',', trim($include, ','));
  427. $_ids = explode(',', trim($ids, ','));
  428. $attachment_ids = array_merge($_attachment_ids, $_include, $_ids);
  429. $attachment_ids = array_unique($attachment_ids);
  430. $attachment_ids = implode(',', $attachment_ids);
  431. $attachment_ids = trim($attachment_ids, ',');
  432. $attachment_ids = trim($attachment_ids);
  433. /**/
  434. if( ! isset( $linkto ) )
  435. $linkto = $link;
  436. $sql_mimetype = '';
  437. if( '' != $mimetype )
  438. {
  439. $mimetype = file_gallery_get_mime_type($mimetype);
  440. $sql_mimetype = wp_post_mime_type_where($mimetype);
  441. }
  442. $approved_attachment_post_statuses = apply_filters('file_gallery_approved_attachment_post_statuses', array('inherit'));
  443. $ignored_attachment_post_statuses = apply_filters('file_gallery_ignored_attachment_post_statuses', array('trash', 'private', 'pending', 'future'));
  444. if( ! empty($approved_attachment_post_statuses) )
  445. $post_statuses = " AND (post_status IN ('" . implode("', '", $approved_attachment_post_statuses) . "') ) ";
  446. elseif( ! empty($ignored_attachment_post_statuses) )
  447. $post_statuses = " AND (post_status NOT IN ('" . implode("', '", $ignored_attachment_post_statuses) . "') ) ";
  448. else
  449. $post_statuses = "";
  450. $file_gallery_query = new stdClass();
  451. // start with tags because they negate everything else
  452. if( '' != $tags )
  453. {
  454. if( '' == $orderby || 'file_gallery' == $orderby )
  455. $orderby = "menu_order ID";
  456. $query = array(
  457. 'post_status' => implode(',', $approved_attachment_post_statuses),
  458. 'post_type' => 'attachment',
  459. 'order' => $order,
  460. 'orderby' => $orderby,
  461. 'posts_per_page' => $limit,
  462. 'post_mime_type' => $mimetype,
  463. FILE_GALLERY_MEDIA_TAG_NAME => $tags
  464. );
  465. if( 'current' == $tags_from )
  466. $query['post_parent'] = $id;
  467. if ( ! empty($exclude) )
  468. $query['post__not_in'] = explode(',', preg_replace( '/[^0-9,]+/', '', $exclude ));
  469. if( 0 < $offset )
  470. $query['offset'] = $offset;
  471. $file_gallery_query = new WP_Query( $query );
  472. $attachments = $file_gallery_query->posts;
  473. unset($query);
  474. }
  475. elseif( '' != $attachment_ids )
  476. {
  477. $attachment_ids = explode(',', $attachment_ids);
  478. $sql_limit = count($attachment_ids);
  479. if( 'rand' == $orderby )
  480. shuffle($attachment_ids);
  481. $attachment_ids = implode(',', $attachment_ids);
  482. if( '' == $orderby || 'rand' == $orderby || $orderby == 'post__in' )
  483. {
  484. $orderby = sprintf("FIELD(ID, %s)", $attachment_ids);
  485. $order = '';
  486. }
  487. elseif( 'title' == $orderby )
  488. {
  489. $orderby = "post_title";
  490. }
  491. $query = sprintf(
  492. "SELECT " . $found_rows . " * FROM $wpdb->posts
  493. WHERE ID IN (%s)
  494. AND post_type = 'attachment'
  495. " . $post_statuses . " ",
  496. $attachment_ids);
  497. $query .= $sql_mimetype;
  498. $query .= sprintf(" ORDER BY %s %s ", $orderby, $order);
  499. if( true !== $paginate )
  500. $limit = $sql_limit;
  501. }
  502. else
  503. {
  504. if( '' == $orderby )
  505. $orderby = "menu_order ID";
  506. $query = array(
  507. 'post_parent' => $id,
  508. 'post_status' => implode(',', $approved_attachment_post_statuses),
  509. 'post_type' => 'attachment',
  510. 'order' => $order,
  511. 'orderby' => $orderby,
  512. 'posts_per_page' => $limit,
  513. 'post_mime_type' => $mimetype
  514. );
  515. if ( ! empty($exclude) )
  516. $query['post__not_in'] = explode(',', preg_replace( '/[^0-9,]+/', '', $exclude ));
  517. if( 0 < $offset )
  518. $query['offset'] = $offset;
  519. $file_gallery_query = new WP_Query( $query );
  520. $attachments = $file_gallery_query->posts;
  521. unset($query);
  522. }
  523. if( isset($query) )
  524. {
  525. if( 0 < $limit )
  526. $query .= " LIMIT " . $limit;
  527. if( 0 < $offset )
  528. $query .= " OFFSET " . $offset;
  529. $attachments = $wpdb->get_results( $query );
  530. if( '' != $found_rows )
  531. {
  532. $file_gallery_query->found_posts = $wpdb->get_var("SELECT FOUND_ROWS()");
  533. $file_gallery_query->max_num_pages = ceil($file_gallery_query->found_posts / $limit);
  534. }
  535. }
  536. $file_gallery->debug_add('attachments_query', compact('file_gallery_query'));
  537. if( empty($attachments) )
  538. return '<!-- "File Gallery" plugin says: - No attachments found for the following shortcode arguments: "' . json_encode($attr) . '" -->';
  539. // feed
  540. if( is_feed() )
  541. {
  542. $output = "\n";
  543. foreach( $attachments as $attachment )
  544. {
  545. $output .= wp_get_attachment_link($attachment->ID, $size, true) . "\n";
  546. }
  547. return $output;
  548. }
  549. $i = 0;
  550. $unique_ids = array();
  551. $gallery_items = '';
  552. if( 'object' == $output_type || 'array' == $output_type )
  553. $gallery_items = array();
  554. $autoqueueclasses = array();
  555. if( defined('FILE_GALLERY_LIGHTBOX_CLASSES') )
  556. $autoqueueclasses = maybe_unserialize(FILE_GALLERY_LIGHTBOX_CLASSES);
  557. else
  558. $autoqueueclasses = explode(',', $options['auto_enqueued_scripts']);
  559. $file_gallery_this_template_counter = 1;
  560. // create output
  561. foreach($attachments as $attachment)
  562. {
  563. $param = array(
  564. 'image_class' => $imageclass,
  565. 'link_class' => $linkclass,
  566. 'rel' => $_rel,
  567. 'title' => '',
  568. 'caption' => '',
  569. 'description' => '',
  570. 'thumb_alt' => ''
  571. );
  572. $attachment_file = get_attached_file($attachment->ID);
  573. $attachment_is_image = file_gallery_file_is_displayable_image($attachment_file);
  574. $startcol = '';
  575. $endcol = '';
  576. $x = '';
  577. if( $output_params )
  578. {
  579. $plcai = array_intersect($autoqueueclasses, explode(' ', trim($linkclass)));
  580. if( ! empty($plcai) )
  581. {
  582. if( $attachment_is_image )
  583. {
  584. if( true === $param['rel'] )
  585. {
  586. $param['rel'] = $plcai[0] . '[' . $file_gallery->gallery_id . ']';
  587. }
  588. elseif( ! is_bool($param['rel']) )
  589. {
  590. if( false !== strpos($_rel, '$GID$') )
  591. $param['rel'] = str_replace('$GID$', $file_gallery->gallery_id, $_rel);
  592. else
  593. $param['rel'] = $_rel . '[' . $file_gallery->gallery_id . ']';
  594. }
  595. $filter_args = array(
  596. 'gallery_id' => $file_gallery->gallery_id,
  597. 'linkrel' => $param['rel'],
  598. 'linkclass' => $param['link_class'],
  599. 'imageclass' => $param['image_class']
  600. );
  601. if( $param['rel'] )
  602. $param['rel'] = apply_filters('file_gallery_lightbox_linkrel', $param['rel'], 'linkrel', $filter_args);
  603. $param['link_class'] = apply_filters('file_gallery_lightbox_linkclass', $param['link_class'], 'linkclass', $filter_args);
  604. $param['image_class'] = apply_filters('file_gallery_lightbox_imageclass', $param['image_class'], 'imageclass', $filter_args);
  605. }
  606. else
  607. {
  608. $param['link_class'] = str_replace( trim(implode(' ', $plcai)), '', trim($linkclass));
  609. }
  610. }
  611. // if rel is still true or false
  612. if( is_bool($param['rel']) )
  613. $param['rel'] = '';
  614. switch( $linkto )
  615. {
  616. case 'parent_post' :
  617. $param['link'] = get_permalink( $wpdb->get_var("SELECT post_parent FROM $wpdb->posts WHERE ID = '" . $attachment->ID . "'") );
  618. break;
  619. case 'file' :
  620. $param['link'] = wp_get_attachment_url($attachment->ID);
  621. break;
  622. case 'attachment' :
  623. case 'post' :
  624. $param['link'] = get_attachment_link($attachment->ID);
  625. break;
  626. case 'none' :
  627. $param['link'] = '';
  628. break;
  629. default : // external url
  630. $param['link'] = urldecode($linkto);
  631. break;
  632. }
  633. $param['title'] = $attachment->post_title;
  634. $param['caption'] = $attachment->post_excerpt;
  635. $param['description'] = $attachment->post_content;
  636. if( $attachment_is_image )
  637. {
  638. $thumb_src = wp_get_attachment_image_src($attachment->ID, $size);
  639. $param['thumb_link'] = $thumb_src[0];
  640. $param['thumb_width'] = 0 == $thumb_src[1] ? file_gallery_get_image_size($param['thumb_link']) : $thumb_src[1];
  641. $param['thumb_height'] = 0 == $thumb_src[2] ? file_gallery_get_image_size($param['thumb_link'], true) : $thumb_src[2];
  642. if( '' != $param['link'] && 'full' != $link_size && in_array($link_size, file_gallery_get_intermediate_image_sizes()) )
  643. {
  644. $full_src = wp_get_attachment_image_src($attachment->ID, $link_size);
  645. $param['link'] = $full_src[0];
  646. }
  647. }
  648. else
  649. {
  650. $param['thumb_link'] = wp_mime_type_icon($attachment->ID);
  651. $param['thumb_link'] = apply_filters('file_gallery_non_image_thumb_link', $param['thumb_link'], $attachment->post_mime_type, $attachment->ID);
  652. $param['thumb_width'] = '46';
  653. $param['thumb_height'] = '60';
  654. }
  655. if( $thumb_alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true) )
  656. $param['thumb_alt'] = $thumb_alt;
  657. $param['attachment_id'] = $attachment->ID;
  658. }
  659. $param = array_map('trim', $param);
  660. if( $include_meta )
  661. $meta = get_post_custom($attachment->ID);
  662. if( 'object' == $output_type )
  663. {
  664. if( $output_params )
  665. $attachment->params = (object) $param;
  666. if( $include_meta )
  667. $attachment->meta = (object) $meta;
  668. $gallery_items[] = $attachment;
  669. }
  670. elseif( 'array' == $output_type || 'json' == $output_type)
  671. {
  672. if( $output_params )
  673. $attachment->params = $param;
  674. if( $include_meta )
  675. $attachment->meta = $meta;
  676. $gallery_items[] = get_object_vars($attachment);
  677. }
  678. else
  679. {
  680. if( $columns > 0 )
  681. {
  682. if( 0 === $i || 0 === $i % $columns )
  683. $startcol = ' gallery-startcol';
  684. elseif( ($i+1) % $columns == 0 )// add the column break class
  685. $endcol = ' gallery-endcol';
  686. }
  687. // parse template
  688. ob_start();
  689. extract( $param );
  690. include($template_file);
  691. $x = ob_get_contents();
  692. ob_end_clean();
  693. $file_gallery_this_template_counter++;
  694. if ( $columns > 0 && $i+1 % $columns == 0 )
  695. $x .= $cleartag;
  696. $gallery_items .= $x;
  697. $i++;
  698. }
  699. }
  700. // handle data types
  701. if( 'object' == $output_type || 'array' == $output_type )
  702. {
  703. $output = $gallery_items;
  704. }
  705. elseif( 'json' == $output_type )
  706. {
  707. $output = json_encode($gallery_items);
  708. }
  709. else
  710. {
  711. $stc = '';
  712. $cols = '';
  713. $pagination_html = '';
  714. if( 0 < (int) $columns )
  715. $cols = ' columns_' . $columns;
  716. if( isset($starttag_class) && '' != $starttag_class )
  717. $stc = ' ' . $starttag_class;
  718. $trans_append = "\n<!-- file gallery output cached on " . date('Y.m.d @ H:i:s', time()) . "-->\n";
  719. if( is_singular() && isset($file_gallery_query->max_num_pages) && 1 < $file_gallery_query->max_num_pages )
  720. $pagination_html = file_gallery_do_pagination( $file_gallery_query->max_num_pages, $page );
  721. $gallery_class = apply_filters('file_gallery_galleryclass', 'gallery ' . str_replace(' ', '-', $template) . $cols . $stc . ' ' . $galleryclass);
  722. $output = '<' . $starttag . ' id="gallery-' . $file_gallery->gallery_id . '" class="' . $gallery_class . '">' . "\n" . $gallery_items . "\n" . $pagination_html . "\n</" . $starttag . '>';
  723. }
  724. if( isset($options['cache']) && true == $options['cache'] )
  725. {
  726. if( 'html' == $output_type )
  727. set_transient($transient, $output . $trans_append, $options['cache_time']); // with a comment appended to the end of cached output
  728. elseif( isset($options['cache_non_html_output']) && true == $options['cache_non_html_output'] )
  729. set_transient($transient, $output, $options['cache_time']);
  730. }
  731. return apply_filters('file_gallery_output', $output, $post->ID, $file_gallery->gallery_id);
  732. }
  733. /**
  734. * Built-in pagination for galleries
  735. *
  736. * @since 1.6.5.1
  737. */
  738. function file_gallery_do_pagination( $total = 0, $page = 0 )
  739. {
  740. if( 0 < $total && 0 < $page )
  741. {
  742. remove_query_arg('page');
  743. $options = get_option('file_gallery');
  744. $out = array('<span class="current">' . $page . '</span>');
  745. if( ! isset($options['pagination_count']) || empty($options['pagination_count']) || 0 >= $options['pagination_count'] )
  746. $limit = 9;
  747. else
  748. $limit = $options['pagination_count'];
  749. $c = 0;
  750. $l = $limit;
  751. $end = false;
  752. $start = false;
  753. $current = $page;
  754. $sides = ($limit - 1) / 2;
  755. $sl = ceil($sides);
  756. $sr = floor($sides);
  757. // skip to first page link
  758. if( ($limit - $sl) < $current )
  759. $start = true;
  760. // skip to last page link
  761. if( ($total - $sr) > $current )
  762. $end = true;
  763. // left side
  764. if( 1 < $current )
  765. {
  766. $current--;
  767. while( 0 < $current && 0 < $sl)
  768. {
  769. array_unshift($out, str_replace('<a ', '<a class="page"', _wp_link_page($current)) . $current . '</a>');
  770. $current--;
  771. $sl--;
  772. $limit--;
  773. }
  774. $c = $current;
  775. }
  776. $current = $page + 1;
  777. $sr += $sl;
  778. // right side
  779. while( $current <= $total && 0 < $sr )
  780. {
  781. array_push($out, str_replace('<a ', '<a class="page"', _wp_link_page($current)) . $current . '</a>');
  782. $current++;
  783. $sr--;
  784. $limit--;
  785. }
  786. // leftovers
  787. while( 1 < $limit && 0 < $c )
  788. {
  789. array_unshift($out, str_replace('<a ', '<a class="page"', _wp_link_page($c)) . $c . '</a>');
  790. $c--;
  791. $limit--;
  792. }
  793. if( $start )
  794. array_unshift($out, str_replace('<a ', '<a title="' . __('Skip to first page', 'file-gallery') . '" class="page"', _wp_link_page(1)) . '&laquo;</a>');
  795. if( $end )
  796. array_push($out, str_replace('<a ', '<a title="' . __('Skip to last page', 'file-gallery') . '" class="page"', _wp_link_page($total)) . '&raquo;</a>');
  797. if( 'rtl' == get_bloginfo('text_direction') )
  798. $out = array_reverse($out);
  799. return '<div class="wp-pagenavi">' . "\n" . implode("\n", $out) . "\n</div>";
  800. }
  801. return '';
  802. }
  803. function file_gallery_register_shortcode_handler()
  804. {
  805. $options = get_option('file_gallery');
  806. if( isset($options['disable_shortcode_handler']) && true == $options['disable_shortcode_handler'] )
  807. return;
  808. add_filter('post_gallery', 'file_gallery_shortcode', 10, 2);
  809. }
  810. add_action('init', 'file_gallery_register_shortcode_handler');