Exemplo n.º 1
0
/**
 * Callback function to format BuddyPress notification
 *
 * @see  buddypress/loader setup_globals()
 *
 * @package WP Idea Stream
 * @subpackage buddypress/notifications
 *
 * @since  2.0.0
 *
 * @param  string $action            string identifier for the notification
 * @param  int    $item_id           the id of the object the notification relates to (ID of an idea)
 * @param  int    $secondary_item_id the user id in case of rating actions, the comment id otherwise
 * @param  int    $total_items       number of notifications having the same action
 * @param  string $format            array is WP Admin Bar, string is notifications screen
 * @uses   buddypress() to get BuddyPress main instance
 * @uses   wp_idea_stream_get_post_type() to get the ideas post type identifier
 * @uses   bp_loggedin_user_domain() to get the logged in user profile url
 * @uses   wp_idea_stream_ideas_get_idea_permalink() to get the idea permalink
 * @uses   get_the_title() to get the title of the idea
 * @uses   bp_core_get_user_displayname() to get the user's display name
 * @uses   apply_filters() call 'wp_idea_stream_buddypress_multiple_{$action}_notification' to override multiple items notification
 *                         call 'wp_idea_stream_buddypress_single_{$action}_notification' to override single item notification
 * @uses   esc_url()  to sanitize url
 * @uses   esc_attr() to sanitize attribute
 * @uses   esc_html() to sanitize output
 * @uses   do_action() call 'wp_idea_stream_buddypress_format_notifications' to perform custom actions once the notification is formated
 * @return string                    notification output
 */
function wp_idea_stream_buddypress_format_notifications($action = '', $item_id = 0, $secondary_item_id = 0, $total_items = 0, $format = 'string')
{
    $bp = buddypress();
    switch ($action) {
        case 'new_' . wp_idea_stream_get_post_type() . '_comment':
            /**
             * BuddyPress is grouping notifications for the same component/component action
             * but we can have comments about several ideas, so best is to view all user's
             * ideas (in his profile)
             */
            if ((int) $total_items > 1) {
                $url = trailingslashit(bp_loggedin_user_domain() . $bp->ideastream->slug) . '?notif=' . $total_items;
                $title = __('New idea comments', 'wp-idea-stream');
                $text = sprintf(__('%d new idea comments', 'wp-idea-stream'), (int) $total_items);
                $filter = "wp_idea_stream_buddypress_multiple_{$action}_notification";
            } else {
                $url = wp_idea_stream_ideas_get_idea_permalink($item_id) . '?notif=1';
                $title = __('New idea comment', 'wp-idea-stream');
                $text = __('New idea comment', 'wp-idea-stream');
                // Viewing notifications on user's notification screen will give a bit more infos.
                if ('string' == $format) {
                    $text = sprintf(__('New comment about the idea: %s', 'wp-idea-stream'), strip_tags(get_the_title($item_id)));
                }
                $filter = "wp_idea_stream_buddypress_single_{$action}_notification";
            }
            break;
        case 'new_' . wp_idea_stream_get_post_type() . '_rate':
            if ((int) $total_items > 1) {
                $url = trailingslashit(bp_loggedin_user_domain() . $bp->ideastream->slug) . '?notif=' . $total_items;
                $title = __('New idea rating', 'wp-idea-stream');
                $text = sprintf(__('%d new idea ratings', 'wp-idea-stream'), (int) $total_items);
                $filter = "wp_idea_stream_buddypress_multiple_{$action}_notification";
            } else {
                $url = wp_idea_stream_ideas_get_idea_permalink($item_id) . '?notif=1';
                $title = __('New idea rating', 'wp-idea-stream');
                $text = __('New idea rating', 'wp-idea-stream');
                // Viewing notifications on user's notification screen will give a bit more infos.
                if ('string' == $format) {
                    $text = sprintf(__('%s rated the idea: %s', 'wp-idea-stream'), bp_core_get_user_displayname($secondary_item_id), strip_tags(get_the_title($item_id)));
                }
                $filter = "wp_idea_stream_buddypress_single_{$action}_notification";
            }
            break;
    }
    if ('string' == $format) {
        /**
         * @param  string                 the notification output
         * @param  string $url            the href attribute of the notification
         * @param  string $title          the title attribute of the notification
         * @param  string $text           the text for the notification
         * @param  int $total_items       the number of objects concerned
         * @param  int $item_id           the ID of the idea
         * @param  int $secondary_item_id the user id in case of rating actions, the comment id otherwise
         */
        $return = apply_filters($filter, '<a href="' . esc_url($url) . '" title="' . esc_attr($title) . '">' . esc_html($text) . '</a>', $url, $title, $text, (int) $total_items, $item_id, $secondary_item_id);
    } else {
        /**
         * @param  array                  the notification array
         * @param  string $url            the href attribute of the notification
         * @param  string $text           the text for the notification
         * @param  int $total_items       the number of objects concerned
         * @param  int $item_id           the ID of the idea
         * @param  int $secondary_item_id the user id in case of rating actions, the comment id otherwise
         */
        $return = apply_filters($filter, array('text' => $text, 'link' => $url), $url, $text, (int) $total_items, $item_id, $secondary_item_id);
    }
    /**
     * @param  string $action         the action of the notification
     * @param  int $item_id           the ID of the idea
     * @param  int $secondary_item_id the user id in case of rating actions, the comment id otherwise
     * @param  int $total_items       the number of objects concerned
     */
    do_action('wp_idea_stream_buddypress_format_notifications', $action, $item_id, $secondary_item_id, $total_items);
    return $return;
}
Exemplo n.º 2
0
/**
 * Handles updating an idea
 *
 * @package WP Idea Stream
 * @subpackage ideas/functions
 *
 * @since 2.0.0
 *
 * @uses   check_admin_referer() to check the request has been done from current site
 * @uses   wp_idea_stream_get_redirect_url() to get default redirect url
 * @uses   get_query_var() to get the value of a specific query var
 * @uses   wp_idea_stream_get_post_type() to get the ideas post type identifier
 * @uses   get_queried_object() to try to get the idea object WordPress built
 * @uses   wp_idea_stream_ideas_get_idea_by_name() to get an idea object out of its post name
 * @uses   wp_idea_stream_user_can() to check user's capability
 * @uses   wp_idea_stream_add_message() to add a feddback message to user
 * @uses   wp_safe_redirect() to safely redirect the user and avoid duplicates
 * @uses   wp_idea_stream_ideas_save_idea() to save the idea
 * @uses   wp_idea_stream_get_form_url() to get the add new form url
 * @uses   wp_idea_stream_ideas_get_idea_permalink() to get the idea link
 */
