public function insertTerm()
 {
     $taxId = dpa_get_event_tax_id();
     foreach ($this->actions as $actionName => $desc) {
         $e = term_exists($actionName, $taxId);
         if ($e === 0 || $e === null) {
             wp_insert_term($actionName, $taxId, array('description' => $desc));
         }
     }
 }
Example #2
0
/**
 * If multisite and running network-wide, clear custom caches when something
 * is added, removed, or updated in the Events taxonomy.
 *
 * @param int|array $ids Single or list of term IDs
 * @param string $taxonomy Taxonomy
 * @see dpa_register_events()
 * @since Achievements (3.0)
 */
function dpa_clear_events_tax_cache($ids, $taxonomy)
{
    if (dpa_get_event_tax_id() !== $taxonomy) {
        return;
    }
    // If multisite and running network-wide, clear the registered events cache for the events taxonomy.
    if (is_multisite() && dpa_is_running_networkwide()) {
        wp_cache_delete('dpa_registered_events', 'achievements_events');
    }
}
Example #3
0
/**
 * Retrieves a list of achievement posts matching criteria
 *
 * Most of the values that $args can accept are documented in {@link WP_Query}. The custom values added by Achievements are as follows:
 * 'ach_event' - string - Loads achievements for a specific event. Matches a slug from the dpa_event tax. Default is empty.
 *
 * If you try to use this function, you will need to implement your own switch_to_blog and wp_reset_postdata() handling if running in a multisite
 * and in a dpa_is_running_networkwide() configuration, otherwise the data won't be fetched from the appropriate site.
 *
 * @param array|string $args All the arguments supported by {@link WP_Query}, and some more.
 * @return array Posts
 * @since Achievements (3.0)
 */
function dpa_get_achievements($args = array())
{
    $defaults = array('ignore_sticky_posts' => true, 'no_found_rows' => true, 'order' => 'ASC', 'orderby' => 'title', 'post_type' => dpa_get_achievement_post_type(), 'posts_per_page' => -1, 'ach_event' => '');
    // Load achievements for a specific event
    if (!empty($args['ach_event'])) {
        $args['tax_query'] = array(array('field' => 'slug', 'taxonomy' => dpa_get_event_tax_id(), 'terms' => $args['ach_event']));
        unset($args['ach_event']);
    }
    $args = dpa_parse_args($args, $defaults, 'get_achievements');
    $achievements = new WP_Query();
    return apply_filters('dpa_get_achievements', $achievements->query($args), $args);
}
Example #4
0
/**
 * Check if any of the plugin extensions need to be set up or updated.
 *
 * If a new extension is found (technically, an extension with an un-registered 'ID'), the
 * actions that the extension supports will be automatically added to the dpa_get_event_tax_id() taxonomy.
 * 
 * If an extension is updated (by changing the 'version' property), its actions WILL NOT be automatically updated.
 * You will need to implement custom upgrade handling in the do_update() method to add, remove, or update taxonomy
 * data for the extension.
 *
 * @since Achievements (3.0)
 */
function dpa_maybe_update_extensions()
{
    // If Achievements is being deactivated, bail out
    if (dpa_is_deactivation(achievements()->basename)) {
        return;
    }
    // Only do things if the user is active (logged in and not a spammer), in wp-admin, and is not doing an import.
    if (!dpa_is_user_active() || !is_admin() || defined('WP_IMPORTING') && WP_IMPORTING) {
        return;
    }
    // Check user has the ability to edit and create terms
    if (!current_user_can('edit_achievement_events')) {
        return;
    }
    $orig_versions = $versions = dpa_get_extension_versions();
    foreach (achievements()->extensions as $extension) {
        // Extensions must inherit the DPA_Extension class
        if (!is_a($extension, 'DPA_Extension')) {
            continue;
        }
        // If no old version in $versions, it's a new extension. Add its actions to the dpa_event taxonomy.
        $id = $extension->get_id();
        if (!isset($versions[$id])) {
            $actions = $extension->get_actions();
            // Add the actions to the dpa_event taxonomy
            foreach ($actions as $action_name => $action_desc) {
                wp_insert_term($action_name, dpa_get_event_tax_id(), array('description' => $action_desc));
            }
            // Record version
            $versions[$id] = $extension->get_version();
            // Check if an update is available.
        } elseif (version_compare($extension->get_version(), $versions[$id], '>')) {
            $extension->do_update($versions[$id]);
            $versions[$id] = $extension->get_version();
        }
    }
    // If $versions has changed, update the option in the database
    if ($orig_versions != $versions) {
        dpa_update_extension_versions($versions);
    }
    // Allow other plugins to run any update routines for their extension
    do_action('dpa_maybe_update_extensions', $orig_versions, $versions);
}
Example #5
0
/**
 * The achievement post type loop.
 *
 * Most of the values that $args can accept are documented in {@link WP_Query}. The custom
 * values added by Achievements are as follows:
 *
 * 'ach_event'             - string   - Loads achievements for a specific event. Matches a slug from the dpa_event tax. Default is empty.
 * 'ach_populate_progress' - bool|int - Populate a user/users' progress for the results.
 *                                    - bool: True - uses the logged in user (default). False - don't fetch progress.
 *                                    - int: pass a user ID (single user).
 * 'ach_progress_status'   - array    - array: Post status IDs for the Progress post type.
 *
 * @param array|string $args All the arguments supported by {@link WP_Query}, and some more.
 * @return bool Returns true if the query has any results to loop over
 * @since Achievements (1.0)
 */
