/** * Retrieve addons filtered by package, employee, event month, event type or category. * * @since 1.4 * @return void */ public function list_addons() { $response = array(); $package_addons = array(); $addons = array(); do_action('mdjm_before_api_list_addons', $this); if (isset($this->request['package'])) { if (!is_numeric($this->request['package'])) { // Using name or slug $package = mdjm_get_package_by('name', $this->request['package']); if ($package) { $package_id = $package->ID; } } else { $package_id = $this->request['package']; } if (!empty($package_id)) { $package_addons = mdjm_get_package_addons($package_id); if ($package_addons) { foreach ($package_addons as $package_addon) { $all_addons[] = mdjm_get_addon($package_addon); } } } } else { $all_addons = mdjm_get_addons(array('suppress_filters' => false)); } if (!empty($all_addons)) { foreach ($all_addons as $addon) { if (isset($this->request['employee_id']) && !mdjm_employee_has_addon($addon->ID, $this->request['employee_id'])) { continue; } if (isset($this->request['event_month']) && !mdjm_addon_is_available_for_event_date($addon->ID, $this->request['event_month'])) { continue; } if (isset($this->request['event_type']) && !mdjm_addon_is_available_for_event_type($addon->ID, $this->request['event_type'])) { continue; } $addons[] = $addon->ID; } } if (empty($addons)) { $error = array(); $error['error'] = __('No addons found.', 'mobile-dj-manager'); $this->data = $error; $this->output(); } $response['addons'] = array(); $i = 0; foreach ($addons as $addon) { $response['addons'][$addon] = mdjm_get_addon_data($addon); $i++; } $response['count'] = $i; $this->data = array_merge($this->data, $response); do_action('mdjm_after_api_list_addons', $this); $this->output(); }
/** * Renders a dropdown list of equipment add-ons. * * @since 1.3.7 * @param arr $args @see $default * @return str */ public function addons_dropdown($args = array()) { $defaults = array('name' => 'event_addons', 'id' => '', 'class' => '', 'selected' => '', 'show_option_none' => __('No Addons', 'mobile-dj-manager'), 'show_option_all' => false, 'chosen' => false, 'employee' => false, 'event_type' => false, 'event_date' => false, 'placeholder' => null, 'multiple' => true, 'package' => '', 'cost' => true, 'desc' => false, 'titles' => false, 'options_only' => false, 'blank_first' => false, 'data' => array()); $args = wp_parse_args($args, $defaults); $options = array(); $titles = array(); $addons = mdjm_get_addons(); if ($addons) { foreach ($addons as $addon) { if (!empty($args['package'])) { if (is_numeric($args['package'])) { $package = mdjm_get_package($args['package']); } else { $package = mdjm_get_package_by('slug', $args['package']); } if ($package) { $package_items = mdjm_get_package_addons($package->ID); } if (!empty($package_items) && in_array($addon->ID, $package_items)) { continue; } } if (!empty($args['employee'])) { if (!mdjm_employee_has_addon($addon->ID, $args['employee'])) { continue; } } if ($args['event_type']) { if (!mdjm_addon_is_available_for_event_type($addon->ID, $args['event_type'])) { continue; } } if ($args['event_date']) { if (!mdjm_addon_is_available_for_event_date($addon->ID, $args['event_date'])) { continue; } } else { $args['event_date'] = NULL; } $price = ''; if ($args['cost'] == true) { $price .= ' - ' . mdjm_currency_filter(mdjm_format_amount(mdjm_get_addon_price($addon->ID, $args['event_date']))); } $desc = ''; if ($args['desc']) { $desc .= ' - ' . mdjm_get_addon_excerpt($addon->ID, $args['desc']); } $term = ''; $terms = get_the_terms($addon->ID, 'addon-category'); if (!empty($terms)) { $term = esc_html($terms[0]->name); } $args['options']['groups'][$term][] = array($addon->ID => $addon->post_title . $price . $desc); if ($args['titles']) { $titles[$addon->ID] = mdjm_get_addon_excerpt($addon->ID); } } } if (!empty($args['options']['groups'])) { ksort($args['options']['groups']); } if (!empty($titles)) { $args['titles'] = $titles; } $output = $this->select($args); return $output; }
/** * Removes an addon from a package. * * @since 1.4 * @param int $package_id The package ID from which to remove the addon. * @param int $addon_id The ID of the addon to remove */ function mdjm_remove_addon_from_package($package_id, $addon_id) { $addons = mdjm_get_package_addons($package_id); $items = array(); if ($addons) { foreach ($addons as $addon) { if ($addon_id != $addon) { $items[] = $addon; } } update_post_meta($package_id, '_package_items', $items); } }
/** * Define the data to be displayed in each of the custom columns for the Package post types * * @since 1.4 * @param str $column_name The name of the column to display * @param int $post_id The current post ID * @return */ function mdjm_package_posts_custom_column($column_name, $post_id) { global $post; switch ($column_name) { // Items case 'items': $items = mdjm_get_package_addons($post_id); if ($items) { $i = 0; foreach ($items as $item) { echo '<a href="' . admin_url("post.php?post={$item}&action=edit") . '">' . mdjm_get_addon_name($item) . '</a>'; $i++; if ($i < count($items)) { echo '<br />'; } } } break; // Category // Category case 'package_category': echo get_the_term_list($post_id, 'package-category', '', ', ', ''); break; // Availability // Availability case 'availability': $output = array(); if (!mdjm_package_is_restricted_by_date($post_id)) { $output[] = __('Always', 'mobile-dj-manager'); } else { $availability = mdjm_get_package_months_available($post_id); if (!$availability) { $output[] = __('Always', 'mobile-dj-manager'); } else { $i = 0; foreach ($availability as $month) { $output[] = mdjm_month_num_to_name($availability[$i]); $i++; } } } echo implode(', ', $output); break; // Event Types // Event Types case 'event_types': $output = array(); $event_label = mdjm_get_label_singular(); $event_types = mdjm_get_package_event_types($post_id); if (in_array('all', $event_types)) { $output[] = sprintf(__('All %s Types', 'mobile-dj-manager'), $event_label); } else { foreach ($event_types as $event_type) { $term = get_term($event_type, 'event-types'); if (!empty($term)) { $output[] = $term->name; } } } echo implode(', ', $output); break; // Employees // Employees case 'employees': $employees = mdjm_get_employees_with_package($post_id); $output = array(); if (in_array('all', $employees)) { $output[] .= __('All Employees', 'mobile-dj-manager'); } else { foreach ($employees as $employee) { if ('all' == $employee) { continue; } $output[] = '<a href="' . get_edit_user_link($employee) . '">' . mdjm_get_employee_display_name($employee) . '</a>'; } } echo implode('<br />', $output); break; // Price // Price case 'price': if (mdjm_package_has_variable_prices($post_id)) { $range = mdjm_get_package_price_range($post_id); echo mdjm_currency_filter(mdjm_format_amount($range['low'])); echo ' — '; echo mdjm_currency_filter(mdjm_format_amount($range['high'])); } else { echo mdjm_currency_filter(mdjm_format_amount(mdjm_get_package_price($post_id))); } break; case 'usage': $count = mdjm_count_events_with_package($post_id); echo $count . ' ' . _n(mdjm_get_label_singular(), mdjm_get_label_plural(), $count, 'mobile-dj-manager'); break; } // switch }
/** * Retrieve all addons within the given package slug * * @since 1.4 * @param str $slug Required: Slug of the package for which to search * @return arr $addons Array of all addons */ function mdjm_addons_by_package_slug($slug) { _deprecated_function(__FUNCTION__, '1.4', "mdjm_get_addons_by_package()"); $package = mdjm_get_package_by('slug', strtolower($slug)); // No package returns false if (empty($package)) { return false; } return mdjm_get_package_addons($package->ID); }
/** * Output the package items row * * @since 1.4 * @param int $post The WP_Post object. * @return str */ function mdjm_package_metabox_items_row($post) { $items = mdjm_get_package_addons($post->ID); $currency_position = mdjm_get_option('currency_format', 'before'); ?> <div id="mdjm-package-items-fields" class="mdjm_items_fields"> <input type="hidden" id="mdjm_package_items" class="mdjm_package_item_name_field" value="" /> <div id="mdjm_item_fields" class="mdjm_meta_table_wrap"> <table class="widefat mdjm_repeatable_table"> <thead> <tr> <th style="width: 50px;"><?php _e('Item', 'mobile-dj-manager'); ?> </th> <?php do_action('mdjm_package_price_table_head', $post->ID); ?> <th style="width: 2%"></th> </tr> </thead> <tbody> <?php if (!empty($items)) { ?> <?php foreach ($items as $item) { ?> <tr class="mdjm_items_wrapper mdjm_repeatable_row"> <?php do_action('mdjm_render_item_row', $item, $post->ID); ?> </tr> <?php } ?> <?php } else { ?> <tr class="mdjm_items_wrapper mdjm_repeatable_row"> <?php do_action('mdjm_render_item_row', null, $post->ID); ?> </tr> <?php } ?> <tr> <td class="submit" colspan="2" style="float: none; clear:both; background:#fff;"> <a class="button-secondary mdjm_add_repeatable" style="margin: 6px 0;"><?php _e('Add New Item', 'mobile-dj-manager'); ?> </a> </td> </tr> </tbody> </table> </div> </div> <?php }
/** * Addons List Shortcode. * * @param arr $atts Shortcode attributes. See $atts. * @param str|int $filter_value The value to which to filter $filter_by. Default false (all). * @param str $list List type to display. li for bulleted. Default p. * @param bool $cost Whether or not display the price. Default false. * * */ function mdjm_shortcode_addons_list($atts) { global $post; $atts = shortcode_atts(array('filter_by' => false, 'filter_value' => false, 'list' => 'p', 'desc' => false, 'desc_length' => mdjm_get_option('package_excerpt_length', 55), 'cost' => false, 'addon_class' => false, 'cost_class' => false, 'desc_class' => false), $atts, 'mdjm-addons'); ob_start(); $output = ''; if (!empty($post) && 'mdjm-package' == get_post_type($post->ID)) { $package_addons = mdjm_get_package_addons($post->ID); $addons = array(); foreach ($package_addons as $package) { $addons[] = mdjm_get_addon($package); } } elseif (!empty($atts['filter_by']) && !empty($atts['filter_value']) && $atts['filter_by'] != 'false' && $atts['filter_value'] != 'false') { // Filter addons by user if ($atts['filter_by'] == 'category') { $addons = mdjm_get_addons_in_category($atts['filter_value']); } elseif ($atts['filter_by'] == 'package') { if (!is_numeric($atts['filter_value'])) { // For backwards compatibility $package = mdjm_get_package_by('slug', $atts['filter_value']); if ($package) { $atts['filter_value'] = $package->ID; } } $package_addons = mdjm_get_package_addons($atts['filter_value']); $addons = array(); foreach ($package_addons as $package) { $addons[] = mdjm_get_addon($package); } } elseif ($atts['filter_by'] == 'user') { $addons = mdjm_get_addons_by_employee($atts['filter_value']); } } else { $addons = mdjm_get_addons(); } /** * Output the results */ if (!$addons) { $output .= '<p>' . __('No addons available', 'mobile-dj-manager') . '</p>'; } else { // Check to start bullet list if ($atts['list'] == 'li') { $output .= '<ul>'; } foreach ($addons as $addon) { // Output the remaining addons if (!empty($atts['list'])) { $output .= '<' . $atts['list'] . '>'; } if (!empty($atts['addon_class']) && $atts['addon_class'] != 'false') { $output = '<span class="' . $atts['addon_class'] . '">'; } $output .= $addon->post_title; if (!empty($atts['addon_class']) && $atts['addon_class'] != 'false') { $output = '</span>'; } $cost = mdjm_get_addon_price($addon->ID); if (!empty($atts['cost']) && $atts['cost'] != 'false' && !empty($cost)) { if (!empty($atts['cost_class']) && $atts['cost_class'] != 'false') { $output = '<span class="' . $atts['cost_class'] . '">'; } $output .= ' – ' . mdjm_currency_filter(mdjm_format_amount($cost)); if (!empty($atts['cost_class']) && $atts['cost_class'] != 'false') { $output = '</span>'; } } $desc = mdjm_get_addon_excerpt($addon->ID, $atts['desc_length']); if (!empty($atts['desc']) && $atts['desc'] != 'false' && !empty($desc)) { $output .= '<br />'; if (!empty($atts['desc_class']) && $atts['desc_class'] != 'false') { $output = '<span class="' . $atts['desc_class'] . '">'; } else { $output .= '<span style="font-style: italic; font-size: smaller;">'; } $output .= $desc; $output .= '</span>'; } if (!empty($atts['list'])) { $output .= '</' . $atts['list'] . '>'; } } // Check to end bullet list if ($atts['list'] == 'li') { $output .= '</ul>'; } } echo apply_filters('mdjm_shortcode_addons_list', $output); return ob_get_clean(); }