Skip to:
Content
Pages
Categories
Search
Top
Bottom

Forum Replies Created

Viewing 7 replies - 1 through 7 (of 7 total)

  • bikerwp000
    Participant

    @bikerwp000

    Hey danbp,

    Thanks a lot for the follow up. No worry! If this sounds like a long shot, I guess I’ll just have to work around it. At least we tried. Appreciate your help. Have a great weekend!


    bikerwp000
    Participant

    @bikerwp000

    Hi danbp,

    Thanks a lot regarding the profile update. I hid the “What’s New” box on my site with CSS code so I must have somehow accidentally posted those update before I hid it. Silly me -:) Thanks again.


    bikerwp000
    Participant

    @bikerwp000

    Hi Paul,

    By all means, please do your testing too. My testing was entirely from the frontend as a non-dev. So it’s by far not something to rely on. My conclusion seems to work on the surface but anything underneath is definitely beyond me.

    I’ve been busy all day with that the other inquiry trying to figure out the tutorial on Adding custom filters to (Activity) loops. So this is to thank you for the help on both inquiries. I really appreciate that.


    bikerwp000
    Participant

    @bikerwp000

    Hi Paul,

    “Second issue – any chance of a screenshot, please?”

    Most certainly. I apologize not including one in the original post.

    http://s3.postimg.org/ve5g34z6b/Screenshot_28.png

    Near the center you’ll see
    @newbuddy1 https://youtu.be/wonqhbSpUyY View

    I have no idea what they are and how they got there.

    Thank you for helping.


    bikerwp000
    Participant

    @bikerwp000

    <?php
    /**
     * Plugin Name:       BP Loop Filters
     * Plugin URI:        https://codex.buddypress.org/add-custom-filters-to-loops-and-enjoy-them-within-your-plugin
     * Description:       Plugin example to illustrate loop filters
     * Version:           1.0
     * Author:            imath
     * Author URI:        http://imathi.eu
     * License:           GPL-2.0+
     * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
     */
    
    // Exit if accessed directly
    if ( !defined( 'ABSPATH' ) ) exit;
    
    class BP_Loop_Filters {
    
    	/**
    	 * Constructor
    	 */
    	public function __construct() {
    		$this->setup_actions();
    		$this->setup_filters();
    	}
    
    	/**
    	 * Actions
    	 *
    	 * @uses bp_is_active()
    	 * @uses is_multisite()
    	 */
    	private function setup_actions() {
    		/**
    		 * Adds the random order to the select boxes of the Members, Groups and Blogs directory pages
    		 */
    		// Members component is core, so it will be available
    		add_action( 'bp_members_directory_order_options', array( $this, 'random_order' ) );
    
    		// You need to check Groups component is available
    		if ( bp_is_active( 'groups' ) ) {
    			add_action( 'bp_groups_directory_order_options',  array( $this, 'random_order' ) );
    		}
    
    		// You need to check WordPress config and that Blogs Component is available
    		if ( is_multisite() && bp_is_active( 'blogs' ) ) {
    			add_action( 'bp_blogs_directory_order_options',   array( $this, 'random_order' ) );
    		}
    
    		/**
    		 * Registers the Activity actions so that they are available in the Activity Administration Screen
    		 */
    		// You need to check Activity component is available
    		if ( bp_is_active( 'activity' ) ) {
    
    			add_action( 'bp_register_activity_actions', array( $this, 'register_activity_actions' ) );
    
    			// Adds a new filter into the select boxes of the Activity directory page,
    			// of group and member single items activity screens
    			add_action( 'bp_activity_filter_options',        array( $this, 'display_activity_actions' ) );
    			add_action( 'bp_member_activity_filter_options', array( $this, 'display_activity_actions' ) );
    
    			// You need to check Groups component is available
    			if ( bp_is_active( 'groups' ) ) {
    				add_action( 'bp_group_activity_filter_options', array( $this, 'display_activity_actions' ) );
    			}
    
    	        // You're going to output the favorite count after action buttons
    			add_action( 'bp_activity_entry_meta', array( $this, 'display_favorite_count' ) );
    		}
    
    	}
    
    	/**
    	 * Displays a new option in the Members/Groups & Blogs directories
    	 *
    	 * @return string html output
    	 */
    	public function random_order() {
    		?>
    		<option value="random"><?php _e( 'Random', 'buddypress' ); ?></option>
    		<?php
    	}
    
    	/**
    	 * Registering the Activity actions for your component
    	 *
    	 * The registered actions will also be available in Administration
    	 * screens
    	 *
    	 * @uses bp_activity_set_action()
    	 * @uses is_admin()
    	 */
    	public function register_activity_actions() {
    		/* arguments are :
    		- 'component_id', 'component_action_type' to use in {$wpdb->prefix}bp_activity database
    		- and 'caption' to display in the select boxes */
    		bp_activity_set_action( 'bp_plugin', 'bpplugin_action', __( 'BP Plugin Action' ) );
    
    		/* Activity Administration screen does not use bp_ajax_querystring
    		Moreover This action type is reordering instead of filtering so you will only
    		use it on front end */
    		if ( ! is_admin() ) {
    			bp_activity_set_action( 'bp_plugin', 'activity_mostfavs', __( 'Most Favorited' ) );
    		}
    	}
    
    	/**
    	 * Building an array to loop in from our display function
    	 *
    	 * Using bp_activity_get_types() will list all registered activity actions
    	 * but you need to get the ones for your plugin, and this particular function
    	 * directly returns an array of key => value. As you need to filter activity
    	 * with your component id, the global buddypress()->activity->actions will be
    	 * more helpful.
    	 *
    	 * @uses buddypress()
    	 * @return array the list of your plugin actions.
    	 */
    	private function list_actions() {
    
    		$bp_activity_actions = buddypress()->activity->actions;
    
    		$bp_plugin_actions = array();
    
    		if ( !empty( $bp_activity_actions->bp_plugin ) ) {
    			$bp_plugin_actions = array_values( (array) $bp_activity_actions->bp_plugin );
    		}
    
    		return $bp_plugin_actions;
    	}
    
    	/**
    	 * Displays new actions into the Activity select boxes
    	 * to filter activities
    	 * - Activity Directory
    	 * - Single Group and Member activity screens
    	 *
    	 * @return string html output
    	 */
    	public function display_activity_actions() {
    		$bp_plugin_actions = $this->list_actions();
    
    		if ( empty( $bp_plugin_actions ) ) {
    			return;
    		}
    
    		foreach ( $bp_plugin_actions as $type ):?>
    			<option value="<?php echo esc_attr( $type['key'] );?>"><?php echo esc_attr( $type['value'] ); ?></option>
    		<?php endforeach;
    	}
    
    	/**
    	 * Displays a mention to inform about the number of time the activity
    	 * was favorited.
    	 *
    	 * @global BP_Activity_Template $activities_template
    	 * @return string html output
    	 */
    	public function display_favorite_count() {
    		global $activities_template;
    
    		// BuddyPress < 2.0 or filtering bp_use_legacy_activity_query
    		if ( ! empty( $activities_template->activity->favorite_count ) ) {
    			$fav_count = $activities_template->activity->favorite_count;
    		} else {
    			// This meta should already have been cached by BuddyPress :)
    			$fav_count = (int) bp_activity_get_meta( bp_get_activity_id(), 'favorite_count' );
    		}
    
    		if ( ! empty( $fav_count ) ): ?>
    			<a name="favorite-<?php bp_activity_id();?>" class="button bp-primary-action">Favorited <span><?php printf( _n( 'once', '%s times', $fav_count ), $fav_count );?></span></a>
    		<?php endif;
    	}
    
    	/**
    	 * Filters
    	 */
    	private function setup_filters() {
    		add_filter( 'bp_ajax_querystring',              array( $this, 'activity_querystring_filter' ), 12, 2 );
    		add_filter( 'bp_activity_get_user_join_filter', array( $this, 'order_by_most_favorited' ),     10, 6 );
    		add_filter( 'bp_activity_paged_activities_sql', array( $this, 'order_by_most_favorited'),      10, 2 );
    
    		// Maybe Fool Heartbeat Activities!
    		add_filter( 'bp_before_activity_latest_args_parse_args', array( $this, 'maybe_fool_heartbeat' ), 10, 1 );
    	}
    
    	/**
    	 * Builds an Activity Meta Query to retrieve the favorited activities
    	 *
    	 * @param  string $query_string the front end arguments for the Activity loop
    	 * @param  string $object       the Component object
    	 * @uses   wp_parse_args()
    	 * @uses   bp_displayed_user_id()
    	 * @return array()|string $query_string new arguments or same if not needed
    	 */
    	public function activity_querystring_filter( $query_string = '', $object = '' ) {
    		if ( $object != 'activity' ) {
    			return $query_string;
    		}
    
    		// You can easily manipulate the query string
    		// by transforming it into an array and merging
    		// arguments with these default ones
    		$args = wp_parse_args( $query_string, array(
    			'action'  => false,
    			'type'    => false,
    			'user_id' => false,
    			'page'    => 1
    		) );
    
    		/* most favorited */
    		if ( $args['action'] == 'activity_mostfavs' ) {
    			unset( $args['action'], $args['type'] );
    
    			// on user's profile, shows the most favorited activities for displayed user
    			if( bp_is_user() ) {
    				$args['user_id'] = bp_displayed_user_id();
    			}
    
    			// An activity meta query :)
    			$args['meta_query'] = array(
    				array(
    					/* this is the meta_key you want to filter on */
    					'key'     => 'favorite_count',
    					/* You need to get all values that are >= to 1 */
    					'value'   => 1,
    					'type'    => 'numeric',
    					'compare' => '>='
    				),
    			);
    
    			$query_string = empty( $args ) ? $query_string : $args;
            }
    
            return apply_filters( 'bp_plugin_activity_querystring_filter', $query_string, $object );
    	}
    
    	/**
    	 * Ninja Warrior trick to reorder the Activity Loop
    	 * regarding the activities favorite count
    	 *
    	 * @param  string $sql        the sql query that will be run
    	 * @param  string $select_sql the select part of the query
    	 * @param  string $from_sql   the from part of the query
    	 * @param  string $where_sql  the where part of the query
    	 * @param  string $sort       the sort order (leaving it to DESC will be helpful!)
    	 * @param  string $pag_sql    the offset part of the query
    	 * @return string $sql        the current or edited query
    	 */
    	public function order_by_most_favorited( $sql = '', $select_sql = '', $from_sql = '', $where_sql = '', $sort = '', $pag_sql = '' ) {
    		if ( apply_filters( 'bp_use_legacy_activity_query', false ) ) {
    			preg_match( '/\'favorite_count\' AND CAST\((.*) AS/', $where_sql, $match );
    
    			if ( ! empty( $match[1] ) ) {
    				$new_order_by = 'ORDER BY '. $match[1] .' + 0';
    				$new_select_sql = $select_sql . ', '. $match[1] .' AS favorite_count';
    
    				$sql = str_replace(
    					array( $select_sql, 'ORDER BY a.date_recorded' ),
    					array( $new_select_sql, $new_order_by ),
    					$sql
    				);
    			}
    
    		// $select_sql is carrying the requested argument since BuddyPress 2.0.0
    		} else {
    			$r = $select_sql;
    
    			if ( empty( $r['meta_query'] ) || ! is_array( $r['meta_query'] ) ) {
    				return $sql;
    			} else {
    				$meta_query_keys = wp_list_pluck( $r['meta_query'], 'key' );
    
    				if ( ! in_array( 'favorite_count', $meta_query_keys ) ) {
    					return $sql;
    				}
    
    				preg_match( '/\'favorite_count\' AND CAST\((.*) AS/', $sql, $match );
    
    				if ( ! empty( $match[1] ) ) {
    					$sql = str_replace( 'ORDER BY a.date_recorded', 'ORDER BY '. $match[1] .' + 0', $sql );
    				}
    			}
    		}
    
    		return $sql;
    	}
    
    	/**
    	 * Cannot pass the favorite data for now so just fool heartbeat activities
    	 */
    	public function maybe_fool_heartbeat( $r = array() ) {
    		if ( empty( $r['meta_query'] ) ) {
    			return $r;
    		}
    
    		$meta_query_keys = wp_list_pluck( $r['meta_query'], 'key' );
    
    		if ( ! in_array( 'favorite_count', $meta_query_keys ) ) {
    			return $r;
    		} else {
    			$r['since'] = '3000-12-31 00:00:00';
    		}
    
    		return $r;
    	}
    }
    
    // 1, 2, 3 go !
    function bp_loop_filters() {
    	return new BP_Loop_Filters();
    }
    
    add_action( 'bp_include', 'bp_loop_filters' );
    
    

    bikerwp000
    Participant

    @bikerwp000

    Hi danbp,

    Thanks for the link. The link is more for adding filter options for Memebers, but you included a link in that thread to a tutorial that’s more for adding filter options for Activity,

    Add custom filters to loops and enjoy them within your plugin

    ,which is what I need.

    Due to my feeble coding comprehension level, I read through that entire tutorial, the whole afternoon trying to understand and implement it to no avail. I did download, install and activate the plugin referred by the tutorial for this purpose from

    https://github.com/imath/bp-loop-filters

    and I can now see the “BP Plugin Action” in my Activity filter dropdown, both site-wide and member profile.

    What I don’t know is how to transform the “BP Plugin Action” into the categories I created for my blog posts.

    I don’t really need to change anything for Members and Groups so I can skip those. As for the “Most Favorited” filter option, if that’s readily available in the php then of course I can use it. If it takes more work, then it’s not really necessary for my site, either.

    The entire php file from this plugin for adding custom filters is below. I’m guessing in order to transform the “BP Plugin Action” into the categories I created for my blog posts, I probably need to physically write something on Line 59 – 60, where it reads,

    
    			add_action( 'bp_activity_filter_options',        array( $this, 'display_activity_actions' ) );
    			add_action( 'bp_member_activity_filter_options', array( $this, 'display_activity_actions' ) );

    and Line 132 – 150,

    
    	/**
    	 * Displays new actions into the Activity select boxes
    	 * to filter activities
    	 * - Activity Directory
    	 * - Single Group and Member activity screens
    	 *
    	 * @return string html output
    	 */
    	public function display_activity_actions() {
    		$bp_plugin_actions = $this->list_actions();
    
    		if ( empty( $bp_plugin_actions ) ) {
    			return;
    		}
    
    		foreach ( $bp_plugin_actions as $type ):?>
    			<option value="<?php echo esc_attr( $type['key'] );?>"><?php echo esc_attr( $type['value'] ); ?></option>
    		<?php endforeach;
    	}

    , is that correct? If correct, where should I go to find the exact data I need and where exactly in those lines I need to paste those data over?

    And besides those lines, are there any other spots I need to do anything about in order to complete this effort?

    I assume I need to do this from the php file because, after activating the plugin, I did not find anywhere at backend that will allow me to easily change the “BP Plugin Action” to other filter options of my choice.

    Thank you.

    I tried to post the php file here but for some reason it’s not going through. I’ll try to post it in a separate post below.


    bikerwp000
    Participant

    @bikerwp000

    Hi Paul,

    I just copy/paste my original inquiry to pastebin.com. I’m not sure how that works, but I assume just by posting it there, people will potentially respond, right?

    Anyway, this whole afternoon I’ve been testing the 3 paragraphs rule you graciously advised me and found it a true statement.

    My conclusion is I need to have
    at least 4 paragraphs separated by [Enter],
    there must be at least one blank line in between paragraphs, and
    each paragraph needs to have at least 1 character, either letter or numeral,

    when I got all those covered, and when I have both feature image and embed video in the post, the feature image will appear in the member’s Activity feed, not the video, which is what I want.

    And there can be more than one embed video.

    What wasted me a lot of time was after successfully achieving the result I want (4 paragraphs and all those other above requirements), I experimentally deleted 1 paragraph at a time to test what makes it fail and what makes it work. After all was done, when I restored it back to the way it originally worked, suddenly either the featured image wouldn’t show up like it did the first time on Activity feed or the embed video only show the URL link but not the video itself in the post.

    I kept tweaking it every possible way hoping to find out what went wrong or if the 3 paragraphs rule was in effect incorrect.

    In the end, I realized it would work again if I simply abandoned the messed-up post and started a new post by following all the above requirements.

    So that’s what you should try if something were to go wrong and you’re at a loss of what’s going on.

    I may also need to point out that all the posts in this upcoming website of mine will be contributed by frontend members via WP User Frontend plugin, which was what I used testing this 3 paragraph rule. But I think the conclusion I came up above should remain the same if you post via backend admin.

    @ Paul, thanks again for your help. Not sure if you’d care to help me out on my the other thread “How to add self-created category to member Activity filter dropdown box?”

    How to add self-created category to member Activity filter dropdown box?

    I’d really appreciate if you, or anybody, could help me out on that one.

    I had another quickie inquiry appended at the end of that thread. That one is not as important, so please don’t worry about if if you’re not interested.

    Thank you.

Viewing 7 replies - 1 through 7 (of 7 total)
Skip to toolbar