function wp_idea_stream_ideas_update_idea()
{
    global $wp_query;
    // Bail if not a post request
    if ('POST' != strtoupper($_SERVER['REQUEST_METHOD'])) {
        return;
    }
    // Bail if not a post idea request
    if (empty($_POST['wp_idea_stream']) || !is_array($_POST['wp_idea_stream'])) {
        return;
    }
    // Bail if it's not an update
    if (empty($_POST['wp_idea_stream']['_the_id'])) {
        return;
    }
    // Check nonce
    check_admin_referer('wp_idea_stream_save');
    $redirect = wp_idea_stream_get_redirect_url();
    // Get idea name
    $idea_name = get_query_var(wp_idea_stream_get_post_type());
    // Get Idea Object
    $idea = get_queried_object();
    // If queried object doesn't match or wasn't helpfull, try to get the idea using core function
    if (empty($idea->post_name) || empty($idea_name) || $idea_name != $idea->post_name) {
        $idea = wp_idea_stream_ideas_get_idea_by_name($idea_name);
    }
    // Found no idea, redirect and inform the user
    if (empty($idea->ID)) {
        wp_idea_stream_add_message(array('type' => 'error', 'content' => __('The idea you are trying to edit does not seem to exist.', 'wp-idea-stream')));
        // Redirect to main archive page
        wp_safe_redirect($redirect);
        exit;
    }
    // Checks if the user can edit the idea
    if (!wp_idea_stream_ideas_can_edit($idea)) {
        // Add feedback to the user
        wp_idea_stream_add_message(array('type' => 'error', 'content' => __('You are not allowed to edit this idea.', 'wp-idea-stream')));
        // Redirect to main archive page
        wp_safe_redirect($redirect);
        exit;
    }
    $updated = array_diff_key($_POST['wp_idea_stream'], array('save' => 'submit'));
    // Title & content are required
    if (empty($updated['_the_title']) || empty($updated['_the_content'])) {
        // Add feedback to the user
        wp_idea_stream_add_message(array('type' => 'error', 'content' => __('Title and description are required fields.', 'wp-idea-stream')));
        // Simply stop, so that the user keeps the posted values.
        return;
    }
    // Reset '_the_id' param to the ID of the idea found
    $updated['_the_id'] = $idea->ID;
    $feedback_message = array();
    $featured_error = __('There was a problem saving the featured image, sorry.', 'wp-idea-stream');
    $featured_type = 'info';
    // Take care of the featured image
    $thumbnail_id = (int) get_post_thumbnail_id($idea);
    if (!empty($updated['_the_thumbnail'])) {
        $thumbnail_src = key($updated['_the_thumbnail']);
        $thumbnail = reset($updated['_the_thumbnail']);
        // Update the Featured image
        if (!is_numeric($thumbnail) || $thumbnail_id !== (int) $thumbnail) {
            if (is_numeric($thumbnail)) {
                // validate the attachment
                if (!get_post($thumbnail)) {
                    $feedback_message[] = $featured_error;
                    // Set the new Featured image
                } else {
                    set_post_thumbnail($idea->ID, $thumbnail);
                }
            } else {
                $sideload = WP_Idea_Stream_Ideas_Thumbnail::start($thumbnail_src, $idea->ID);
                if (is_wp_error($sideload->result)) {
                    $feedback_message[] = $featured_error;
                }
            }
        }
        // Delete the featured image
    } elseif (!empty($thumbnail_id)) {
        delete_post_thumbnail($idea);
    }
    // Update the idea
    $id = wp_idea_stream_ideas_save_idea($updated);
    if (empty($id)) {
        // Set the feedback for the user
        $featured_type = 'error';
        $feedback_message = __('Something went wrong while trying to update your idea.', 'wp-idea-stream');
        // Redirect to the form
        $redirect = wp_idea_stream_get_form_url(wp_idea_stream_edit_slug(), $idea_name);
        // Redirect to the idea
    } else {
        $redirect = wp_idea_stream_ideas_get_idea_permalink($id);
    }
    if (!empty($feedback_message)) {
        // Add feedback to the user
        wp_idea_stream_add_message(array('type' => $featured_type, 'content' => join(' ', $feedback_message)));
    }
    wp_safe_redirect($redirect);
    exit;
}
Exemplo n.º 3
0
/**
 * Output the Idea Ratings if needed into the Embedded idea
 *
 * @since  2.3.0
 *
 * @return string HTML output
 */