function dpa_has_achievements($args = array())
{
    // If multisite and running network-wide, switch_to_blog to the data store site
    if (is_multisite() && dpa_is_running_networkwide()) {
        switch_to_blog(DPA_DATA_STORE);
    }
    $default_post_parent = dpa_is_single_achievement() ? dpa_get_achievement_id() : 'any';
    $default_progress_status = dpa_is_single_user_achievements() ? array(dpa_get_unlocked_status_id()) : array(dpa_get_locked_status_id(), dpa_get_unlocked_status_id());
    $defaults = array('ignore_sticky_posts' => true, 'order' => 'ASC', 'orderby' => 'title', 'max_num_pages' => false, 'paged' => dpa_get_paged(), 'perm' => 'readable', 'post_parent' => $default_post_parent, 'post_type' => dpa_get_achievement_post_type(), 'posts_per_page' => dpa_get_achievements_per_page(), 'ach_progress_status' => $default_progress_status, 's' => !empty($_GET['achievements']) ? wp_unslash($_GET['achievements']) : '', 'ach_event' => '', 'ach_populate_progress' => false);
    // Load achievements for a specific event
    if (!empty($args['ach_event'])) {
        $args['tax_query'] = array(array('field' => 'slug', 'taxonomy' => dpa_get_event_tax_id(), 'terms' => $args['ach_event']));
        unset($args['ach_event']);
    }
    $args = dpa_parse_args($args, $defaults, 'has_achievements');
    extract($args);
    // Run the query
    achievements()->achievement_query = new WP_Query($args);
    // User to popular progress for
    $progress_user_ids = false;
    if (isset($args['ach_populate_progress'])) {
        if (true === $args['ach_populate_progress']) {
            if (dpa_is_single_user_achievements()) {
                $progress_user_ids = dpa_get_displayed_user_id();
            } elseif (is_user_logged_in()) {
                $progress_user_ids = get_current_user_id();
            }
        } else {
            $progress_user_ids = (int) $args['ach_populate_progress'];
        }
    }
    // If no limit to posts per page, set it to the current post_count
    if (-1 === $posts_per_page) {
        $posts_per_page = achievements()->achievement_query->post_count;
    }
    // Add pagination values to query object
    achievements()->achievement_query->posts_per_page = $posts_per_page;
    achievements()->achievement_query->paged = $paged;
    // Only add pagination if query returned results
    if (((int) achievements()->achievement_query->post_count || (int) achievements()->achievement_query->found_posts) && (int) achievements()->achievement_query->posts_per_page) {
        // Limit the number of achievements shown based on maximum allowed pages
        if (!empty($max_num_pages) && achievements()->achievement_query->found_posts > achievements()->achievement_query->max_num_pages * achievements()->achievement_query->post_count) {
            achievements()->achievement_query->found_posts = achievements()->achievement_query->max_num_pages * achievements()->achievement_query->post_count;
        }
        // If pretty permalinks are enabled, make our pagination pretty
        if ($GLOBALS['wp_rewrite']->using_permalinks()) {
            // Page or single post
            if (is_page() || is_single()) {
                $base = get_permalink();
            } elseif (dpa_is_achievement_archive()) {
                $base = dpa_get_achievements_url();
            } else {
                $base = get_permalink($post_parent);
            }
            // Use pagination base
            $base = trailingslashit($base) . user_trailingslashit($GLOBALS['wp_rewrite']->pagination_base . '/%#%/');
            // Unpretty pagination
        } else {
            $base = add_query_arg('paged', '%#%');
        }
        // Pagination settings with filter
        $achievement_pagination = apply_filters('dpa_achievement_pagination', array('base' => $base, 'current' => (int) achievements()->achievement_query->paged, 'format' => '', 'mid_size' => 1, 'next_text' => is_rtl() ? '←' : '→', 'prev_text' => is_rtl() ? '→' : '←', 'total' => $posts_per_page == achievements()->achievement_query->found_posts ? 1 : ceil((int) achievements()->achievement_query->found_posts / (int) $posts_per_page)));
        // Add pagination to query object
        achievements()->achievement_query->pagination_links = paginate_links($achievement_pagination);
        // Remove first page from pagination
        achievements()->achievement_query->pagination_links = str_replace($GLOBALS['wp_rewrite']->pagination_base . "/1/'", "'", achievements()->achievement_query->pagination_links);
    }
    // Populate extra progress information for the achievements
    if (!empty($progress_user_ids) && achievements()->achievement_query->have_posts()) {
        $progress_post_ids = wp_list_pluck((array) achievements()->achievement_query->posts, 'ID');
        // Args for progress query
        $progress_args = array('author' => $progress_user_ids, 'no_found_rows' => true, 'post_parent' => $progress_post_ids, 'post_status' => $args['ach_progress_status'], 'posts_per_page' => -1);
        // Run the query
        dpa_has_progress($progress_args);
    }
    // If multisite and running network-wide, undo the switch_to_blog
    if (is_multisite() && dpa_is_running_networkwide()) {
        restore_current_blog();
    }
    return apply_filters('dpa_has_achievements', achievements()->achievement_query->have_posts());
}
Example #6
0
 /**
  * Register the achievement event taxonomy
  *
  * @since Achievements (3.0)
  */
 public function register_taxonomies()
 {
     $labels = $tax = array();
     // Event tax labels
     $labels['event'] = array('add_new_item' => __('Add New Event', 'achievements'), 'all_items' => __('All', 'achievements'), 'edit_item' => __('Edit Event', 'achievements'), 'name' => _x('Events', 'event taxonomy general name', 'achievements'), 'new_item_name' => __('New Event Name', 'achievements'), 'popular_items' => __('Popular Events', 'achievements'), 'search_items' => __('Search Events', 'achievements'), 'singular_name' => _x('Event', 'event taxonomy singular name', 'achievements'), 'update_item' => __('Update Event', 'achievements'), 'view_item' => __('View Event', 'achievements'));
     // Action filter
     $tax = apply_filters('dpa_register_taxonomies_action', array('capabilities' => dpa_get_event_caps(), 'hierarchical' => false, 'labels' => $labels['event'], 'public' => false, 'query_var' => false, 'rewrite' => false, 'show_in_nav_menus' => false, 'show_tagcloud' => false, 'show_ui' => dpa_is_developer_mode(), 'update_count_callback' => 'dpa_update_event_term_count'));
     // Register the achievement event taxonomy
     register_taxonomy(dpa_get_event_tax_id(), dpa_get_achievement_post_type(), $tax);
 }
Example #7
0
/**
 * Outputs the content for the custom columns on the achievement post type index screen
 *
 * @param string $column
 * @param int $post_id
 * @since Achievements (3.0)
 */
function dpa_achievement_custom_column($column, $post_id)
{
    if ('karma' === $column) {
        dpa_achievement_points($post_id);
    } elseif ('achievement_type' === $column) {
        $existing_events = wp_get_post_terms($post_id, dpa_get_event_tax_id(), array('fields' => 'ids'));
        $existing_type = empty($existing_events) ? __('Award', 'achievements') : __('Event', 'achievements');
        echo $existing_type;
    } elseif ('dpa_thumb' === $column) {
        the_post_thumbnail('dpa-thumb');
    }
}
Example #8
0
 /**
  * Update routine for this extension.
  * 
  * A future version of Achievements will likely handle extension updates automagically.
  *
  * @param string $current_version
  * @since Achievements (3.4)
  */
 public function do_update($current_version)
 {
     // Upgrading to v2 -- "user_register" is new. Add the action to the dpa_event taxonomy.
     if ($current_version === 1) {
         wp_insert_term('user_register', dpa_get_event_tax_id(), array('description' => $this->actions['user_register']));
     }
 }