PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/Wordpress-Project/wp-content/plugins/foogallery/includes/class-foogallery.php

https://bitbucket.org/hinzanhilmy/hinzan-sample-works
PHP | 477 lines | 247 code | 65 blank | 165 comment | 28 complexity | 17afb39510a9090e00c618497fe9bdc4 MD5 | raw file
Possible License(s): GPL-3.0, 0BSD, Apache-2.0, BSD-2-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1, GPL-2.0, BSD-3-Clause, MIT
  1. <?php
  2. /**
  3. * Class FooGallery
  4. *
  5. * An easy to use wrapper class for a FooGallery gallery post
  6. */
  7. class FooGallery extends stdClass {
  8. /**
  9. * private constructor
  10. *
  11. * @param null $post
  12. */
  13. private function __construct( $post = null ) {
  14. $this->set_defaults();
  15. if ( $post !== null ) {
  16. $this->load( $post );
  17. }
  18. }
  19. /**
  20. * Sets the default when a new gallery is instantiated
  21. */
  22. private function set_defaults() {
  23. $this->_post = null;
  24. $this->ID = 0;
  25. $this->attachment_ids = array();
  26. $this->_attachments = false;
  27. $this->datasource_name = foogallery_default_datasource();
  28. $this->_datasource = false;
  29. $this->settings = array();
  30. }
  31. /**
  32. * private gallery load function
  33. * @param $post
  34. */
  35. private function load( $post ) {
  36. $this->_post = $post;
  37. $this->ID = $post->ID;
  38. $this->slug = $post->post_name;
  39. $this->name = $post->post_title;
  40. $this->author = $post->post_author;
  41. $this->post_status = $post->post_status;
  42. $attachment_meta = get_post_meta( $post->ID, FOOGALLERY_META_ATTACHMENTS, true );
  43. $this->attachment_ids = is_array( $attachment_meta ) ? array_filter( $attachment_meta ) : array();
  44. $this->load_meta( $post->ID );
  45. do_action( 'foogallery_instance_after_load', $this, $post );
  46. }
  47. /**
  48. * private meta data load function
  49. * @param $post_id int
  50. */
  51. private function load_meta( $post_id ) {
  52. $this->gallery_template = get_post_meta( $post_id, FOOGALLERY_META_TEMPLATE, true );
  53. $this->settings = $this->load_settings( $post_id );
  54. $this->custom_css = get_post_meta( $post_id, FOOGALLERY_META_CUSTOM_CSS, true );
  55. $this->sorting = get_post_meta( $post_id, FOOGALLERY_META_SORT, true );
  56. $this->datasource_name = get_post_meta( $post_id, FOOGALLERY_META_DATASOURCE, true );
  57. if ( empty( $this->datasource_name ) ) {
  58. $this->datasource_name = foogallery_default_datasource();
  59. }
  60. $this->retina = get_post_meta( $post_id, FOOGALLERY_META_RETINA, true );
  61. $this->force_use_original_thumbs = 'true' === get_post_meta( $post_id, FOOGALLERY_META_FORCE_ORIGINAL_THUMBS, true );
  62. }
  63. private function load_settings( $post_id ) {
  64. $settings = get_post_meta( $post_id, FOOGALLERY_META_SETTINGS, true );
  65. //the gallery is considered new if the template has not been set
  66. $is_new = empty( $this->gallery_template );
  67. //if we have no settings, and the gallery is not new, then allow for an upgrade
  68. if ( empty( $settings ) && !$is_new ) {
  69. $settings = apply_filters( 'foogallery_settings_upgrade', $settings, $this );
  70. }
  71. //if we still have no settings, then get default settings for the gallery template
  72. if ( empty( $settings ) && !$is_new ) {
  73. $settings = foogallery_build_default_settings_for_gallery_template( $this->gallery_template );
  74. $settings = apply_filters('foogallery_default_settings-' . $this->gallery_template, $settings, $this);
  75. }
  76. return $settings;
  77. }
  78. /**
  79. * private function to load a gallery by an id
  80. * @param $post_id
  81. */
  82. private function load_by_id( $post_id ) {
  83. $post = get_post( $post_id );
  84. if ( $post ) {
  85. $this->load( $post );
  86. }
  87. }
  88. /**
  89. * private function to load a gallery by the slug.
  90. * Will be used when loading gallery shortcodes
  91. * @param $slug
  92. */
  93. private function load_by_slug( $slug ) {
  94. if ( ! empty( $slug ) ) {
  95. $args = array(
  96. 'name' => $slug,
  97. 'numberposts' => 1,
  98. 'post_type' => FOOGALLERY_CPT_GALLERY,
  99. );
  100. $galleries = get_posts( $args );
  101. if ( $galleries ) {
  102. $this->load( $galleries[0] );
  103. }
  104. }
  105. }
  106. /**
  107. * Static function to build a dynamic gallery that does not exist in the database
  108. * @param $template
  109. * @param $attachment_ids
  110. *
  111. * @return FooGallery
  112. */
  113. public static function dynamic( $template, $attachment_ids ) {
  114. $gallery = new self( null );
  115. $gallery->gallery_template = $template;
  116. $gallery->attachment_ids = $attachment_ids;
  117. //loads all meta data from the default gallery
  118. $default_gallery_id = foogallery_get_setting( 'default_gallery_settings' );
  119. if ( $default_gallery_id > 0 ) {
  120. $gallery->load_meta( $default_gallery_id );
  121. }
  122. return $gallery;
  123. }
  124. /**
  125. * Static function to load a Gallery instance by passing in a post object
  126. * @static
  127. *
  128. * @param $post
  129. *
  130. * @return FooGallery
  131. */
  132. public static function get( $post ) {
  133. return new self( $post );
  134. }
  135. /**
  136. * Static function to load a Gallery instance by post id
  137. *
  138. * @param $post_id
  139. *
  140. * @return FooGallery | boolean
  141. */
  142. public static function get_by_id( $post_id ) {
  143. $gallery = new self();
  144. $gallery->load_by_id( $post_id );
  145. if ( ! $gallery->does_exist() ) {
  146. return false;
  147. }
  148. return $gallery;
  149. }
  150. /**
  151. * Static function to load a gallery instance by passing in a gallery slug
  152. *
  153. * @param string $slug
  154. *
  155. * @return FooGallery | boolean
  156. */
  157. public static function get_by_slug( $slug ) {
  158. $gallery = new self();
  159. $gallery->load_by_slug( $slug );
  160. if ( ! $gallery->does_exist() ) {
  161. return false;
  162. }
  163. return $gallery;
  164. }
  165. /**
  166. * Get a setting using the current template and meta key
  167. * @param $key
  168. * @param $default
  169. *
  170. * @return mixed|null
  171. */
  172. function get_setting( $key, $default ) {
  173. return $this->get_meta( "{$this->gallery_template}_$key", $default );
  174. }
  175. /**
  176. * Get a meta value using a full key
  177. * @param $key
  178. * @param $default
  179. *
  180. * @return mixed|null
  181. */
  182. function get_meta( $key, $default ) {
  183. if ( ! is_array( $this->settings ) ) {
  184. return $default;
  185. }
  186. $value = array_key_exists( $key, $this->settings ) ? $this->settings[ $key ] : null;
  187. if ( $value === null ) {
  188. return $default;
  189. }
  190. return $value;
  191. }
  192. function is_checked( $key, $default = false ) {
  193. if ( ! is_array( $this->settings ) ) {
  194. return $default;
  195. }
  196. return array_key_exists( $key, $this->settings );
  197. }
  198. /**
  199. * Returns the number of attachments in the current gallery
  200. * @return int
  201. */
  202. public function attachment_count() {
  203. return $this->datasource()->getCount();
  204. }
  205. /**
  206. * Checks if the gallery has attachments
  207. * @return bool
  208. */
  209. public function has_attachments() {
  210. return $this->attachment_count() > 0;
  211. }
  212. /**
  213. * Checks if the gallery exists
  214. * @return bool
  215. */
  216. public function does_exist() {
  217. return $this->ID > 0;
  218. }
  219. /**
  220. * Returns true if the gallery is published
  221. * @return bool
  222. */
  223. public function is_published() {
  224. return $this->post_status === 'publish';
  225. }
  226. /**
  227. * Returns true if the gallery is newly created and not yet saved
  228. */
  229. public function is_new() {
  230. $template = get_post_meta( $this->ID, FOOGALLERY_META_TEMPLATE, true );
  231. return empty( $template );
  232. }
  233. /**
  234. * Get a comma separated list of attachment ids
  235. * @return string
  236. */
  237. public function attachment_id_csv() {
  238. return $this->datasource()->getSerializedData();
  239. }
  240. /**
  241. * Lazy load the attachments for the gallery
  242. *
  243. * @return array
  244. */
  245. public function attachments() {
  246. //lazy load the attachments for performance
  247. if ( $this->_attachments === false ) {
  248. $this->_attachments = $this->datasource()->getAttachments();
  249. }
  250. return $this->_attachments;
  251. }
  252. /**
  253. * @deprecated 1.3.0 This is now moved into the datasource implementation
  254. *
  255. * This forces the attachments to be fetched using the correct ordering.
  256. * Some plugins / themes override this globally for some reason, so this is a preventative measure to ensure sorting is correct
  257. * @param $query WP_Query
  258. */
  259. public function force_gallery_ordering( $query ) {
  260. _deprecated_function( __FUNCTION__, '1.3.0' );
  261. }
  262. /**
  263. * Output the shortcode for the gallery
  264. *
  265. * @return string
  266. */
  267. public function shortcode() {
  268. return foogallery_build_gallery_shortcode( $this->ID );
  269. }
  270. /**
  271. * @deprecated 1.3.0 This is now moved into the datasource implementation
  272. *
  273. * @return int|mixed|string
  274. */
  275. public function find_featured_attachment_id() {
  276. _deprecated_function( __FUNCTION__, '1.3.0' );
  277. return 0;
  278. }
  279. /**
  280. * Gets the featured image FooGalleryAttachment object. If no featured image is set, then get back the first image in the gallery
  281. *
  282. * @return bool|FooGalleryAttachment
  283. */
  284. public function featured_attachment() {
  285. return $this->datasource()->getFeaturedAttachment();
  286. }
  287. /**
  288. * @deprecated 1.3.0 This is now moved into the datasource implementation
  289. *
  290. * @param string $size
  291. * @param bool $icon
  292. *
  293. * @return bool
  294. */
  295. public function featured_image_src( $size = 'thumbnail', $icon = false ) {
  296. _deprecated_function( __FUNCTION__, '1.3.0' );
  297. return false;
  298. }
  299. /**
  300. * @deprecated 1.3.0 This is now moved into the datasource implementation
  301. *
  302. * Get an HTML img element representing the featured image for the gallery
  303. *
  304. * @param string $size Optional, default is 'thumbnail'.
  305. * @param bool $icon Optional, default is false. Whether it is an icon.
  306. *
  307. * @return string HTML img element or empty string on failure.
  308. */
  309. public function featured_image_html( $size = 'thumbnail', $icon = false ) {
  310. _deprecated_function( __FUNCTION__, '1.3.0' );
  311. return '';
  312. }
  313. public function image_count() {
  314. $no_images_text = foogallery_get_setting( 'language_images_count_none_text', __( 'No images', 'foogallery' ) );
  315. $singular_text = foogallery_get_setting( 'language_images_count_single_text', __( '1 image', 'foogallery' ) );
  316. $plural_text = foogallery_get_setting( 'language_images_count_plural_text', __( '%s images', 'foogallery' ) );
  317. $count = $this->attachment_count();
  318. switch ( $count ) {
  319. case 0:
  320. $count_text = $no_images_text === false ? __( 'No images', 'foogallery' ) : $no_images_text;
  321. break;
  322. case 1:
  323. $count_text = $singular_text === false ? __( '1 image', 'foogallery' ) : $singular_text;
  324. break;
  325. default:
  326. $count_text = sprintf( $plural_text === false ? __( '%s images', 'foogallery' ) : $plural_text, $count );
  327. }
  328. return apply_filters( 'foogallery_image_count', $count_text, $this );
  329. }
  330. /**
  331. * Returns a safe name for the gallery, in case there has been no title set
  332. *
  333. * @return string
  334. */
  335. public function safe_name() {
  336. return empty( $this->name ) ?
  337. sprintf( __( '%s #%s', 'foogallery' ), foogallery_plugin_name(), $this->ID ) :
  338. $this->name;
  339. }
  340. public function find_usages() {
  341. return get_posts( array(
  342. 'post_type' => array( 'post', 'page', ),
  343. 'post_status' => array( 'draft', 'publish', ),
  344. 'posts_per_page' => -1,
  345. 'orderby' => 'post_type',
  346. 'meta_query' => array(
  347. array(
  348. 'key' => FOOGALLERY_META_POST_USAGE,
  349. 'value' => $this->ID,
  350. 'compare' => 'IN',
  351. ),
  352. ),
  353. ) );
  354. }
  355. public function gallery_template_details() {
  356. if ( ! empty( $this->gallery_template ) ) {
  357. foreach ( foogallery_gallery_templates() as $template ) {
  358. if ( $this->gallery_template == $template['slug'] ) {
  359. return $template;
  360. }
  361. }
  362. }
  363. return false;
  364. }
  365. /**
  366. * Returns the name of the gallery template
  367. * @return string|void
  368. */
  369. public function gallery_template_name() {
  370. $template = $this->gallery_template_details();
  371. if ( false !== $template ) {
  372. return $template['name'];
  373. }
  374. return __( 'Unknown', 'foogallery' );
  375. }
  376. public function gallery_template_has_field_of_type( $field_type ) {
  377. $gallery_template_details = $this->gallery_template_details();
  378. if ( false != $gallery_template_details ) {
  379. if ( array_key_exists( 'fields', $gallery_template_details ) ) {
  380. foreach ( $gallery_template_details['fields'] as $field ) {
  381. if ( $field_type == $field['type'] ) {
  382. return true;
  383. }
  384. }
  385. }
  386. }
  387. return false;
  388. }
  389. /**
  390. * Loads default settings from another gallery if it is set on the settings page
  391. */
  392. public function load_default_settings_if_new() {
  393. if ( $this->is_new() ) {
  394. $default_gallery_id = foogallery_get_setting( 'default_gallery_settings' );
  395. //loads all meta data from the default gallery
  396. $this->load_meta( $default_gallery_id );
  397. }
  398. }
  399. /**
  400. * Returns the current gallery datasource object
  401. *
  402. * @returns IFooGalleryDatasource
  403. */
  404. public function datasource() {
  405. //lazy load the datasource only when needed
  406. if ( $this->_datasource === false ) {
  407. $this->_datasource = foogallery_instantiate_datasource( $this->datasource_name );
  408. $this->_datasource->setGallery( $this );
  409. }
  410. return $this->_datasource;
  411. }
  412. }