function wp_idea_stream_ideas_embed_meta()
{
    $idea = get_post();
    if (!isset($idea->post_type) || wp_idea_stream_get_post_type() !== $idea->post_type || wp_idea_stream_is_rating_disabled()) {
        return;
    }
    // Get the Average Rate
    $average_rate = wp_idea_stream_ideas_get_average_rating($idea->ID);
    if (!$average_rate) {
        return;
    }
    // Get rating link
    $rating_link = wp_idea_stream_ideas_get_idea_permalink($idea) . '#rate';
    ?>
	<div class="wp-idea-stream-embed-ratings">
		<a href="<?php 
    echo esc_url($rating_link);
    ?>
" target="_top">
			<span class="dashicons ideastream-star-filled"></span>
			<?php 
    printf(esc_html__('%1$sAverage Rating:%2$s%3$s', 'wp-idea-stream'), '<span class="screen-reader-text">', '</span>', $average_rate);
    ?>
		</a>
	</div>
	<?php 
}
Exemplo n.º 4
0
 /**
  * Create specific updated messages for ideas
  *
  * @package WP Idea Stream
  * @subpackage admin/admin
  *
  * @since 2.0.0
  *
  * @global $post
  * @param  array  $messages list of available updated messages
  * @uses   wp_idea_stream_is_admin() to check if on an IdeaStream Administration screen
  * @uses   apply_filters() call 'wp_idea_stream_admin_updated_messages' to add/edit/remove updated messages
  * @uses   esc_url() to sanitize a link
  * @uses   wp_idea_stream_ideas_get_idea_permalink() to get the idea permalink
  * @uses   wp_post_revision_title()
  * @uses   date_i18n() to format a date
  * @uses   add_query_arg() to add query vars to an url
  * @return array  the original updated messages if not on an IdeaStream screen, custom messages otherwise
  */
 public function ideas_updated_messages($messages = array())
 {
     global $post;
     // Bail if not posting/editing an idea
     if (!wp_idea_stream_is_admin()) {
         return $messages;
     }
     /**
      * @param  array list of IdeaStream updated messages
      */
     $messages[$this->post_type] = apply_filters('wp_idea_stream_admin_updated_messages', array(0 => '', 1 => sprintf(__('Idea updated. <a href="%s">View idea</a>', 'wp-idea-stream'), esc_url(wp_idea_stream_ideas_get_idea_permalink($post))), 2 => __('Custom field updated.', 'wp-idea-stream'), 3 => __('Custom field deleted.', 'wp-idea-stream'), 4 => __('Idea updated.', 'wp-idea-stream'), 5 => isset($_GET['revision']) ? sprintf(__('Idea restored to revision from %s', 'wp-idea-stream'), wp_post_revision_title((int) $_GET['revision'], false)) : false, 6 => sprintf(__('Idea published. <a href="%s">View idea</a>', 'wp-idea-stream'), esc_url(wp_idea_stream_ideas_get_idea_permalink($post))), 7 => __('Idea saved.', 'wp-idea-stream'), 8 => sprintf(__('Idea submitted. <a target="_blank" href="%s">Preview idea</a>', 'wp-idea-stream'), esc_url(add_query_arg('preview', 'true', wp_idea_stream_ideas_get_idea_permalink($post)))), 9 => sprintf(__('Idea scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview idea</a>', 'wp-idea-stream'), date_i18n(_x('M j, Y @ G:i', 'Idea Publish box date format', 'wp-idea-stream'), strtotime($post->post_date)), esc_url(wp_idea_stream_ideas_get_idea_permalink($post))), 10 => sprintf(__('Idea draft updated. <a target="_blank" href="%s">Preview idea</a>', 'wp-idea-stream'), esc_url(add_query_arg('preview', 'true', wp_idea_stream_ideas_get_idea_permalink($post))))));
     return $messages;
 }