+<?php
+
+/**
+ * Sets up the Coupon post type and its taxonomies.
+ * Handles also the new fields in
+ * the metaboxes.
+ */
+class glm_coupon_coupon
+{
+
+ public $pluginDirName;
+
+ /**
+ * Contstructor for the coupon class
+ *
+ * Calls filters and actions to setup Coupon post type
+ *
+ * @param type $path Plugin Path
+ */
+ public function __construct($path)
+ {
+// global $typenow;
+ $this->pluginDirName = $path;
+ add_action('init', array($this, 'addPostTypes'));
+ add_action('init', array($this, 'createCouponTaxonomy'));
+
+ add_filter('post_updated_messages', array($this, 'couponsUpdatedMessages'));
+
+ add_filter('manage_edit-glm_coupons_columns',
+ array($this, 'couponsEditColumns'));
+
+ add_action('manage_posts_custom_column',
+ array($this, 'couponsCustomColumns'));
+ add_action('admin_init', array($this, 'metaDates'));
+ add_action('save_post', array($this, 'saveCouponMeta'));
+ add_filter('manage_edit-glm_coupons_sortable_columns',
+ array($this, 'sortingColumns'));
+ add_action('restrict_manage_posts',
+ array($this, 'taxonomy_filter_restrict_manage_posts'));
+ add_filter('parse_query', array($this, 'couponFilter'));
+// var_dump($typenow);
+// add_filter('user_row_actions', array($this, 'remove_row_actions'));
+ }
+
+ // Filter the request to just give posts for the given taxonomy, if applicable.
+ function taxonomy_filter_restrict_manage_posts()
+ {
+ global $typenow;
+// var_dump($typenow);
+ if ($typenow == GLM_COUPON_POST_TYPE) {
+ $filters = get_object_taxonomies(GLM_COUPON_POST_TYPE);
+
+ foreach ($filters as $tax_slug) {
+ $tax_obj = get_taxonomy($tax_slug);
+ wp_dropdown_categories(array(
+ 'show_option_all' => __('Show All ' . $tax_obj->label),
+ 'taxonomy' => $tax_slug,
+ 'name' => $tax_obj->name,
+ 'orderby' => 'name',
+ 'selected' => (isset($_GET[$tax_slug]) ? $_GET[$tax_slug] : ''),
+ 'hierarchical' => $tax_obj->hierarchical,
+ 'show_count' => true,
+ 'hide_empty' => true
+ ));
+ }
+ }
+
+
+
+ }
+
+ function taxonomy_filter_post_type_request($query)
+ {
+ global $pagenow, $typenow;
+//var_dump($typenow);
+ if ('edit.php' == $pagenow && $typenow == GLM_COUPON_POST_TYPE) {
+ $filters = get_object_taxonomies(GLM_COUPON_POST_TYPE);
+// var_dump($filters);
+ foreach ($filters as $tax_slug) {
+ $var = &$query->query_vars[$tax_slug];
+// var_dump($var);
+// var_dump($tax_slug);
+ if (isset($var)) {
+ $term = get_term_by('id', $var, $tax_slug);
+ var_dump($term);
+ $var['term'] = $term->slug;
+ var_dump($var['term']);
+ var_dump($term->slug);
+ }
+ }
+ }
+ }
+
+ public function remove_row_actions($actions, $post)
+ {
+ global $curren_screen;
+ if ($curren_screen->post_type != GLM_COUPON_POST_TYPE) {
+ return $actions;
+ }
+ unset($actions['view']);
+ unset($actions['inline hide-if-no-js']);
+ return $actions;
+ }
+
+ /**
+ * Add sorting by columns
+ *
+ * @param type $columns Array of columns
+ *
+ * @return arrray
+ */
+ public function sortingColumns($columns)
+ {
+ $columns['glmcoupons_col_date'] = 'glmcoupons_col_date';
+ $columns['glmcoupons_col_cat'] = 'glmcoupons_col_cat';
+ $columns['glmcoupons_col_dep'] = 'glmcoupons_col_dep';
+ return $columns;
+ }
+
+ /**
+ * Filter for the parse_query action
+ *
+ * @param type $query WP_Query object
+ *
+ * @return Object $query
+ */
+ public function couponFilter($query)
+ {
+ global $pagenow, $typenow;
+ if ('edit.php' == $pagenow && $typenow == GLM_COUPON_POST_TYPE) {
+ $qv = & $query->query_vars;
+ if (isset($qv['post_type']) && $qv['post_type'] == GLM_COUPON_POST_TYPE) {
+ //$qv['post_type'] = 'page';//GLM_COUPON_POST_TYPE;
+ if (isset($qv[GLM_COUPON_TAX_CATEGORIES]) && $qv[GLM_COUPON_TAX_CATEGORIES] != 0) {
+ $term = get_term_by('id', $qv[GLM_COUPON_TAX_CATEGORIES], GLM_COUPON_TAX_CATEGORIES);
+ $qv[GLM_COUPON_TAX_CATEGORIES] = $term->slug;
+
+ if (isset($query->tax_query)) {
+ $tax_query =& $query->tax_query;
+ $queries =& $tax_query->queries;
+// echo '<pre>'.print_r($queries, true).'</pre>';
+ foreach ($queries as $key => &$qs) {
+ if ($qs['taxonomy'] == GLM_COUPON_TAX_CATEGORIES) {
+ $qs['terms'][$key] = $term->slug;
+ }
+ }
+ }
+
+ }
+ if (isset($qv[GLM_COUPON_TAX_DEPARTMENTS]) && $qv[GLM_COUPON_TAX_DEPARTMENTS] != 0) {
+ $term = get_term_by('id', $qv[GLM_COUPON_TAX_DEPARTMENTS], GLM_COUPON_TAX_DEPARTMENTS);
+ $qv[GLM_COUPON_TAX_DEPARTMENTS] = $term->slug;
+ }
+ if ($qv['post_type'] == GLM_COUPON_POST_TYPE) {
+// echo '<pre>'.print_r($qv, true).'</pre>';
+ }
+// echo '<pre>'.print_r($query, true).'</pre>';
+ }
+
+
+ return $query;
+ }
+ }
+
+ /**
+ * Setup the two drop downs for the Taxonomies
+ *
+ * @global type $wp_query
+ */
+ public function couponFilterList()
+ {
+ $screen = get_current_screen();
+ global $wp_query;
+ if ($screen->post_type == GLM_COUPON_POST_TYPE) {
+ $tax1 = get_taxonomy(GLM_COUPON_TAX_CATEGORIES);
+ wp_dropdown_categories(array(
+ 'show_option_all' => __("Show all {$tax1->label}"),
+ 'taxonomy' => GLM_COUPON_TAX_CATEGORIES,
+ 'name' => GLM_COUPON_TAX_CATEGORIES,
+ 'orderby' => 'name',
+ 'selected' => (isset($wp_query->query[GLM_COUPON_TAX_CATEGORIES])
+ ? $wp_query->query[GLM_COUPON_TAX_CATEGORIES]
+ : ''),
+ 'hierarchical' => true,
+ 'depth' => 3,
+ 'show_count' => true,
+ 'hide_empty' => true
+ ));
+ $tax2 = get_taxonomy(GLM_COUPON_TAX_DEPARTMENTS);
+ wp_dropdown_categories(array(
+ 'show_option_all' => __("Show all {$tax2->label}"),
+ 'taxonomy' => GLM_COUPON_TAX_DEPARTMENTS,
+ 'name' => GLM_COUPON_TAX_DEPARTMENTS,
+ 'orderby' => 'name',
+ 'selected' => (isset($wp_query->query[GLM_COUPON_TAX_DEPARTMENTS])
+ ? $wp_query->query[GLM_COUPON_TAX_DEPARTMENTS]
+ : ''),
+ 'hierarchical' => true,
+ 'depth' => 3,
+ 'show_count' => true,
+ 'hide_empty' => true
+ ));
+ }
+ }
+
+ /**
+ * Add meta box for dates
+ */
+ public function metaDates()
+ {
+ add_meta_box('glm_coupon_meta', 'Position Information',
+ array($this, 'editCouponMeta'), GLM_COUPON_POST_TYPE);
+ }
+
+ /**
+ * Build the edit part of the date
+ * @global type $post
+ */
+ public function editCouponMeta()
+ {
+ global $post;
+ $custom = get_post_custom($post->ID);
+ extract($custom);
+
+ $meta_sd = (isset($custom['glm_coupons_startdate']))
+ ? $custom['glm_coupons_startdate'][0]
+ : time();
+ $meta_ed = (isset($custom['glm_coupons_enddate']))
+ ? $custom['glm_coupons_enddate'][0]
+ : time();
+
+ $clean_sd = date('D, M d, Y', $meta_sd);
+ $clean_ed = date('D, M d, Y', $meta_ed);
+
+ $glm_coupons_status = (isset($glm_coupons_status[0]))
+ ? $glm_coupons_status[0]
+ : '';
+ $glm_coupons_pay_grade = (isset($glm_coupons_pay_grade[0]))
+ ? $glm_coupons_pay_grade[0]
+ : '';
+ $glm_coupons_shift = (isset($glm_coupons_shift[0]))
+ ? $glm_coupons_shift[0]
+ : '';
+ $glm_coupons_contact = (isset($glm_coupons_contact[0]))
+ ? $glm_coupons_contact[0]
+ : '';
+ $glm_coupons_email = (isset($glm_coupons_email[0]))
+ ? $glm_coupons_email[0]
+ : '';
+ $glm_coupons_comments = (isset($glm_coupons_comments[0]))
+ ? $glm_coupons_comments[0]
+ : '';
+ $glm_coupons_code = (isset($glm_coupons_code[0]))
+ ? $glm_coupons_code[0]
+ : '';
+
+ include $this->pluginDirName . 'views/admin/coupon_meta.php';
+ }
+
+ /**
+ * Save the coupon meta data
+ *
+ * @global type $post Post Object
+ *
+ * @return mixed
+ */
+ public function saveCouponMeta()
+ {
+ global $post;
+ if (!isset($_POST['glm-coupons-nonce'])) {
+ return;
+ }
+ // - still require nonce
+ if (!wp_verify_nonce($_POST['glm-coupons-nonce'], 'glm-coupons-nonce')) {
+ return $post->ID;
+ }
+
+ if (!current_user_can('edit_post', $post->ID)) {
+ return $post->ID;
+ }
+
+ // - convert back to unix & update post
+ if (!isset($_POST["glm_coupons_startdate"])) {
+ return $post;
+ }
+
+ $updatestartd = strtotime($_POST['glm_coupons_startdate']);
+ update_post_meta($post->ID, 'glm_coupons_startdate', $updatestartd);
+
+ if (!isset($_POST['glm_coupons_enddate'])) {
+ return $post;
+ }
+ $updateendd = strtotime($_POST['glm_coupons_enddate']);
+ update_post_meta($post->ID, 'glm_coupons_enddate', $updateendd);
+
+ update_post_meta($post->ID, 'glm_coupons_status', $_POST['glm_coupons_status']);
+ update_post_meta($post->ID, 'glm_coupons_pay_grade',
+ $_POST['glm_coupons_pay_grade']);
+ update_post_meta($post->ID, 'glm_coupons_shift', $_POST['glm_coupons_shift']);
+ update_post_meta($post->ID, 'glm_coupons_contact',
+ $_POST['glm_coupons_contact']);
+ update_post_meta($post->ID, 'glm_coupons_email', $_POST['glm_coupons_email']);
+ update_post_meta($post->ID, 'glm_coupons_comments',
+ $_POST['glm_coupons_comments']);
+ update_post_meta($post->ID, 'glm_coupons_code', $_POST['glm_coupons_code']);
+ }
+
+ /**
+ * Create new Post type
+ */
+ public function addPostTypes()
+ {
+ if (isset($_REQUEST['post_type']) && $_REQUEST['post_type'] == GLM_COUPON_POST_TYPE) {
+ add_filter('months_dropdown_results', '__return_empty_array');
+ }
+ $labels = array(
+ 'name' => _x('Coupons', 'coupon general name'),
+ 'singular_name' => _x('Coupon', 'coupon type singular name'),
+ 'add_new' => _x('Add New', 'coupon'),
+ 'add_new_item' => __('Add New Coupon'),
+ 'edit_item' => __('Edit Coupon'),
+ 'new_item' => __('New Coupon'),
+ 'all_items' => __('All Coupons'),
+ 'view_item' => __('View Coupon'),
+ 'search_items' => __('Search Coupons'),
+ 'not_found' => __('No Coupons found'),
+ 'not_found_in_trash' => __('No Coupons found in the Trash'),
+ 'parent_item_colon' => '',
+ 'menu_name' => 'Coupons'
+ );
+ $args = array(
+// 'label' => __('Coupons'),
+ 'labels' => $labels,
+ 'description' => 'Stores Coupons and Coupon data',
+ 'public' => true,
+ 'can_export' => true,
+ 'menu_position' => 21,
+ 'supports' => array('title', 'editor'),
+ 'has_archive' => true,
+ 'menu_icon' => 'dashicons-businessman',
+ 'capability_type' => 'post',
+ 'hierarchical' => false,
+ 'rewrite' => array('slug' => 'coupons'),
+ 'taxonomies' => array(GLM_COUPON_TAX_CATEGORIES, GLM_COUPON_TAX_DEPARTMENTS),
+ 'show_in_menu' => true,
+ 'show_ui' => true,
+ 'query_var' => true,
+ 'publicly_queryable' => true
+ );
+ register_post_type(GLM_COUPON_POST_TYPE, $args);
+ register_taxonomy_for_object_type(GLM_COUPON_TAX_CATEGORIES,
+ GLM_COUPON_POST_TYPE);
+ register_taxonomy_for_object_type(GLM_COUPON_TAX_DEPARTMENTS,
+ GLM_COUPON_POST_TYPE);
+ }
+
+ /**
+ * Update the message text for the coupon post type
+ *
+ * @global type $post Post object
+ * @global type $post_ID Id of post
+ *
+ * @param type $messages Messages array
+ *
+ * @return type
+ */
+ public function couponsUpdatedMessages($messages)
+ {
+ global $post, $post_ID;
+
+ $messages['post'] = array(
+ 0 => '', // Unused. Messages start at index 1.
+ 1 => sprintf(__('Coupon updated. <a href="%s">View item</a>'),
+ esc_url(get_permalink($post_ID))),
+ 2 => __('Custom field updated.'),
+ 3 => __('Custom field deleted.'),
+ 4 => __('Coupon updated.'),
+ /* translators: %s: date and time of the revision */
+ 5 => isset($_GET['revision'])
+ ? sprintf(__('Coupon restored to revision from %s'),
+ wp_post_revision_title((int) $_GET['revision'],
+ false))
+ : false,
+ 6 => sprintf(__('Coupon published. <a href="%s">View event</a>'),
+ esc_url(get_permalink($post_ID))),
+ 7 => __('Coupon saved.'),
+ 8 => sprintf(__('Coupon submitted. <a target="_blank" href="%s">Preview event</a>'),
+ esc_url(add_query_arg('preview', 'true',
+ get_permalink($post_ID)))),
+ 9 => sprintf(__('Coupon scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview coupon</a>'),
+ // translators: Publish box date format, see http://php.net/date
+ date_i18n(__('M j, Y @ G:i'),
+ strtotime($post->post_date)),
+ esc_url(get_permalink($post_ID))),
+ 10 => sprintf(__('Coupon draft updated. <a target="_blank" href="%s">Preview event</a>'),
+ esc_url(add_query_arg('preview', 'true',
+ get_permalink($post_ID)))),
+ );
+ return $messages;
+ }
+
+ /**
+ * Create the taxanomies for the post type
+ */
+ public function createCouponTaxonomy()
+ {
+ $labels = array(
+ 'name' => _x('Categories',
+ 'taxonomy general name'),
+ 'singular_name' => _x('Category',
+ 'taxonomy singular name'),
+ 'search_items' => __('Search Categories'),
+ 'popular_items' => __('Popular Categories'),
+ 'all_items' => __('All Categories'),
+ 'parent_item' => null,
+ 'parent_item_colon' => null,
+ 'edit_item' => __('Edit Category'),
+ 'update_item' => __('Update Category'),
+ 'add_new_item' => __('Add New Category'),
+ 'new_item_name' => __('New Category Name'),
+ 'separate_items_with_commas' => __('Separate categories with commas'),
+ 'add_or_remove_items' => __('Add or remove categories'),
+ 'choose_from_most_used' => __('Choose from the most used categories'),
+ );
+
+ register_taxonomy(
+ GLM_COUPON_TAX_CATEGORIES, GLM_COUPON_POST_TYPE,
+ array(
+ 'label' => __('Coupon Category'),
+ 'labels' => $labels,
+ 'hierarchical' => true,
+ 'show_ui' => true,
+ 'query_var' => GLM_COUPON_TAX_CATEGORIES,
+ 'rewrite' => array('slug' => GLM_COUPON_TAX_CATEGORIES),
+ 'show_admin_column' => true,
+ '_builtin' => true
+ ));
+
+ $labels = array(
+ 'name' => _x('Departments',
+ 'taxonomy general name'),
+ 'singular_name' => _x('Department',
+ 'taxonomy singular name'),
+ 'search_items' => __('Search Departments'),
+ 'popular_items' => __('Popular Departments'),
+ 'all_items' => __('All Departments'),
+ 'parent_item' => null,
+ 'parent_item_colon' => null,
+ 'edit_item' => __('Edit Department'),
+ 'update_item' => __('Update Department'),
+ 'add_new_item' => __('Add New Department'),
+ 'new_item_name' => __('New Department Name'),
+ 'separate_items_with_commas' => __('Separate departments with commas'),
+ 'add_or_remove_items' => __('Add or remove departments'),
+ 'choose_from_most_used' => __('Choose from the most used departments'),
+ );
+
+ register_taxonomy(
+ GLM_COUPON_TAX_DEPARTMENTS, GLM_COUPON_POST_TYPE,
+ array(
+ 'label' => __('Coupon Department'),
+ 'labels' => $labels,
+ 'hierarchical' => true,
+ 'show_ui' => true,
+ 'query_var' => GLM_COUPON_TAX_DEPARTMENTS,
+ 'rewrite' => array('slug' => GLM_COUPON_TAX_DEPARTMENTS),
+ ));
+ }
+
+ /**
+ * To set which fields appear in the post type admin list
+ *
+ * @param array $columns Columns for the admin list view
+ *
+ * @return array
+ */
+ function couponsEditColumns($columns)
+ {
+ $columns = array(
+ "cb" => "<input type=\"checkbox\" />",
+ "title" => "Coupon Title",
+ "glmcoupons_col_cat" => "Category",
+ "glmcoupons_col_dep" => "Department",
+ "glmcoupons_col_date" => "Dates",
+ );
+ return $columns;
+ }
+
+ /**
+ * Setup the custom columns for the admin list view
+ *
+ * @global type $post Post
+ *
+ * @param type $column Column
+ */
+ function couponsCustomColumns($column)
+ {
+ global $post;
+ $custom = get_post_custom();
+
+ switch ($column) {
+ case "glmcoupons_col_cat":
+ // - show taxonomy terms -
+ $eventcats = get_the_terms($post->ID, "glm_couponscategory");
+ $eventcats_html = array();
+ if ($eventcats) {
+ foreach ($eventcats as $eventcat) {
+ array_push($eventcats_html, $eventcat->name);
+ }
+ echo implode($eventcats_html, ", ");
+ } else {
+ _e('None', 'themeforce');
+ ;
+ }
+ break;
+ case "glmcoupons_col_dep":
+ // - show taxonomy terms -
+ $eventcats = get_the_terms($post->ID, "glm_couponsdepartment");
+ $eventcats_html = array();
+ if ($eventcats) {
+ foreach ($eventcats as $eventcat) {
+ array_push($eventcats_html, $eventcat->name);
+ }
+ echo implode($eventcats_html, ", ");
+ } else {
+ _e('None', 'themeforce');
+ ;
+ }
+ break;
+ case "glmcoupons_col_date":
+ // - show dates -
+ $startd = $custom['glm_coupons_startdate'][0];
+ $endd = $custom['glm_coupons_enddate'][0];
+ $startdate = date("F j, Y", $startd);
+ $enddate = date("F j, Y", $endd);
+ echo $startdate . '<br /><em>' . $enddate . '</em>';
+ break;
+ }
+ }
+
+}