PageRenderTime 24ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/classes/initialize.php

http://recipe-press.googlecode.com/
PHP | 850 lines | 595 code | 108 blank | 147 comment | 92 complexity | 482df46a360abd0400bd20091c7cce63 MD5 | raw file
  1. <?php
  2. if ( preg_match('#' . basename(__FILE__) . '#', $_SERVER['PHP_SELF']) ) {
  3. die('You are not allowed to call this page directly.');
  4. }
  5. /**
  6. * initialize.php - Initialize the post types and taxonomies
  7. *
  8. * @package RecipePress
  9. * @subpackage includes
  10. * @author GrandSlambert
  11. * @copyright 2009-2011
  12. * @access public
  13. * @since 1.0
  14. */
  15. class recipePress_Init extends recipePressCore {
  16. static $instance;
  17. /**
  18. * Initialize the plugin
  19. */
  20. function recipePress_Init() {
  21. global $wpdb;
  22. parent::recipePressCore();
  23. $inglist = $wpdb->get_results('select * from `' . $wpdb->prefix . 'postmeta` where `meta_key` = "_recipe_ingredient_list" order by `meta_value`');
  24. if ( count($inglist) >= 1 ) {
  25. add_action('admin_menu', array(&$this, 'add_upgrade_menu_option'));
  26. add_action('admin_notices', array(&$this, 'upgrade_warning'));
  27. } else {
  28. if ( $this->options['use-taxonomies'] ) {
  29. add_action('init', array($this, 'setup_taxonomies'));
  30. }
  31. add_action('init', array(&$this, 'setup_sizes'));
  32. add_action('init', array(&$this, 'setup_serving_sizes'));
  33. add_action('init', array(&$this, 'setup_ingredients'));
  34. add_action('init', array(&$this, 'create_post_type'));
  35. add_action('init', array(&$this, 'setup_my_box'));
  36. /* WordPress Filters */
  37. add_filter('index_template', array($this, 'index_template'), 10, 1);
  38. add_filter('home_template', array($this, 'index_template'), 10, 1);
  39. add_filter('archive_template', array($this, 'archive_template'), 10, 1);
  40. /* Use built in categories and tags. */
  41. if ( $this->options['use-post-categories'] ) {
  42. register_taxonomy_for_object_type('post_categories', 'recipe');
  43. }
  44. if ( $this->options['use-post-tags'] ) {
  45. register_taxonomy_for_object_type('post_tag', 'recipe');
  46. }
  47. }
  48. }
  49. /**
  50. * Initialize the shortcodes.
  51. */
  52. static function initialize() {
  53. $instance = self::get_instance();
  54. }
  55. /**
  56. * Returns singleton instance of object
  57. *
  58. * @return instance
  59. */
  60. static function get_instance() {
  61. if ( is_null(self::$instance) ) {
  62. self::$instance = new recipePress_Init;
  63. }
  64. return self::$instance;
  65. }
  66. /**
  67. * Puts a warning on the admin side that an update is needed.
  68. */
  69. function upgrade_warning() {
  70. echo "<div id='recipe-press-warning' class='updated fade'><p><strong>" . __('RecipePress requires an update.') . "</strong> " . sprintf(__('You must <a href="%1$s">run the update tool</a> for it to work.'), admin_url() . "plugins.php?page=recipe-press-update") . "</p></div>";
  71. }
  72. /**
  73. * Adds an option to the plugins menu to perform the update to version 2.0
  74. */
  75. function add_upgrade_menu_option() {
  76. add_submenu_page('plugins.php', __('RecipePress Upgrade'), __('RecipePress Upgrade'), 'manage_options', 'recipe-press-update', array(&$this, 'update_function'));
  77. }
  78. /**
  79. * Updates older version to 2.0
  80. *
  81. * @global object $wpdb
  82. */
  83. function update_function() {
  84. global $wpdb;
  85. ?>
  86. <div class="wrap">
  87. <div class="icon32" id="icon-recipe-press"><br/></div>
  88. <h2><?php echo $this->pluginName; ?> &raquo; <?php _e('Update to version 2.0', 'recipe-press'); ?> </h2>
  89. <ul>
  90. <?php
  91. /* Register the post type */
  92. $this->create_post_type();
  93. /* Create default ingredient sizes */
  94. echo '<li>' . __('Creating new taxonomy for ingredient sizes and populating data.', 'recipe-press') . '</li>';
  95. $this->setup_sizes();
  96. $terms = get_terms('recipe-size', array('fields' => 'names', 'hide_empty' => false));
  97. /* If no sizes registered, add default sizes */
  98. if ( count($terms) == 0 ) {
  99. $sizes = array_unique(array_merge(
  100. $this->options['standard']['ingredient-sizes'],
  101. $this->options['metric']['ingredient-sizes']));
  102. foreach ( $sizes as $size ) {
  103. if ( !in_array($size, $terms) ) {
  104. wp_insert_term($size, 'recipe-size');
  105. }
  106. }
  107. }
  108. /* Convert all recipes */
  109. $inglist = $wpdb->get_results('select * from `' . $wpdb->prefix . 'postmeta` where `meta_key` = "_recipe_ingredient_value" order by `post_id`');
  110. foreach ( $inglist as $ingredient ) {
  111. $oldDetails = $details = unserialize($ingredient->meta_value);
  112. $term = get_term_by('name', $details['size'], 'recipe-size');
  113. if ( is_object($term) ) {
  114. $details['size'] = $term->term_id;
  115. update_post_meta($ingredient->post_id, '_recipe_ingredient_value', $details, $oldDetails);
  116. }
  117. }
  118. /* Create default serving sizes. */
  119. echo '<li>' . __('Creating new taxonomy for serving sizes and populating data.', 'recipe-press') . '</li>';
  120. $this->setup_serving_sizes();
  121. $terms = get_terms('recipe-serving', array('fields' => 'names', 'hide_empty' => false));
  122. /* If no sizes registered, add default sizes */
  123. if ( count($terms) == 0 ) {
  124. $sizes = array_unique(array_merge(
  125. $this->options['standard']['serving-sizes'],
  126. $this->options['metric']['serving-sizes']));
  127. foreach ( $sizes as $size ) {
  128. if ( !in_array($size, $terms) ) {
  129. wp_insert_term($size, 'recipe-serving');
  130. }
  131. }
  132. }
  133. /* Convert all serving sizes */
  134. $inglist = $wpdb->get_results('select * from `' . $wpdb->prefix . 'postmeta` where `meta_key` = "_recipe_serving_size_value" order by `post_id`', ARRAY_A);
  135. foreach ( $inglist as $ingredient ) {
  136. $term = get_term_by('name', $ingredient['meta_value'], 'recipe-serving');
  137. if ( is_object($term) ) {
  138. $ingredient['meta_value'] = $term->term_id;
  139. update_post_meta($ingredient['post_id'], '_recipe_serving_size_value', $details, $oldDetails);
  140. }
  141. }
  142. /* Update ingredients */
  143. echo '<li>' . __('Creating new taxonomy for ingredients and populating data.', 'recipe-press') . '</li>';
  144. $this->setup_ingredients();
  145. $ingredients = get_terms('recipe-ingredient', array('fields' => 'names', 'hide_empty' => false));
  146. if ( count($ingredients) == 0 ) {
  147. /* Create the ingredients */
  148. $inglist = $wpdb->get_results('select * from `' . $wpdb->prefix . 'postmeta` where `meta_key` = "_recipe_ingredient_list" order by `meta_value`');
  149. foreach ( $inglist as $ingredient ) {
  150. $recipes[$ingredient->post_id]['ingredients'][] = $ingredient->meta_value;
  151. }
  152. foreach ( $recipes as $id => $recipe ) {
  153. wp_set_object_terms($id, $recipe['ingredients'], 'recipe-ingredient');
  154. delete_post_meta($id, '_recipe_ingredient_list');
  155. }
  156. }
  157. /* Convert the recipes */
  158. $inglist = $wpdb->get_results('select * from `' . $wpdb->prefix . 'postmeta` where `meta_key` = "_recipe_ingredient_value" order by `post_id`');
  159. foreach ( $inglist as $ingredient ) {
  160. $oldDetails = $details = unserialize($ingredient->meta_value);
  161. $term = get_term_by('name', $details['item'], 'recipe-ingredient');
  162. if ( is_object($term) ) {
  163. $details['item'] = $term->term_id;
  164. update_post_meta($ingredient->post_id, '_recipe_ingredient_value', $details, $oldDetails);
  165. delete_post_meta($ingredient->post_id, '_recipe_ingredient_list');
  166. }
  167. }
  168. /* Make sure that all of the old ingredient data is removed. */
  169. $insurance = $wpdb->get_results('delete from `' . $wpdb->prefix . 'postmeta` where `meta_key` = "_recipe_ingredient_list" order by `meta_value`');
  170. /* Add version to settings */
  171. remove_action('update_option_' . $this->optionsName, array(&$this, 'update_option'), 10, 2);
  172. $this->options['version'] = $this->version;
  173. update_option($this->optionsName, $this->options);
  174. ?>
  175. <li><?php _e('Done Updating.', 'recipe-press'); ?></li>
  176. </ul>
  177. </div>
  178. <?php
  179. }
  180. /**
  181. * Create the post type.
  182. *
  183. * @global object $wp_rewrite
  184. */
  185. function create_post_type() {
  186. global $wp_version;
  187. $page = get_page($this->options['display-page']);
  188. $labels = array(
  189. 'name' => $this->options['plural-name'],
  190. 'singular_name' => $this->options['singular-name'],
  191. 'add_new' => __('Add New', 'recipe-press'),
  192. 'add_new_item' => sprintf(__('Add New %1$s', 'recipe-press'), $this->options['singular-name']),
  193. 'edit_item' => sprintf(__('Edit %1$s', 'recipe-press'), $this->options['singular-name']),
  194. 'edit' => __('Edit', 'recipe-press'),
  195. 'new_item' => sprintf(__('New %1$s', 'recipe-press'), $this->options['singular-name']),
  196. 'view_item' => sprintf(__('View %1$s', 'recipe-press'), $this->options['singular-name']),
  197. 'search_items' => sprintf(__('Search %1$s', 'recipe-press'), $this->options['singular-name']),
  198. 'not_found' => sprintf(__('No %1$s found', 'recipe-press'), $this->options['plural-name']),
  199. 'not_found_in_trash' => sprintf(__('No %1$s found in Trash', 'recipe-press'), $this->options['plural-name']),
  200. 'view' => sprintf(__('View %1$s', 'recipe-press'), $this->options['singular-name']),
  201. 'parent_item' => sprintf(__('Parent %1$s', 'recipe-press'), $this->options['singular-name']),
  202. 'parent_item_colon' => sprintf(__('Parent %1$s:', 'recipe-press'), $this->options['singular-name']),
  203. );
  204. $args = array(
  205. 'labels' => $labels,
  206. 'public' => true,
  207. 'publicly_queryable' => true,
  208. 'show_ui' => true,
  209. 'query_var' => true,
  210. 'capability_type' => 'page',
  211. 'hierarchical' => true,
  212. 'menu_position' => (int) $this->options['menu-position'],
  213. 'menu_icon' => $this->options['menu-icon'],
  214. 'supports' => array('title', 'editor', 'author', 'excerpt', 'page-attributes'),
  215. 'register_meta_box_cb' => array(&$this, 'init_metaboxes'),
  216. );
  217. if ( $this->options['use-custom-fields'] ) {
  218. $args['supports'][] = 'custom-fields';
  219. }
  220. if ( $this->options['use-thumbnails'] ) {
  221. $args['supports'][] = 'thumbnail';
  222. }
  223. if ( $this->options['use-comments'] ) {
  224. $args['supports'][] = 'comments';
  225. }
  226. if ( $this->options['use-trackbacks'] ) {
  227. $args['supports'][] = 'trackbacks';
  228. }
  229. if ( $this->options['use-revisions'] ) {
  230. $args['supports'][] = 'revisions';
  231. }
  232. if ( $this->options['use-post-tags'] ) {
  233. $args['taxonomies'][] = 'post_tag';
  234. }
  235. if ( $this->options['use-post-categories'] ) {
  236. $args['taxonomies'][] = 'category';
  237. }
  238. if ( version_compare($wp_version, '3.1', '>=') and !$this->options['use-plugin-permalinks'] ) {
  239. $args['rewrite'] = true;
  240. $args['has_archive'] = $this->options['index-slug'];
  241. } else {
  242. $args['rewrite'] = false;
  243. $args['has_archive'] = false;
  244. /* Flush the rewrite rules */
  245. global $wp_rewrite;
  246. $this->recipe_rewrite_rules(array('identifier' => $this->options['identifier'], 'structure' => $this->options['permalink'], 'type' => 'recipe'));
  247. if ( isset($this->options['use-form']) and $this->options['use-form'] ) {
  248. $this->recipe_rewrite_rules(array('identifier' => $this->options['form-identifier'], 'structure' => $this->options['form-permalink'], 'type' => 'form'));
  249. }
  250. $wp_rewrite->flush_rules();
  251. /* Add filters to handle custom pages */
  252. add_filter('post_type_link', array($this, 'post_link'), 10, 3);
  253. }
  254. register_post_type('recipe', $args);
  255. }
  256. /**
  257. * Handle index pages for the recipe post type.
  258. *
  259. * @global object $post
  260. * @param string $template
  261. * @return string
  262. */
  263. public function index_template($template) {
  264. global $post, $wp_query, $taxonomy, $terms, $tax, $pagination, $recipeData, $current_user;
  265. /* Handle Recipe Box Page */
  266. if ( $recipeBox = get_query_var('recipe-box') ) {
  267. $page = get_query_var('box-page');
  268. $replacement_template = get_query_template('recipe-box');
  269. if ( file_exists($replacement_template) ) {
  270. add_filter('wp_title', array(&$this, 'recipe_box_title'));
  271. return $replacement_template;
  272. } else {
  273. if ( $this->options['recipe-box-page'] and get_page($this->options['recipe-box-page']) ) {
  274. wp_redirect(get_permalink($this->options['recipe-box-page']));
  275. exit();
  276. } else {
  277. wp_die(__('Warning: The site administrator has not set a page to load the recipe box on.', 'recipe-press'));
  278. }
  279. }
  280. }
  281. /* Handle Taxonomy Page */
  282. if ( $taxonomy = get_query_var('recipe-taxonomy') ) {
  283. $page = get_query_var('page');
  284. $replacement_template = get_query_template('taxonomy-recipe');
  285. if ( file_exists($replacement_template) ) {
  286. $atts = array(
  287. 'taxonomy' => $taxonomy,
  288. 'number' => 0,
  289. 'offset' => 0,
  290. 'orderby' => 'name',
  291. 'order' => 'asc',
  292. 'hide_empty' => true,
  293. 'fields' => 'all',
  294. 'slug' => false,
  295. 'hierarchical' => true,
  296. 'name__like' => '',
  297. 'pad_counts' => false,
  298. 'child_of' => NULL,
  299. 'parent' => 0,
  300. 'include' => get_published_categories($taxonomy)
  301. );
  302. $tax = get_taxonomy($taxonomy);
  303. /* Count all terms */
  304. $atts['fields'] = 'ids';
  305. $all_terms = get_terms($atts['taxonomy'], $atts);
  306. if ( $taxonomy == 'recipe-ingredient' ) {
  307. $pagination = array(
  308. 'total' => count($all_terms),
  309. 'pages' => ceil(count($all_terms) / $this->options['ingredients-per-page']),
  310. 'current-page' => max($page, 1),
  311. 'taxonomy' => __('Ingredients', 'recipe-press'),
  312. 'url' => get_option('home') . '/' . $this->options['ingredient-slug'],
  313. 'per-page' => $this->options['ingredients-per-page']
  314. );
  315. } else {
  316. $this->options['taxonomies'][$taxonomy] = $this->taxDefaults($this->options['taxonomies'][$taxonomy]);
  317. $pagination = array(
  318. 'total' => count($all_terms),
  319. 'pages' => ceil(count($all_terms) / $this->options['taxonomies'][$taxonomy]['per-page']),
  320. 'current-page' => max($page, 1),
  321. 'taxonomy' => $this->options['taxonomies'][$taxonomy]['plural'],
  322. 'url' => get_option('home') . '/' . $this->options['taxonomies'][$taxonomy]['slug'],
  323. 'per-page' => $this->options['taxonomies'][$taxonomy]['per-page']
  324. );
  325. }
  326. unset($atts['fields']);
  327. $atts['number'] = $pagination['per-page'];
  328. if ( $page > 1 ) {
  329. $atts['offset'] = $page * $atts['number'] - $atts['number'];
  330. } else {
  331. $atts['offset'] = 0;
  332. }
  333. $terms = get_terms($atts['taxonomy'], $atts);
  334. add_filter('wp_title', array(&$this, 'recipe_taxonomy_page_title'));
  335. return $replacement_template;
  336. } else {
  337. $taxonomy = get_query_var('recipe-taxonomy');
  338. if ( $taxonomy == 'recipe-ingredient' ) {
  339. $pageID = $this->options['ingredient-page'];
  340. } else {
  341. $pageID = $this->options['taxonomies'][$taxonomy]['page'];
  342. }
  343. if ( $pageID and get_page($pageID) ) {
  344. wp_redirect(get_permalink($pageID));
  345. } else {
  346. wp_redirect(get_option('home') . '/' . $this->options['index-slug']);
  347. }
  348. }
  349. }
  350. if ( is_object($post) and $post->post_type == 'recipe' and $replacement_template = get_query_template('index-recipe') ) {
  351. return $replacement_template;
  352. } else {
  353. return $template;
  354. }
  355. }
  356. /**
  357. * Handle archive pages for the recipe post type
  358. *
  359. * @global object $post
  360. * @param string $template
  361. * @return string
  362. */
  363. public function archive_template($template) {
  364. global $post;
  365. if ( is_object($post) and $post->post_type == 'recipe' and $replacement_template = get_query_template('archive-recipe') ) {
  366. return $replacement_template;
  367. } else {
  368. return $template;
  369. }
  370. }
  371. /**
  372. * Filter to correct the title on the recipe box pages when using template files.
  373. *
  374. * This function can be overriden by adding a function named recipe_box_title
  375. * in your themes function file. The function will receive the generated title as an
  376. * argument. You need to return the text to display in the title.
  377. *
  378. * @param string $title
  379. * @return string
  380. */
  381. function recipe_box_title($title) {
  382. if ( function_exists('recipe_box_title') ) {
  383. return recipe_box_title($title);
  384. } else {
  385. return $this->options['recipe-box-title'] . ' | ' . get_bloginfo('name');
  386. }
  387. }
  388. /**
  389. * Filter to correct the title on index pages when using template files.
  390. *
  391. * This function can be overriden by adding a function named recipe_taxonomy_page_title
  392. * in your themes function file. The function will receive the generated title as an
  393. * argument. You need to return the text to display in the title.
  394. *
  395. * @param string $title
  396. * @return string
  397. */
  398. function recipe_taxonomy_page_title($title) {
  399. if ( function_exists('recipe_taxonomy_page_title') ) {
  400. return recipe_taxonomy_page_title($title);
  401. } else {
  402. if ( get_query_var('recipe-taxonomy') == 'recipe-ingredient' ) {
  403. $title = __('Recipe Ingredients', 'recipe-press');
  404. } else {
  405. $title = $this->options['taxonomies'][get_query_var('recipe-taxonomy')]['plural'];
  406. }
  407. return $title . ' | ' . get_bloginfo('name');
  408. }
  409. }
  410. /**
  411. * Rewrite rules for custom recipe permalinks.
  412. *
  413. * @global object $wp_rewrite
  414. * @param array $permastructure (identifier, structure, type)
  415. */
  416. function recipe_rewrite_rules($permastructure) {
  417. global $wp_rewrite;
  418. $structure = $permastructure['structure'];
  419. $front = substr($structure, 0, strpos($structure, '%'));
  420. $type_query_var = 'recipe';
  421. $structure = str_replace('%identifier%', $permastructure['identifier'], $structure);
  422. $rewrite_rules = $wp_rewrite->generate_rewrite_rules($structure, EP_NONE, true, true, true, true, true);
  423. /* build a rewrite rule from just the identifier if it is the first token */
  424. preg_match('/%.+?%/', $permastructure['structure'], $tokens);
  425. if ( $tokens[0] == '%identifier%' ) {
  426. $rewrite_rules = array_merge($wp_rewrite->generate_rewrite_rules($front . $this->options['index-slug'] . '/'), $rewrite_rules);
  427. $rewrite_rules[$front . $this->options['index-slug'] . '/?$'] = 'index.php?paged=1';
  428. }
  429. foreach ( $rewrite_rules as $regex => $redirect ) {
  430. if ( strpos($redirect, 'attachment=') === false ) {
  431. /* don't set the post_type for attachments */
  432. $redirect .= '&post_type=recipe';
  433. }
  434. if ( 0 < preg_match_all('@\$([0-9])@', $redirect, $matches) ) {
  435. for ( $i = 0; $i < count($matches[0]); $i++ ) {
  436. $redirect = str_replace($matches[0][$i], '$matches[' . $matches[1][$i] . ']', $redirect);
  437. }
  438. }
  439. $redirect = str_replace('name=', $type_query_var . '=', $redirect);
  440. add_rewrite_rule($regex, $redirect, 'top');
  441. }
  442. }
  443. /**
  444. * Permalink handling for post_type
  445. *
  446. * @param string $permalink
  447. * @param object $post
  448. * @param bool $leavename
  449. * @return string
  450. */
  451. public function post_link($permalink, $id, $leavename = false) {
  452. if ( is_object($id) && isset($id->filter) && 'sample' == $id->filter ) {
  453. $post = $id;
  454. } else {
  455. $post = &get_post($id);
  456. }
  457. if ( empty($post->ID) || $post->post_type != 'recipe' )
  458. return $permalink;
  459. $rewritecode = array(
  460. '%identifier%',
  461. '%year%',
  462. '%monthnum%',
  463. '%day%',
  464. '%hour%',
  465. '%minute%',
  466. '%second%',
  467. $leavename ? '' : '%postname%',
  468. '%post_id%',
  469. '%category%',
  470. '%author%',
  471. $leavename ? '' : '%pagename%',
  472. );
  473. $permastructure = array('identifier' => $this->options['identifier'], 'structure' => $this->options['permalink']);
  474. $identifier = $permastructure['identifier'];
  475. $permalink = $permastructure['structure'];
  476. if ( '' != $permalink && get_option('permalink_structure') && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) {
  477. $unixtime = strtotime($post->post_date);
  478. $category = '';
  479. if ( strpos($permalink, '%category%') !== false ) {
  480. $cats = get_the_category($post->ID);
  481. if ( $cats ) {
  482. usort($cats, '_usort_terms_by_ID'); // order by ID
  483. $category = $cats[0]->slug;
  484. if ( $parent = $cats[0]->parent )
  485. $category = get_category_parents($parent, false, '/', true) . $category;
  486. }
  487. /* show default category in permalinks, without having to assign it explicitly */
  488. if ( empty($category) ) {
  489. $default_category = get_category(get_option('default_category'));
  490. $category = is_wp_error($default_category) ? '' : $default_category->slug;
  491. }
  492. }
  493. $author = '';
  494. if ( strpos($permalink, '%author%') !== false ) {
  495. $authordata = get_userdata($post->post_author);
  496. $author = $authordata->user_nicename;
  497. }
  498. $date = explode(" ", date('Y m d H i s', $unixtime));
  499. $rewritereplace =
  500. array(
  501. $identifier,
  502. $date[0],
  503. $date[1],
  504. $date[2],
  505. $date[3],
  506. $date[4],
  507. $date[5],
  508. $post->post_name,
  509. $post->ID,
  510. $category,
  511. $author,
  512. $post->post_name,
  513. );
  514. $permalink = home_url(str_replace($rewritecode, $rewritereplace, $permalink));
  515. $permalink = user_trailingslashit($permalink, 'single');
  516. } else {
  517. $permalink = home_url('?p=' . $post->ID . '&post_type=' . urlencode('recipe'));
  518. }
  519. return $permalink;
  520. }
  521. /**
  522. * Set up all taxonomies.
  523. */
  524. function setup_taxonomies() {
  525. foreach ( $this->options['taxonomies'] as $key => $taxonomy ) {
  526. if ( isset($taxonomy['active']) and isset($taxonomy['plural']) ) {
  527. if ( !isset($taxonomy['slug']) ) {
  528. $rewrite = false;
  529. } else {
  530. $rewrite = array('slug' => $taxonomy['slug'], 'with_front' => true);
  531. }
  532. $labels = array(
  533. 'name' => $taxonomy['plural'],
  534. 'singular_name' => $taxonomy['singular'],
  535. 'search_items' => sprintf(__('Search %1$s', 'recipe-press'), $taxonomy['plural']),
  536. 'popular_items' => sprintf(__('Popular %1$s', 'recipe-press'), $taxonomy['plural']),
  537. 'all_items' => sprintf(__('All %1$s', 'recipe-press'), $taxonomy['plural']),
  538. 'parent_item' => sprintf(__('Parent %1$s', 'recipe-press'), $taxonomy['singular']),
  539. 'edit_item' => sprintf(__('Edit %1$s', 'recipe-press'), $taxonomy['singular']),
  540. 'update_item' => sprintf(__('Update %1$s', 'recipe-press'), $taxonomy['singular']),
  541. 'add_new_item' => sprintf(__('Add %1$s', 'recipe-press'), $taxonomy['singular']),
  542. 'new_item_name' => sprintf(__('New %1$s', 'recipe-press'), $taxonomy['singular']),
  543. 'add_or_remove_items' => sprintf(__('Add or remove %1$s', 'recipe-press'), $taxonomy['plural']),
  544. 'choose_from_most_used' => sprintf(__('Choose from the most used %1$s', 'recipe-press'), $taxonomy['plural'])
  545. );
  546. $args = array(
  547. 'hierarchical' => isset($taxonomy['hierarchical']),
  548. 'label' => $taxonomy['plural'],
  549. 'labels' => $labels,
  550. 'public' => true,
  551. 'show_ui' => true,
  552. 'rewrite' => $rewrite,
  553. );
  554. register_taxonomy($key, array('recipe'), $args);
  555. $this->taxonomy_rewrite_rules($key, $taxonomy);
  556. }
  557. }
  558. }
  559. function taxonomy_rewrite_rules($taxonomy, $settings) {
  560. global $wp_rewrite;
  561. $type_query_var = $settings['slug'];
  562. //$structure = str_replace('%identifier%', $permastructure['identifier'], $structure);
  563. $rewrite_rules = $wp_rewrite->generate_rewrite_rules($settings['slug'], EP_NONE, true, true, true, true, true);
  564. $rewrite_rules[$settings['slug'] . '/?$'] = 'index.php?paged=1';
  565. foreach ( $rewrite_rules as $regex => $redirect ) {
  566. if ( strpos($redirect, 'attachment=') === false ) {
  567. /* don't set the post_type for attachments */
  568. $redirect .= '&post_type=recipe&recipe-taxonomy=' . $taxonomy;
  569. }
  570. if ( 0 < preg_match_all('@\$([0-9])@', $redirect, $matches) ) {
  571. for ( $i = 0; $i < count($matches[0]); $i++ ) {
  572. $redirect = str_replace($matches[0][$i], '$matches[' . $matches[1][$i] . ']', $redirect);
  573. }
  574. }
  575. $redirect = str_replace('name=', $type_query_var . '=', $redirect);
  576. add_rewrite_rule($regex, $redirect, 'top');
  577. }
  578. }
  579. /**
  580. * Setup sizes taxonomy.
  581. */
  582. function setup_sizes() {
  583. $labels = array(
  584. 'name' => __('Sizes', 'recipe-press'),
  585. 'singular_name' => __('Size', 'recipe-press'),
  586. 'search_items' => __('Search Sizes', 'recipe-press'),
  587. 'popular_items' => __('Popular Sizes', 'recipe-press'),
  588. 'all_items' => __('All Sizes', 'recipe-press'),
  589. 'parent_item' => __('Parent Size', 'recipe-press'),
  590. 'edit_item' => __('Edit Size', 'recipe-press'),
  591. 'update_item' => __('Update Size', 'recipe-press'),
  592. 'add_new_item' => __('Add Size', 'recipe-press'),
  593. 'new_item_name' => __('New Size', 'recipe-press'),
  594. 'add_or_remove_items' => __('Add or remove Sizes', 'recipe-press'),
  595. 'choose_from_most_used' => __('Choose from the most used Sizes', 'recipe-press'),
  596. );
  597. $args = array(
  598. 'hierarchical' => false,
  599. 'label' => __('Sizes', 'recipe-press'),
  600. 'labels' => $labels,
  601. 'public' => true,
  602. 'show_ui' => true,
  603. 'capabilities' => array(
  604. 'assign_terms' => false
  605. ),
  606. 'rewrite' => array('slug' => 'recipe-size'),
  607. );
  608. register_taxonomy('recipe-size', array('recipe'), $args);
  609. }
  610. /**
  611. * Setup sizes taxonomy.
  612. */
  613. function setup_serving_sizes() {
  614. $labels = array(
  615. 'name' => __('Serving Sizes', 'recipe-press'),
  616. 'singular_name' => __('Serving Size', 'recipe-press'),
  617. 'search_items' => __('Search Serving Sizes', 'recipe-press'),
  618. 'popular_items' => __('Popular Serving Sizes', 'recipe-press'),
  619. 'all_items' => __('All Serving Sizes', 'recipe-press'),
  620. 'parent_item' => __('Parent Serving Size', 'recipe-press'),
  621. 'edit_item' => __('Edit Serving Size', 'recipe-press'),
  622. 'update_item' => __('Update Serving Size', 'recipe-press'),
  623. 'add_new_item' => __('Add Serving Size', 'recipe-press'),
  624. 'new_item_name' => __('New Serving Size', 'recipe-press'),
  625. 'add_or_remove_items' => __('Add or remove Serving Sizes', 'recipe-press'),
  626. 'choose_from_most_used' => __('Choose from the most used Serving Sizes', 'recipe-press'),
  627. );
  628. $args = array(
  629. 'hierarchical' => false,
  630. 'label' => __('Serving Sizes', 'recipe-press'),
  631. 'labels' => $labels,
  632. 'public' => true,
  633. 'show_ui' => true,
  634. 'capabilities' => array(
  635. 'assign_terms' => false
  636. ),
  637. 'rewrite' => array('slug' => 'recipe-serving'),
  638. );
  639. register_taxonomy('recipe-serving', array('recipe'), $args);
  640. return true;
  641. }
  642. /**
  643. * Setup ingredients taxonomy.
  644. */
  645. function setup_ingredients() {
  646. $labels = array(
  647. 'name' => __('Ingredients', 'recipe-press'),
  648. 'singular_name' => __('Ingredient', 'recipe-press'),
  649. 'search_items' => __('Search Ingredients', 'recipe-press'),
  650. 'popular_items' => __('Popular Ingredients', 'recipe-press'),
  651. 'all_items' => __('All Ingredients', 'recipe-press'),
  652. 'parent_item' => __('Parent Ingredient', 'recipe-press'),
  653. 'edit_item' => __('Edit Ingredient', 'recipe-press'),
  654. 'update_item' => __('Update Ingredient', 'recipe-press'),
  655. 'add_new_item' => __('Add Ingredient', 'recipe-press'),
  656. 'new_item_name' => __('New Ingredient', 'recipe-press'),
  657. 'add_or_remove_items' => __('Add or remove Ingredients', 'recipe-press'),
  658. 'choose_from_most_used' => __('Choose from the most used Ingredients', 'recipe-press'),
  659. );
  660. $args = array(
  661. 'hierarchical' => false,
  662. 'label' => __('Ingredients', 'recipe-press'),
  663. 'labels' => $labels,
  664. 'public' => true,
  665. 'show_ui' => true,
  666. 'capabilities' => array(
  667. 'assign_terms' => false
  668. ),
  669. 'rewrite' => array('slug' => 'ingredient'),
  670. );
  671. register_taxonomy('recipe-ingredient', array('recipe'), $args);
  672. $this->taxonomy_rewrite_rules('recipe-ingredient', array('slug' => $this->options['ingredient-slug']));
  673. return true;
  674. }
  675. /**
  676. * Set up the My Recipe Box rewrite rules.
  677. *
  678. * @global object $wp_rewrite
  679. */
  680. function setup_my_box() {
  681. global $wp_rewrite;
  682. $wp_rewrite->flush_rules();
  683. $type_query_var = $this->options['recipe-box-slug'];
  684. $rewrite_rules = $wp_rewrite->generate_rewrite_rules($this->options['recipe-box-slug'], EP_NONE, true, false, false, true, true);
  685. $rewrite_rules[$this->options['recipe-box-slug'] . '/?$'] = 'index.php?paged=1';
  686. foreach ( $rewrite_rules as $regex => $redirect ) {
  687. if ( strpos($redirect, 'attachment=') === false ) {
  688. /* don't set the post_type for attachments */
  689. $redirect .= '&post_type=recipe&recipe-box=home';
  690. }
  691. if ( 0 < preg_match_all('@\$([0-9])@', $redirect, $matches) ) {
  692. for ( $i = 0; $i < count($matches[0]); $i++ ) {
  693. $redirect = str_replace($matches[0][$i], '$matches[' . $matches[1][$i] . ']', $redirect);
  694. }
  695. }
  696. $redirect = str_replace('name=', $type_query_var . '=', $redirect);
  697. add_rewrite_rule($regex, $redirect, 'top');
  698. }
  699. $wp_rewrite->flush_rules();
  700. }
  701. /**
  702. * Adds additional meta boxes to the recipe edit screen.
  703. */
  704. function init_metaboxes() {
  705. add_meta_box('recipes_ingredients', __('Ingredients', 'recipe-press'), array(&$this, 'ingredients_box'), 'recipe', 'advanced', 'high');
  706. add_meta_box('recipes_details', __('Details', 'recipe-press'), array(&$this, 'details_box'), 'recipe', 'side', 'high');
  707. /* Display Nutritional value */
  708. if ( $this->options['use-nutritional-value'] ) :
  709. add_meta_box('recipes_nutrients', __('Nutrients', 'recipe-press'), array(&$this, 'nutrients_box'), 'recipe', 'advanced', 'high');
  710. endif;
  711. }
  712. /**
  713. * Sets up the box for entering ingredients.
  714. */
  715. function ingredients_box() {
  716. /* Use nonce for verification */
  717. echo '<input type="hidden" name="ingredients_noncename" id="ingredients_noncename" value="' . wp_create_nonce('recipe_press_ingredients') . '" />';
  718. include($this->pluginPath . 'includes/ingredient-form.php');
  719. }
  720. /**
  721. * Sets up the box for the recipe details.
  722. *
  723. * @global object $post
  724. */
  725. function details_box() {
  726. global $post;
  727. /* Use nonce for verification */
  728. echo '<input type="hidden" name="details_noncename" id="details_noncename" value="' . wp_create_nonce('recipe_press_details') . '" />';
  729. include ($this->pluginPath . 'includes/details-form.php');
  730. }
  731. /**
  732. * Sets up the nutrients box.
  733. *
  734. * @global object $post
  735. */
  736. function nutrients_box() {
  737. global $post;
  738. /* Use nonce for verification */
  739. echo '<input type="hidden" name="nutrients_noncename" id="nutrients_noncename" value="' . wp_create_nonce('recipe_press_nutrients') . '" />';
  740. include($this->pluginPath . 'includes/nutrients-form.php');
  741. }
  742. }