PageRenderTime 23ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://bitbucket.org/Wallynm/iptb
PHP | 987 lines | 723 code | 194 blank | 70 comment | 220 complexity | 08269103d2671d71f31d8bd249918916 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, GPL-2.0, GPL-3.0
  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 '<!-- ' . __('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 = $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. // extract the defaults...
  328. extract(
  329. shortcode_atts(
  330. array(
  331. /* default values: */
  332. // 'itemtag' => 'dl',
  333. // 'icontag' => 'dt',
  334. // 'captiontag' => 'dd',
  335. 'order' => 'ASC',
  336. 'orderby' => '',
  337. 'id' => $post->ID,
  338. 'columns' => 3,
  339. 'size' => 'thumbnail',
  340. 'link' => 'attachment',
  341. 'include' => '',
  342. 'exclude' => '',
  343. /* added by file gallery: */
  344. 'template' => 'default',
  345. 'linkclass' => '',
  346. 'imageclass' => '',
  347. 'galleryclass' => '',
  348. 'rel' => 1,
  349. 'tags' => '',
  350. 'tags_from' => 'current',
  351. 'output_type' => 'html',
  352. 'output_params' => 1, // needed when outputting html
  353. 'attachment_ids' => '', // alias of 'include'
  354. 'mimetype' => '',
  355. 'limit' => -1,
  356. 'offset' => -1,
  357. 'paginate' => 0,
  358. 'link_size' => 'full'
  359. )
  360. , $attr)
  361. );
  362. if( ! in_array($template, $default_templates) )
  363. {
  364. $template_file = FILE_GALLERY_THEME_TEMPLATES_ABSPATH . '/' . $template . '/gallery.php';
  365. if( ! is_readable($template_file) )
  366. $template_file = FILE_GALLERY_CONTENT_TEMPLATES_ABSPATH . '/' . $template . '/gallery.php';
  367. }
  368. else
  369. {
  370. if( 'default' == $template )
  371. {
  372. $template_file = FILE_GALLERY_DEFAULT_TEMPLATE_ABSPATH . '/gallery.php';
  373. $template = FILE_GALLERY_DEFAULT_TEMPLATE_NAME;
  374. }
  375. else
  376. {
  377. $template_file = FILE_GALLERY_ABSPATH . '/templates/' . $template . '/gallery.php';
  378. }
  379. }
  380. // check if template exists and replace with default if it does not
  381. if( ! is_readable($template_file) )
  382. {
  383. $template_file = FILE_GALLERY_ABSPATH . '/templates/default/gallery.php';
  384. $template = 'default';
  385. }
  386. // get overriding variables from the template file
  387. $overriding = true;
  388. ob_start();
  389. include($template_file);
  390. ob_end_clean();
  391. $overriding = false;
  392. if( is_array($file_gallery->overrides) && ! empty($file_gallery->overrides) )
  393. {
  394. extract($file_gallery->overrides);
  395. $file_gallery->overrides = NULL;
  396. }
  397. $limit = (int) $limit;
  398. $offset = (int) $offset;
  399. $page = (int) get_query_var('page');
  400. if( 'false' === $rel || (is_numeric($rel) && 0 === (int) $rel) )
  401. $_rel = false;
  402. elseif( 1 === $rel )
  403. $_rel = true;
  404. else
  405. $_rel = $rel;
  406. if( 'false' === $output_params || (is_numeric($output_params) && 0 === (int) $output_params) )
  407. $output_params = false;
  408. else
  409. $output_params = true;
  410. if( 'false' === $paginate || (is_numeric($paginate) && 0 === (int) $paginate) || 0 > $limit )
  411. {
  412. $paginate = false;
  413. $found_rows = '';
  414. }
  415. else
  416. {
  417. $paginate = true;
  418. $found_rows = 'SQL_CALC_FOUND_ROWS';
  419. if( 0 === $page )
  420. $page = 1;
  421. if( is_singular() && 1 < $page )
  422. $offset = $limit * ($page - 1);
  423. }
  424. $file_gallery->debug_add('pagination', compact('paginate', 'page'));
  425. if( '' != $include && '' == $attachment_ids )
  426. $attachment_ids = $include;
  427. if( ! isset( $linkto ) )
  428. $linkto = $link;
  429. $sql_mimetype = '';
  430. if( '' != $mimetype )
  431. {
  432. $mimetype = file_gallery_get_mime_type($mimetype);
  433. $sql_mimetype = wp_post_mime_type_where($mimetype);
  434. }
  435. $approved_attachment_post_statuses = apply_filters('file_gallery_approved_attachment_post_statuses', array('inherit', 'draft'));
  436. $ignored_attachment_post_statuses = apply_filters('file_gallery_ignored_attachment_post_statuses', array('trash', 'private', 'pending', 'future'));
  437. if( ! empty($approved_attachment_post_statuses) )
  438. $post_statuses = " AND ($wpdb->posts.post_status IN ('" . implode("', '", $approved_attachment_post_statuses) . "') ) ";
  439. elseif( ! empty($ignored_attachment_post_statuses) )
  440. $post_statuses = " AND ($wpdb->posts.post_status NOT IN ('" . implode("', '", $ignored_attachment_post_statuses) . "') ) ";
  441. else
  442. $post_statuses = "";
  443. $file_gallery_query = new stdClass();
  444. // start with tags because they negate everything else
  445. if( '' != $tags )
  446. {
  447. if( '' == $orderby || 'file_gallery' == $orderby )
  448. $orderby = "menu_order ID";
  449. $query = array(
  450. 'post_status' => implode(',', $approved_attachment_post_statuses),
  451. 'post_type' => 'attachment',
  452. 'order' => $order,
  453. 'orderby' => $orderby,
  454. 'posts_per_page' => $limit,
  455. 'post_mime_type' => $mimetype,
  456. FILE_GALLERY_MEDIA_TAG_NAME => $tags
  457. );
  458. if( 'current' == $tags_from )
  459. $query['post_parent'] = $id;
  460. if ( ! empty($exclude) )
  461. $query['post__not_in'] = explode(',', preg_replace( '/[^0-9,]+/', '', $exclude ));
  462. if( 0 < $offset )
  463. $query['offset'] = $offset;
  464. $file_gallery_query = new WP_Query( $query );
  465. $attachments = $file_gallery_query->posts;
  466. unset($query);
  467. }
  468. elseif( '' != $attachment_ids )
  469. {
  470. $attachment_ids = trim($attachment_ids, ',');
  471. $attachment_ids = explode(',', $attachment_ids);
  472. $sql_limit = count($attachment_ids);
  473. if( 'rand' == $orderby )
  474. shuffle($attachment_ids);
  475. $attachment_ids = implode(',', $attachment_ids);
  476. if( '' == $orderby || 'rand' == $orderby )
  477. {
  478. $orderby = sprintf("FIELD(ID,'%s')", str_replace(",", "','", $attachment_ids));
  479. $order = '';
  480. }
  481. elseif( 'title' == $orderby )
  482. {
  483. $orderby = "$wpdb->posts.post_title";
  484. }
  485. $query = sprintf(
  486. "SELECT " . $found_rows . " * FROM $wpdb->posts
  487. WHERE $wpdb->posts.ID IN (%s)
  488. AND $wpdb->posts.post_type = 'attachment'
  489. " . $post_statuses . " ",
  490. $attachment_ids);
  491. $query .= $sql_mimetype;
  492. $query .= sprintf(" ORDER BY %s %s ", $orderby, $order);
  493. if( true !== $paginate )
  494. $limit = $sql_limit;
  495. }
  496. else
  497. {
  498. if( '' == $orderby )
  499. $orderby = "menu_order ID";
  500. $query = array(
  501. 'post_parent' => $id,
  502. 'post_status' => implode(',', $approved_attachment_post_statuses),
  503. //'post_status' => $approved_attachment_post_statuses,
  504. 'post_type' => 'attachment',
  505. 'order' => $order,
  506. 'orderby' => $orderby,
  507. //'numberposts' => $limit,
  508. 'posts_per_page' => $limit,
  509. 'post_mime_type' => $mimetype
  510. );
  511. if ( ! empty($exclude) )
  512. $query['post__not_in'] = explode(',', preg_replace( '/[^0-9,]+/', '', $exclude ));
  513. if( 0 < $offset )
  514. $query['offset'] = $offset;
  515. $file_gallery_query = new WP_Query( $query );
  516. $attachments = $file_gallery_query->posts;
  517. unset($query);
  518. }
  519. if( isset($query) )
  520. {
  521. if( 0 < $limit )
  522. $query .= " LIMIT " . $limit;
  523. if( 0 < $offset )
  524. $query .= " OFFSET " . $offset;
  525. $attachments = $wpdb->get_results( $query );
  526. if( '' != $found_rows )
  527. {
  528. $file_gallery_query->found_posts = $wpdb->get_var("SELECT FOUND_ROWS()");
  529. $file_gallery_query->max_num_pages = ceil($file_gallery_query->found_posts / $limit);
  530. }
  531. }
  532. if( empty($attachments) )
  533. return '<!-- "File Gallery" plugin says: - No attachments found for the following shortcode arguments: "' . json_encode($attr) . '" -->';
  534. // feed
  535. if( is_feed() )
  536. {
  537. $output = "\n";
  538. foreach( $attachments as $attachment )
  539. {
  540. $output .= wp_get_attachment_link($attachment->ID, $size, true) . "\n";
  541. }
  542. return $output;
  543. }
  544. $i = 0;
  545. $unique_ids = array();
  546. $gallery_items = '';
  547. if( 'object' == $output_type || 'array' == $output_type )
  548. $gallery_items = array();
  549. $autoqueueclasses = array();
  550. if( defined('FILE_GALLERY_LIGHTBOX_CLASSES') )
  551. $autoqueueclasses = maybe_unserialize(FILE_GALLERY_LIGHTBOX_CLASSES);
  552. else
  553. $autoqueueclasses = explode(',', $options['auto_enqueued_scripts']);
  554. $file_gallery_this_template_counter = 1;
  555. // create output
  556. foreach($attachments as $attachment)
  557. {
  558. $param = array(
  559. 'image_class' => $imageclass,
  560. 'link_class' => $linkclass,
  561. 'rel' => $_rel,
  562. 'title' => '',
  563. 'caption' => '',
  564. 'description' => '',
  565. 'thumb_alt' => ''
  566. );
  567. $attachment_file = get_attached_file($attachment->ID);
  568. $attachment_is_image = file_gallery_file_is_displayable_image($attachment_file);
  569. $endcol = '';
  570. $x = '';
  571. if( $output_params )
  572. {
  573. $plcai = array_intersect($autoqueueclasses, explode(' ', trim($linkclass)));
  574. if( ! empty($plcai) )
  575. {
  576. if( $attachment_is_image )
  577. {
  578. if( true === $param['rel'] )
  579. {
  580. $param['rel'] = $plcai[0] . '[' . $file_gallery->gallery_id . ']';
  581. }
  582. elseif( ! is_bool($param['rel']) )
  583. {
  584. if( false !== strpos($_rel, '$GID$') )
  585. $param['rel'] = str_replace('$GID$', $file_gallery->gallery_id, $_rel);
  586. else
  587. $param['rel'] = $_rel . '[' . $file_gallery->gallery_id . ']';
  588. }
  589. $filter_args = array(
  590. 'gallery_id' => $file_gallery->gallery_id,
  591. 'linkrel' => $param['rel'],
  592. 'linkclass' => $param['link_class'],
  593. 'imageclass' => $param['image_class']
  594. );
  595. if( $param['rel'] )
  596. $param['rel'] = apply_filters('file_gallery_lightbox_linkrel', $param['rel'], 'linkrel', $filter_args);
  597. $param['link_class'] = apply_filters('file_gallery_lightbox_linkclass', $param['link_class'], 'linkclass', $filter_args);
  598. $param['image_class'] = apply_filters('file_gallery_lightbox_imageclass', $param['image_class'], 'imageclass', $filter_args);
  599. }
  600. else
  601. {
  602. $param['link_class'] = str_replace( trim(implode(' ', $plcai)), '', trim($linkclass));
  603. }
  604. }
  605. // if rel is still true or false
  606. if( is_bool($param['rel']) )
  607. $param['rel'] = '';
  608. switch( $linkto )
  609. {
  610. case 'parent_post' :
  611. $param['link'] = get_permalink( $wpdb->get_var("SELECT post_parent FROM $wpdb->posts WHERE ID = '" . $attachment->ID . "'") );
  612. break;
  613. case 'file' :
  614. $param['link'] = wp_get_attachment_url($attachment->ID);
  615. break;
  616. case 'attachment' :
  617. $param['link'] = get_attachment_link($attachment->ID);
  618. break;
  619. case 'none' :
  620. $param['link'] = '';
  621. break;
  622. default : // external url
  623. $param['link'] = urldecode($linkto);
  624. break;
  625. }
  626. $param['title'] = $attachment->post_title;
  627. $param['caption'] = $attachment->post_excerpt;
  628. $param['description'] = $attachment->post_content;
  629. if( $attachment_is_image )
  630. {
  631. $thumb_src = wp_get_attachment_image_src($attachment->ID, $size);
  632. $param['thumb_link'] = $thumb_src[0];
  633. $param['thumb_width'] = 0 == $thumb_src[1] ? file_gallery_get_image_size($param['thumb_link']) : $thumb_src[1];
  634. $param['thumb_height'] = 0 == $thumb_src[2] ? file_gallery_get_image_size($param['thumb_link'], true) : $thumb_src[2];
  635. if( '' != $param['link'] && 'full' != $link_size && in_array($link_size, file_gallery_get_intermediate_image_sizes()) )
  636. {
  637. $full_src = wp_get_attachment_image_src($attachment->ID, $link_size);
  638. $param['link'] = $full_src[0];
  639. }
  640. }
  641. else
  642. {
  643. $param['thumb_link'] = file_gallery_https( FILE_GALLERY_CRYSTAL_URL ) . '/' . file_gallery_get_file_type($attachment->post_mime_type) . '.png';
  644. $param['thumb_link'] = apply_filters('file_gallery_non_image_thumb_link', $param['thumb_link'], $attachment->post_mime_type, $attachment->ID);
  645. $param['thumb_width'] = '46';
  646. $param['thumb_height'] = '60';
  647. }
  648. if( $thumb_alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true) )
  649. $param['thumb_alt'] = $thumb_alt;
  650. $param['attachment_id'] = $attachment->ID;
  651. }
  652. $param = array_map('trim', $param);
  653. if( 'object' == $output_type )
  654. {
  655. if( $output_params )
  656. $attachment->params = (object) $param;
  657. $gallery_items[] = $attachment;
  658. }
  659. elseif( 'array' == $output_type || 'json' == $output_type)
  660. {
  661. if( $output_params )
  662. $attachment->params = $param;
  663. $gallery_items[] = get_object_vars($attachment);
  664. }
  665. else
  666. {
  667. // add the column break class and append a line break...
  668. if ( $columns > 0 && ++$i % $columns == 0 )
  669. $endcol = ' gallery-endcol';
  670. // parse template
  671. ob_start();
  672. extract( $param );
  673. include($template_file);
  674. $x = ob_get_contents();
  675. ob_end_clean();
  676. $file_gallery_this_template_counter++;
  677. if ( $columns > 0 && $i % $columns == 0 )
  678. $x .= $cleartag;
  679. $gallery_items .= $x;
  680. }
  681. }
  682. // handle data types
  683. if( 'object' == $output_type || 'array' == $output_type )
  684. {
  685. $output = $gallery_items;
  686. }
  687. elseif( 'json' == $output_type )
  688. {
  689. $output = json_encode($gallery_items);
  690. }
  691. else
  692. {
  693. $stc = '';
  694. $cols = '';
  695. $pagination_html = '';
  696. if( 0 < (int) $columns )
  697. $cols = ' columns_' . $columns;
  698. if( isset($starttag_class) && '' != $starttag_class )
  699. $stc = ' ' . $starttag_class;
  700. $trans_append = "\n<!-- file gallery output cached on " . date('Y.m.d @ H:i:s', time()) . "-->\n";
  701. if( is_singular() && isset($file_gallery_query->max_num_pages) && 1 < $file_gallery_query->max_num_pages )
  702. $pagination_html = file_gallery_do_pagination( $file_gallery_query->max_num_pages, $page );
  703. $gallery_class = apply_filters('file_gallery_galleryclass', 'gallery ' . str_replace(' ', '-', $template) . $cols . $stc . ' ' . $galleryclass);
  704. $output = '<' . $starttag . ' id="gallery-' . $file_gallery->gallery_id . '" class="' . $gallery_class . '">' . "\n" . $gallery_items . "\n" . $pagination_html . "\n</" . $starttag . '>';
  705. }
  706. if( isset($options['cache']) && true == $options['cache'] )
  707. {
  708. if( 'html' == $output_type )
  709. set_transient($transient, $output . $trans_append, $options['cache_time']); // with a comment appended to the end of cached output
  710. elseif( isset($options['cache_non_html_output']) && true == $options['cache_non_html_output'] )
  711. set_transient($transient, $output, $options['cache_time']);
  712. }
  713. return apply_filters('file_gallery_output', $output, $post->ID, $file_gallery->gallery_id);
  714. }
  715. /**
  716. * Built-in pagination for galleries
  717. *
  718. * @since 1.6.5.1
  719. */
  720. function file_gallery_do_pagination( $total = 0, $page = 0 )
  721. {
  722. if( 0 < $total && 0 < $page )
  723. {
  724. remove_query_arg('page');
  725. $options = get_option('file_gallery');
  726. $out = array('<span class="current">' . $page . '</span>');
  727. if( ! isset($options['pagination_count']) || empty($options['pagination_count']) || 0 >= $options['pagination_count'] )
  728. $limit = 9;
  729. else
  730. $limit = $options['pagination_count'];
  731. $c = 0;
  732. $l = $limit;
  733. $end = false;
  734. $start = false;
  735. $current = $page;
  736. $sides = ($limit - 1) / 2;
  737. $sl = ceil($sides);
  738. $sr = floor($sides);
  739. // skip to first page link
  740. if( ($limit - $sl) < $current )
  741. $start = true;
  742. // skip to last page link
  743. if( ($total - $sr) > $current )
  744. $end = true;
  745. // left side
  746. if( 1 < $current )
  747. {
  748. $current--;
  749. while( 0 < $current && 0 < $sl)
  750. {
  751. array_unshift($out, str_replace('<a ', '<a class="page"', _wp_link_page($current)) . $current . '</a>');
  752. $current--;
  753. $sl--;
  754. $limit--;
  755. }
  756. $c = $current;
  757. }
  758. $current = $page + 1;
  759. $sr += $sl;
  760. // right side
  761. while( $current <= $total && 0 < $sr )
  762. {
  763. array_push($out, str_replace('<a ', '<a class="page"', _wp_link_page($current)) . $current . '</a>');
  764. $current++;
  765. $sr--;
  766. $limit--;
  767. }
  768. // leftovers
  769. while( 1 < $limit && 0 < $c )
  770. {
  771. array_unshift($out, str_replace('<a ', '<a class="page"', _wp_link_page($c)) . $c . '</a>');
  772. $c--;
  773. $limit--;
  774. }
  775. if( $start )
  776. array_unshift($out, str_replace('<a ', '<a title="' . __('Skip to first page', 'file-gallery') . '" class="page"', _wp_link_page(1)) . '&laquo;</a>');
  777. if( $end )
  778. array_push($out, str_replace('<a ', '<a title="' . __('Skip to last page', 'file-gallery') . '" class="page"', _wp_link_page($total)) . '&raquo;</a>');
  779. if( 'rtl' == get_bloginfo('text_direction') )
  780. $out = array_reverse($out);
  781. return '<div class="wp-pagenavi">' . "\n" . implode("\n", $out) . "\n</div>";
  782. }
  783. return '';
  784. }
  785. function file_gallery_register_shortcode_handler()
  786. {
  787. $options = get_option('file_gallery');
  788. if( isset($options['disable_shortcode_handler']) && true == $options['disable_shortcode_handler'] )
  789. return;
  790. add_filter('post_gallery', 'file_gallery_shortcode', 10, 2);
  791. }
  792. add_action('init', 'file_gallery_register_shortcode_handler');
  793. ?>