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

/wp-content/themes/nonus/framework/types/ctPortfolioTypeBase.class.php

https://github.com/alniko009/magic
PHP | 257 lines | 176 code | 33 blank | 48 comment | 14 complexity | 8bed4808475dea921b0536ecc2b225b0 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. require_once 'ctTypeBase.class.php';
  3. /**
  4. * Portfolio type handler
  5. * @author alex
  6. */
  7. class ctPortfolioTypeBase extends ctTypeBase {
  8. /**
  9. * Slug option name
  10. */
  11. const OPTION_SLUG = 'portfolio_index_slug';
  12. /**
  13. * Initializes events
  14. * @return mixed|void
  15. */
  16. public function init() {
  17. add_action('template_redirect', array($this, 'portfolioContextFixer'));
  18. $this->registerType();
  19. $this->registerTaxonomies();
  20. add_action("admin_init", array($this, "addMetaBox"));
  21. /** @var $NHP_Options NHP_Options */
  22. global $NHP_Options;
  23. //add options listener for license
  24. add_action('nhp-opts-options-validate-' . $NHP_Options->args['opt_name'], array($this, 'handleSlugOptionSaved'));
  25. }
  26. /**
  27. * Adds meta box
  28. */
  29. public function addMetaBox() {
  30. add_meta_box("portfolio-meta", __("Portfolio settings", 'ct_theme'), array($this, "portfolioMeta"), "portfolio", "normal", "high");
  31. add_action('save_post', array($this, 'saveDetails'));
  32. }
  33. /**
  34. * Fixes proper menu state
  35. */
  36. public function portfolioContextFixer() {
  37. if (get_query_var('post_type') == 'portfolio') {
  38. global $wp_query;
  39. $wp_query->is_home = false;
  40. }
  41. if (get_query_var('taxonomy') == 'portfolio_category') {
  42. global $wp_query;
  43. $wp_query->is_404 = true;
  44. $wp_query->is_tax = false;
  45. $wp_query->is_archive = false;
  46. }
  47. }
  48. /**
  49. * Register type
  50. */
  51. protected function registerType() {
  52. $typeData = $this->callFilter('pre_register_type', array(
  53. 'labels' => array(
  54. 'name' => _x('Portfolio items', 'post type general name', 'ct_theme'),
  55. 'singular_name' => _x('Portfolio Item', 'post type singular name', 'ct_theme'),
  56. 'add_new' => _x('Add New', 'portfolio', 'ct_theme'),
  57. 'add_new_item' => __('Add New Portfolio Item', 'ct_theme'),
  58. 'edit_item' => __('Edit Portfolio Item', 'ct_theme'),
  59. 'new_item' => __('New Portfolio Item', 'ct_theme'),
  60. 'view_item' => __('View Portfolio Item', 'ct_theme'),
  61. 'search_items' => __('Search Portfolio Items', 'ct_theme'),
  62. 'not_found' => __('No portfolio item found', 'ct_theme'),
  63. 'not_found_in_trash' => __('No portfolio items found in Trash', 'ct_theme'),
  64. 'parent_item_colon' => '',
  65. 'menu_name' => __('Portfolio items', 'ct_theme'),
  66. ),
  67. 'singular_label' => __('portfolio', 'ct_theme'),
  68. 'public' => true,
  69. 'publicly_queryable' => true,
  70. 'exclude_from_search' => false,
  71. 'show_ui' => true,
  72. 'show_in_menu' => true,
  73. //'menu_position' => 20,
  74. 'capability_type' => 'post',
  75. 'hierarchical' => false,
  76. 'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'comments', 'page-attributes'),
  77. 'has_archive' => false,
  78. 'rewrite' => array('slug' => $this->getPermalinkSlug(), 'with_front' => true, 'pages' => true, 'feeds' => false),
  79. 'query_var' => false,
  80. 'can_export' => true,
  81. 'show_in_nav_menus' => true,
  82. 'taxonomies' => array('post_tag')
  83. ));
  84. register_post_type('portfolio', $typeData);
  85. $this->callHook('post_register_type');
  86. }
  87. /**
  88. * Returns permalink slug
  89. * @return string
  90. */
  91. protected function getPermalinkSlug() {
  92. // Rewriting Permalink Slug
  93. $permalink_slug = ct_get_option('portfolio_index_slug', 'portfolio');
  94. if (empty($permalink_slug)) {
  95. $permalink_slug = 'portfolio';
  96. }
  97. return $permalink_slug;
  98. }
  99. /**
  100. * Gets hook name
  101. * @return string
  102. */
  103. protected function getHookBaseName() {
  104. return 'ct_portfolio';
  105. }
  106. /**
  107. * Creates taxonomies
  108. */
  109. protected function registerTaxonomies() {
  110. $data = $this->callFilter('pre_register_taxonomies', array(
  111. 'hierarchical' => true,
  112. 'labels' => array(
  113. 'name' => _x('Portfolio Categories', 'taxonomy general name', 'ct_theme'),
  114. 'singular_name' => _x('Portfolio Category', 'taxonomy singular name', 'ct_theme'),
  115. 'search_items' => __('Search Categories', 'ct_theme'),
  116. 'popular_items' => __('Popular Categories', 'ct_theme'),
  117. 'all_items' => __('All Categories', 'ct_theme'),
  118. 'parent_item' => null,
  119. 'parent_item_colon' => null,
  120. 'edit_item' => __('Edit Portfolio Category', 'ct_theme'),
  121. 'update_item' => __('Update Portfolio Category', 'ct_theme'),
  122. 'add_new_item' => __('Add New Portfolio Category', 'ct_theme'),
  123. 'new_item_name' => __('New Portfolio Category Name', 'ct_theme'),
  124. 'separate_items_with_commas' => __('Separate Portfolio category with commas', 'ct_theme'),
  125. 'add_or_remove_items' => __('Add or remove portfolio category', 'ct_theme'),
  126. 'choose_from_most_used' => __('Choose from the most used portfolio category', 'ct_theme'),
  127. 'menu_name' => __('Categories', 'ct_theme'),
  128. ),
  129. 'public' => false,
  130. 'show_in_nav_menus' => false,
  131. 'show_ui' => true,
  132. 'show_tagcloud' => false,
  133. 'query_var' => 'portfolio_category',
  134. 'rewrite' => false,
  135. ));
  136. register_taxonomy('portfolio_category', 'portfolio', $data);
  137. $this->callHook('post_register_taxonomies');
  138. }
  139. /**
  140. * Draw s portfolio meta
  141. */
  142. public function portfolioMeta() {
  143. global $post;
  144. $custom = get_post_custom($post->ID);
  145. $client = isset($custom["client"][0]) ? $custom["client"][0] : "";
  146. $external_url = isset($custom["external_url"][0]) ? $custom["external_url"][0] : "";
  147. $video = isset($custom["video"][0]) ? $custom["video"][0] : "";
  148. $displayMethod = isset($custom['display_method'][0]) ? $custom['display_method'][0] : 'image';
  149. $revolution_slider = isset($custom['revolution_slider'][0]) ? $custom['revolution_slider'][0] : '';
  150. if($supportsRevolutionSlider = function_exists('rev_slider_shortcode')){
  151. global $wpdb;
  152. $slides =$wpdb->get_results("SELECT * FROM ".GlobalsRevSlider::$table_sliders);
  153. }
  154. ?>
  155. <p>
  156. <label for="client"><?php _e('Client', 'ct_theme')?>: </label>
  157. <input id="client" class="regular-text" name="client" value="<?php echo $client; ?>"/>
  158. <p class="howto"><?php _e("Information about client", 'ct_theme')?></p>
  159. <p>
  160. <label for="url">Url: </label>
  161. <input id="url" class="regular-text" name="external_url" value="<?php echo $external_url; ?>"/>
  162. </p>
  163. <p class="howto"><?php _e("Link to external site. Leave empty to hide button", 'ct_theme')?></p>
  164. <p>
  165. <label for="display_method"><?php _e('Show portfolio item as', 'ct_theme')?>: </label>
  166. <select class="ct-toggler" id="display_method" name="display_method">
  167. <option data-group=".display" value="image" <?php echo selected('image', $displayMethod)?>><?php _e("Featured image",'ct_theme')?></option>
  168. <option data-group=".display" data-toggle=".ct-toggable.gallery" value="gallery" <?php echo selected('gallery', $displayMethod)?>><?php _e("Gallery",'ct_theme')?></option>
  169. <option data-group=".display" data-toggle=".ct-toggable.video" value="video" <?php echo selected('video', $displayMethod)?>><?php _e("Video",'ct_theme')?></option>
  170. <?php if ($supportsRevolutionSlider): ?>
  171. <option data-group=".display" data-toggle=".ct-toggable.revolution-slider" value="revolution-slider" <?php echo selected('revolution-slider', $displayMethod)?>><?php _e("Revolution slider gallery",'ct_theme')?></option>
  172. <?php endif;?>
  173. </select>
  174. </p>
  175. <p class="ct-toggable video display">
  176. <label for="video"><?php _e('Video url', 'ct_theme')?>: </label>
  177. <input id="video" class="regular-text" name="video" value="<?php echo $video; ?>"/>
  178. </p>
  179. <?php if ($supportsRevolutionSlider): ?>
  180. <p class="ct-toggable revolution-slider display">
  181. <label for="revolutionSlider"><?php _e('Revolution slider', 'ct_theme')?>: </label>
  182. <select id="revolutionSlider" name="revolution_slider">
  183. <?php foreach ($slides as $slide): ?>
  184. <option <?php echo selected($slide->alias, $revolution_slider)?> value="<?php echo $slide->alias?>"><?php echo $slide->title?></option>
  185. <?php endforeach;?>
  186. </select>
  187. </p>
  188. <?php endif; ?>
  189. <?php
  190. }
  191. public function saveDetails() {
  192. global $post;
  193. $fields = array('external_url', 'client', 'video', 'display_method', 'revolution_slider');
  194. foreach ($fields as $field) {
  195. if (isset($_POST[$field])) {
  196. update_post_meta($post->ID, $field, $_POST[$field]);
  197. }
  198. }
  199. }
  200. /**
  201. * Gets display method for portfolio
  202. * @param array $meta - post meta
  203. * @return null|string
  204. */
  205. public static function getMethodFromMeta($meta) {
  206. $method = isset($meta['display_method']) ? $meta['display_method'][0] : null;
  207. if (!$method) {
  208. $method = isset($meta['video'][0]) && trim($meta['video'][0]) ? 'video' : 'image';
  209. }
  210. return $method;
  211. }
  212. /**
  213. * Handles rebuild
  214. */
  215. public function handleSlugOptionSaved($newValues) {
  216. $currentSlug = $this->getPermalinkSlug();
  217. //rebuild rewrite if new slug
  218. if (isset($newValues[self::OPTION_SLUG]) && ($currentSlug != $newValues[self::OPTION_SLUG])) {
  219. $this->callHook('pre_slug_option_saved', array('current_slug' => $currentSlug, 'new_slug' => $newValues[self::OPTION_SLUG]));
  220. //clean rewrite to refresh it
  221. delete_option('rewrite_rules');
  222. }
  223. }
  224. }