PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/projects-by-woothemes/projects-core-functions.php

https://gitlab.com/mostafame/team_website
PHP | 438 lines | 207 code | 83 blank | 148 comment | 54 complexity | 4ceda026cc1abb316caf1638be1cf48c MD5 | raw file
  1. <?php
  2. /**
  3. * WooThemes Projects Core Functions
  4. *
  5. * Functions available on both the front-end and admin.
  6. *
  7. * @author WooThemes
  8. * @category Core
  9. * @package Projects/Functions
  10. * @version 1.0.0
  11. */
  12. if ( ! function_exists( 'projects_get_page_id' ) ) {
  13. /**
  14. * WooThemes Projects page IDs
  15. *
  16. * retrieve page ids - used for projects
  17. *
  18. * returns -1 if no page is found
  19. *
  20. * @access public
  21. * @param string $page
  22. * @return int
  23. */
  24. function projects_get_page_id ( $page ) {
  25. $options = get_option( 'projects-pages-fields' );
  26. $page = apply_filters( 'projects_get_' . $page . '_page_id', $options[ $page . '_page_id' ] );
  27. return $page ? $page : -1;
  28. } // End projects_get_page_id()
  29. }
  30. if ( ! function_exists( 'projects_get_image' ) ) {
  31. /**
  32. * Get the image for the given ID.
  33. * @param int $id Post ID.
  34. * @param string/array/int $size Image dimension. (default: "projects-thumbnail")
  35. * @since 1.0.0
  36. * @return string <img> tag.
  37. */
  38. function projects_get_image ( $id, $size = 'projects-thumbnail' ) {
  39. $response = '';
  40. if ( has_post_thumbnail( $id ) ) {
  41. // If not a string or an array, and not an integer, default to 150x9999.
  42. if ( is_int( $size ) || ( 0 < intval( $size ) ) ) {
  43. $size = array( intval( $size ), intval( $size ) );
  44. } elseif ( ! is_string( $size ) && ! is_array( $size ) ) {
  45. $size = array( 150, 9999 );
  46. }
  47. $response = get_the_post_thumbnail( intval( $id ), $size );
  48. }
  49. return $response;
  50. } // End projects_get_image()
  51. }
  52. /**
  53. * is_projects - Returns true if on a page which uses WooThemes Projects templates
  54. *
  55. * @access public
  56. * @return bool
  57. */
  58. function is_projects () {
  59. return ( is_projects_archive() || is_project_category() || is_project() ) ? true : false;
  60. } // End is_projects()
  61. if ( ! function_exists( 'is_projects_archive' ) ) {
  62. /**
  63. * is_projects_archive - Returns true when viewing the project type archive.
  64. *
  65. * @access public
  66. * @return bool
  67. */
  68. function is_projects_archive() {
  69. return ( is_post_type_archive( 'project' ) || is_project_taxonomy() || is_page( projects_get_page_id( 'projects' ) ) ) ? true : false;
  70. } // End is_projects_archive()
  71. }
  72. if ( ! function_exists( 'is_project_taxonomy' ) ) {
  73. /**
  74. * is_project_taxonomy - Returns true when viewing a project taxonomy archive.
  75. *
  76. * @access public
  77. * @return bool
  78. */
  79. function is_project_taxonomy() {
  80. return is_tax( get_object_taxonomies( 'project' ) );
  81. } // End is_project_taxonomy()
  82. }
  83. if ( ! function_exists( 'is_project_category' ) ) {
  84. /**
  85. * is_project_category - Returns true when viewing a project category.
  86. *
  87. * @access public
  88. * @param string $term (default: '') The term slug your checking for. Leave blank to return true on any.
  89. * @return bool
  90. */
  91. function is_project_category( $term = '' ) {
  92. return is_tax( 'project-category', $term );
  93. } // End is_project_category()
  94. }
  95. if ( ! function_exists( 'is_project' ) ) {
  96. /**
  97. * is_project - Returns true when viewing a single project.
  98. *
  99. * @access public
  100. * @return bool
  101. */
  102. function is_project() {
  103. return is_singular( array( 'project' ) );
  104. } // End is_project()
  105. }
  106. if ( ! function_exists( 'is_ajax' ) ) {
  107. /**
  108. * is_ajax - Returns true when the page is loaded via ajax.
  109. *
  110. * @access public
  111. * @return bool
  112. */
  113. function is_ajax() {
  114. if ( defined('DOING_AJAX') )
  115. return true;
  116. return ( isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && strtolower( $_SERVER['HTTP_X_REQUESTED_WITH'] ) == 'xmlhttprequest' ) ? true : false;
  117. }
  118. }
  119. /**
  120. * Get template part (for templates like the projects-loop).
  121. *
  122. * @access public
  123. * @param mixed $slug
  124. * @param string $name (default: '')
  125. * @return void
  126. */
  127. function projects_get_template_part( $slug, $name = '' ) {
  128. global $projects;
  129. $template = '';
  130. // Look in yourtheme/slug-name.php and yourtheme/projects/slug-name.php
  131. if ( $name )
  132. $template = locate_template( array ( "{$slug}-{$name}.php", "{$projects->template_url}{$slug}-{$name}.php" ) );
  133. // Get default slug-name.php
  134. if ( !$template && $name && file_exists( $projects->plugin_path() . "/templates/{$slug}-{$name}.php" ) )
  135. $template = $projects->plugin_path() . "/templates/{$slug}-{$name}.php";
  136. // If template file doesn't exist, look in yourtheme/slug.php and yourtheme/projects/slug.php
  137. if ( !$template )
  138. $template = locate_template( array ( "{$slug}.php", "{$projects->template_url}{$slug}.php" ) );
  139. if ( $template )
  140. load_template( $template, false );
  141. } // End projects_get_template_part()
  142. /**
  143. * Get other templates and including the file.
  144. *
  145. * @access public
  146. * @param mixed $template_name
  147. * @param array $args (default: array())
  148. * @param string $template_path (default: '')
  149. * @param string $default_path (default: '')
  150. * @return void
  151. */
  152. function projects_get_template ( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
  153. global $projects;
  154. if ( $args && is_array($args) )
  155. extract( $args );
  156. $located = projects_locate_template( $template_name, $template_path, $default_path );
  157. do_action( 'projects_before_template_part', $template_name, $template_path, $located, $args );
  158. include( $located );
  159. do_action( 'projects_after_template_part', $template_name, $template_path, $located, $args );
  160. } // End projects_get_template()
  161. /**
  162. * Locate a template and return the path for inclusion.
  163. *
  164. * This is the load order:
  165. *
  166. * yourtheme / $template_path / $template_name
  167. * yourtheme / $template_name
  168. * $default_path / $template_name
  169. *
  170. * @access public
  171. * @param mixed $template_name
  172. * @param string $template_path (default: '')
  173. * @param string $default_path (default: '')
  174. * @return string
  175. */
  176. function projects_locate_template( $template_name, $template_path = '', $default_path = '' ) {
  177. global $projects;
  178. if ( ! $template_path ) $template_path = $projects->template_url;
  179. if ( ! $default_path ) $default_path = $projects->plugin_path() . '/templates/';
  180. // Look within passed path within the theme - this is priority
  181. $template = locate_template(
  182. array(
  183. trailingslashit( $template_path ) . $template_name,
  184. $template_name
  185. )
  186. );
  187. // Get default template
  188. if ( ! $template )
  189. $template = $default_path . $template_name;
  190. // Return what we found
  191. return apply_filters( 'projects_locate_template', $template, $template_name, $template_path );
  192. } // End projects_locate_template()
  193. /**
  194. * Filter to allow project-category in the permalinks for projects.
  195. *
  196. * @access public
  197. * @param string $permalink The existing permalink URL.
  198. * @param object $post
  199. * @return string
  200. */
  201. function projects_project_post_type_link( $permalink, $post ) {
  202. // Abort if post is not a project
  203. if ( $post->post_type !== 'project' )
  204. return $permalink;
  205. // Abort early if the placeholder rewrite tag isn't in the generated URL
  206. if ( false === strpos( $permalink, '%' ) )
  207. return $permalink;
  208. // Get the custom taxonomy terms in use by this post
  209. $terms = get_the_terms( $post->ID, 'project-category' );
  210. if ( empty( $terms ) ) {
  211. // If no terms are assigned to this post, use a string instead (can't leave the placeholder there)
  212. $project_cat = _x( 'uncategorized', 'slug', 'projects-by-woothemes' );
  213. } else {
  214. // Replace the placeholder rewrite tag with the first term's slug
  215. $first_term = array_shift( $terms );
  216. $project_cat = $first_term->slug;
  217. }
  218. $find = array(
  219. '%year%',
  220. '%monthnum%',
  221. '%day%',
  222. '%hour%',
  223. '%minute%',
  224. '%second%',
  225. '%post_id%',
  226. '%category%',
  227. '%project_category%'
  228. );
  229. $replace = array(
  230. date_i18n( 'Y', strtotime( $post->post_date ) ),
  231. date_i18n( 'm', strtotime( $post->post_date ) ),
  232. date_i18n( 'd', strtotime( $post->post_date ) ),
  233. date_i18n( 'H', strtotime( $post->post_date ) ),
  234. date_i18n( 'i', strtotime( $post->post_date ) ),
  235. date_i18n( 's', strtotime( $post->post_date ) ),
  236. $post->ID,
  237. $project_cat,
  238. $project_cat
  239. );
  240. $replace = array_map( 'sanitize_title', $replace );
  241. $permalink = str_replace( $find, $replace, $permalink );
  242. return $permalink;
  243. } // End projects_project_post_type_link()
  244. add_filter( 'post_type_link', 'projects_project_post_type_link', 10, 2 );
  245. /**
  246. * projects_get_gallery_attachment_ids function.
  247. *
  248. * @access public
  249. * @return array
  250. */
  251. function projects_get_gallery_attachment_ids ( $post_id = 0 ) {
  252. global $post;
  253. if ( 0 == $post_id ) $post_id = get_the_ID();
  254. $project_image_gallery = get_post_meta( $post_id, '_project_image_gallery', true );
  255. if ( '' == $project_image_gallery ) {
  256. // Backwards compat
  257. $attachment_ids = get_posts( 'post_parent=' . intval( $post_id ) . '&numberposts=-1&post_type=attachment&orderby=menu_order&order=ASC&post_mime_type=image&fields=ids' );
  258. $attachment_ids = array_diff( $attachment_ids, array( get_post_thumbnail_id() ) );
  259. $project_image_gallery = implode( ',', $attachment_ids );
  260. }
  261. return array_filter( (array) explode( ',', $project_image_gallery ) );
  262. } // End projects_get_gallery_attachment_ids()
  263. /**
  264. * Add body classes for Projects pages
  265. *
  266. * @param array $classes
  267. * @return array
  268. */
  269. function woo_projects_body_class( $classes ) {
  270. $classes = (array) $classes;
  271. if ( is_projects() ) {
  272. $classes[] = 'projects';
  273. $classes[] = 'projects-page';
  274. }
  275. if ( is_project() ) {
  276. $attachments = count( projects_get_gallery_attachment_ids() );
  277. if ( $attachments > 0 ) {
  278. $classes[] = 'has-gallery';
  279. } else {
  280. $classes[] = 'no-gallery';
  281. }
  282. if ( !has_post_thumbnail() ) {
  283. $classes[] = 'no-cover-image';
  284. }
  285. }
  286. return array_unique( $classes );
  287. }
  288. /**
  289. * Find a category image.
  290. * @since 1.0.0
  291. * @return string
  292. */
  293. function projects_category_image ( $cat_id = 0 ) {
  294. global $post;
  295. if ( 0 == $cat_id ) return;
  296. $image = '';
  297. if ( false === ( $image = get_transient( 'projects_category_image_' . $cat_id ) ) ) {
  298. $cat = get_term( $cat_id, 'project-category' );
  299. $query_args = array(
  300. 'post_type' => 'project',
  301. 'posts_per_page' => -1,
  302. 'no_found_rows' => 1,
  303. 'tax_query' => array(
  304. array(
  305. 'taxonomy' => 'project-category',
  306. 'field' => 'id',
  307. 'terms' => array( $cat->term_id )
  308. )
  309. )
  310. );
  311. $query = new WP_Query( $query_args );
  312. while ( $query->have_posts() && $image == '' ) : $query->the_post();
  313. $image = projects_get_image( get_the_ID() );
  314. if ( $image ) {
  315. $image = '<a href="' . get_term_link( $cat ) . '" title="' . $cat->name . '">' . $image . '</a>';
  316. set_transient( 'projects_category_image_' . $cat->term_id, $image, 60 * 60 ); // 1 Hour.
  317. }
  318. endwhile;
  319. wp_reset_postdata();
  320. } // get transient
  321. return $image;
  322. } // End projects_category_image()
  323. /**
  324. * When a post is saved, flush the transient used to store the category images.
  325. * @since 1.0.0
  326. * @return void
  327. */
  328. function projects_category_image_flush_transient ( $post_id ) {
  329. if ( get_post_type( $post_id ) != 'project' ) return; // we only want projects
  330. $categories = get_the_terms( $post_id, 'project-category' );
  331. if ( $categories ) {
  332. foreach ($categories as $category ) {
  333. delete_transient( 'projects_category_image_' . $category->term_id );
  334. }
  335. }
  336. } // End projects_category_image_flush_transient()
  337. add_action( 'save_post', 'projects_category_image_flush_transient' );
  338. /**
  339. * Enqueue styles
  340. */
  341. function projects_script() {
  342. // Register projects CSS
  343. wp_register_style( 'projects-styles', plugins_url( '/assets/css/woo-projects.css', __FILE__ ), array(), PROJECTS_VERSION );
  344. wp_register_style( 'projects-handheld', plugins_url( '/assets/css/woo-projects-handheld.css', __FILE__ ), array(), PROJECTS_VERSION );
  345. if ( apply_filters( 'projects_enqueue_styles', true ) ) {
  346. // Load Main styles
  347. wp_enqueue_style( 'projects-styles' );
  348. // Load styles applied to handheld devices
  349. wp_enqueue_style( 'projects-handheld' );
  350. // Load Dashicons
  351. if ( is_project() ) {
  352. wp_enqueue_style( 'dashicons' );
  353. }
  354. }
  355. }