stream icon indicating copy to clipboard operation
stream copied to clipboard

Add Stream REST API endpoint

Open lukecarbis opened this issue 8 years ago • 10 comments

And use it internally, e.g. Live updates.

lukecarbis avatar Mar 08 '16 02:03 lukecarbis

See also: Stream's WP CLI support.

lukecarbis avatar Mar 08 '16 02:03 lukecarbis

Any chance we can get this idea moving? Here is some quick demo code if it helps.

/**
 * Imforza Rest Routes initialization class.
 */
class Stream_REST {

	/**
	 * Create the rest API routes.
	 *
	 * @access public
	 * @return void
	 */
	public function __construct() {
		
		add_action( 'rest_api_init', function(){
			register_rest_route( 'stream/v1', 'settings', array(
				'methods'  => array('get', 'post', 'put', 'patch'),
				'callback' => array( $this, 'settings' ),
				// 'permission_callback' => array( $this, 'permission_check' ),
				'args'     => array(
					'keep_records' => array(
						'required'    => false,
						'default'     => '', 
						'description' => 'Keep Records Indefinitely. Not recommended.',
						'type'        => 'bool',
					),
					'keep_records_for' => array(
						'required'    => false,
						'default'     => '30', 
						'description' => 'Maximum number of days to keep activity records.',
						'type'        => 'int',
					),
					'role_access' => array(
						'required'    => false,
						'default'     => '', 
						'description' => 'Users from the selected roles above will have permission to view Stream Records.',
						'type'        => 'string',
					),
					// Advanced Settings.
					'akismet_tracking' => array(
						'required'    => false,
						'default'     => '', 
						'description' => '',
						'type'        => 'bool',
					),
					'comment_flood_tracking' => array(
						'required'    => false,
						'default'     => '', 
						'description' => '',
						'type'        => 'bool',
					),
					'wpcron_tracking' => array(
						'required'    => false,
						'default'     => '', 
						'description' => '',
						'type'        => 'bool',
					),
				),
			));
		});
		
		add_action( 'rest_api_init', function(){
			register_rest_route( 'stream/v1', 'stream', array(
				'methods'  => 'get',
				'callback' => array( $this, 'get_stream' ),
				// 'permission_callback' => array( $this, 'permission_check' ),
			));
		});
		
		add_action( 'rest_api_init', function(){
			register_rest_route( 'stream/v1', 'resetdb', array(
				'methods'  => 'delete',
				'callback' => array( $this, 'reset_stream_db' ),
				// 'permission_callback' => array( $this, 'permission_check' ),
			));
		});
		
		add_action( 'rest_api_init', function(){
			register_rest_route( 'stream/v1', 'excludes', array(
				'methods'  => array('get', 'post', 'delete'),
				'callback' => array( $this, 'excludes' ),
				// 'permission_callback' => array( $this, 'permission_check' ),
			));
		});
				
	}

	
	/**
	 * get_stream function.
	 * 
	 * @access public
	 * @return void
	 */
	public function get_stream() {
		
		$stream_json = '';
		
		return rest_ensure_response( $stream_json );
	
	}

	/**
	 * Check whether the function is allowed to be run.
	 *
	 * Must have either capabilities to enact action, or a valid nonce.
	 *
	 * @param  [type] $data [description]
	 * @return [type]       [description]
	 */
	public function permission_check( $data ) {
		if ( ! current_user_can( 'manage_options' ) ) {
			return new WP_Error( 'forbidden', 'You are not allowed to do that.', array( 'status' => 403 ) );
		}
		return true;
	}
	
}	
new Stream_REST();

bhubbard avatar Feb 15 '18 19:02 bhubbard

+1 for this.

mrpritchett avatar May 04 '18 12:05 mrpritchett

Something to bear in mind is that the REST API will support multisite shortly, and I'd love to see this functionality in Stream also support that. Definitely a major use case for us.

mrpritchett avatar May 04 '18 12:05 mrpritchett

I'm all for this as well. Will see how we can get some traction on it.

DavidCramer avatar May 04 '18 12:05 DavidCramer

+💯 for this 😀

colorful-tones avatar Jun 13 '18 13:06 colorful-tones

+💯 for sure; for this to also work with multisite would be super great

wp-networks avatar Jun 14 '18 17:06 wp-networks

will the REST API endpoint ever find it's way into the plugin? would really appreciate it!

resqonline avatar Dec 10 '18 13:12 resqonline

As far as I can tell over the past months/year, work on this plugin has ground to a screeching halt.

Not sure what it would take for someone to pick up interest again. Is there something better out there that people are already gravitating to?

lkraav avatar Dec 10 '18 16:12 lkraav

@kasparsd while I do agree that adding REST API endpoints has true merit, I feel it is a much larger lift than just adding endpoints. With the ability to build with React, it would be much more beneficial to build out a new version of Stream, which exposes endpoints, and rebuild the UI in React.

In short, add REST API endpoints, but as a bigger picture project with the idea of reworking the UI of the plugin as a whole.

kopepasah avatar Jul 07 '20 16:07 kopepasah