-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from cmanon/feature/activiteslist-block
Feature/activiteslist block
- Loading branch information
Showing
12 changed files
with
194 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<?php | ||
/** | ||
* Activities List. | ||
* Activities List Renderer. | ||
* @package WPStrava | ||
*/ | ||
|
||
|
@@ -10,9 +10,18 @@ | |
* @author Justin Foell <[email protected]> | ||
* @since 2.3.0 | ||
*/ | ||
class WPStrava_ActivitiesList { | ||
public static function get_activities_html( $args ) { | ||
if ( isset( $args['athlete_token'] ) ) { | ||
class WPStrava_ActivitiesListRenderer { | ||
|
||
/** | ||
* Get the HTML for an Activities List. | ||
* | ||
* @param array $atts | ||
* @return string HTML for an route. | ||
* @author Justin Foell <[email protected]> | ||
* @since 2.8.0 | ||
*/ | ||
public function get_html( $atts ) { | ||
if ( isset( $atts['athlete_token'] ) ) { | ||
// Translators: Message shown when using deprecated athlete_token parameter. | ||
return __( 'The <code>athlete_token</code> parameter is deprecated as of WP-Strava version 2 and should be replaced with <code>client_id</code>.', 'wp-strava' ); | ||
} | ||
|
@@ -26,14 +35,14 @@ public static function get_activities_html( $args ) { | |
'date_end' => '', | ||
); | ||
|
||
$args = wp_parse_args( $args, $defaults ); | ||
$atts = wp_parse_args( $atts, $defaults ); | ||
|
||
$som = WPStrava_SOM::get_som( $args['som'] ); | ||
$som = WPStrava_SOM::get_som( $atts['som'] ); | ||
$strava_activity = WPStrava::get_instance()->activity; | ||
$activities = array(); | ||
|
||
try { | ||
$activities = $strava_activity->get_activities( $args ); | ||
$activities = $strava_activity->get_activities( $atts ); | ||
} catch ( WPStrava_Exception $e ) { | ||
return $e->to_html(); | ||
} | ||
|
@@ -51,11 +60,11 @@ public static function get_activities_html( $args ) { | |
// Translators: Shows something like "On <date> <[went 10 miles] [during 2 hours] [climbing 100 feet]>." | ||
__( 'On %1$s %2$s', 'wp-strava' ), | ||
date_i18n( get_option( 'date_format' ), $unixtime ), | ||
self::get_activity_time( $unixtime ) | ||
$this->get_activity_time( $unixtime ) | ||
); | ||
} | ||
|
||
if ( is_numeric( $args['strava_club_id'] ) ) { | ||
if ( is_numeric( $atts['strava_club_id'] ) ) { | ||
$name = $activity->athlete->firstname . ' ' . $activity->athlete->lastname; | ||
$response .= empty( $activity->athlete->id ) ? | ||
" {$name}" : | ||
|
@@ -86,7 +95,7 @@ public static function get_activities_html( $args ) { | |
* @author Justin Foell <[email protected]> | ||
* @since 1.7.1 | ||
*/ | ||
public static function get_activity_time( $unixtime ) { | ||
private function get_activity_time( $unixtime ) { | ||
if ( WPStrava::get_instance()->settings->hide_time ) { | ||
return ''; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
/* | ||
* ActivitiesList block. | ||
*/ | ||
|
||
class WPStrava_Blocks_ActivitiesList implements WPStrava_Blocks_Interface { | ||
|
||
/** | ||
* Whether or not to enqueue styles (if shortcode is present). | ||
* | ||
* @var boolean | ||
* @author Justin Foell <[email protected]> | ||
* @since 2.5.0 | ||
*/ | ||
private $add_script = false; | ||
|
||
/** | ||
* Register the wp-strava/activitieslist block. | ||
* | ||
* @author Justin Foell <[email protected]> | ||
* @since 2.2.0 | ||
*/ | ||
public function register_block() { | ||
register_block_type( | ||
'wp-strava/activitieslist', | ||
array( | ||
'style' => 'wp-strava-block', | ||
'editor_style' => 'wp-strava-block-editor', | ||
'editor_script' => 'wp-strava-block', | ||
'render_callback' => array( $this, 'render_block' ), | ||
'attributes' => array( | ||
'som' => array( | ||
'type' => 'string', | ||
'default' => null, | ||
), | ||
), | ||
) | ||
); | ||
add_action( 'wp_footer', array( $this, 'print_scripts' ) ); | ||
} | ||
|
||
/** | ||
* Render for this block. | ||
* | ||
* @param array $attributes JSON attributes saved in the HTML comment for this block. | ||
* @param string $content The content from JS save() for this block. | ||
* @return string HTML for this block. | ||
* @author Justin Foell <[email protected]> | ||
* @since 2.2.0 | ||
*/ | ||
public function render_block( $attributes, $content ) { | ||
$this->add_script = true; | ||
|
||
// Transform from block attributes to shortcode standard. | ||
$attributes = array( | ||
'som' => ! empty( $attributes['som'] ) ? $attributes['som'] : null, | ||
); | ||
|
||
$renderer = new WPStrava_ActivitiesListRenderer(); | ||
return $renderer->get_html( $attributes ); | ||
} | ||
|
||
/** | ||
* Enqueue style if block is being used. | ||
* | ||
* @author Justin Foell <[email protected]> | ||
* @since 2.5.0 | ||
*/ | ||
public function print_scripts() { | ||
if ( $this->add_script ) { | ||
wp_enqueue_style( 'wp-strava-style' ); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "wp-strava/activitieslist", | ||
"title": "Strava Activities List", | ||
"category": "embed", | ||
"icon": "editor-ul", | ||
"description": "List of Strava Activities", | ||
"keywords": [ "activity", "ride" ], | ||
"textdomain": "wp-strava", | ||
"attributes": { | ||
"som": { | ||
"type": "string", | ||
"default": null | ||
} | ||
}, | ||
"editorScript": "file:../../../build/index.js", | ||
"editorStyle": "file:../../../build/editor.css", | ||
"style": "file:../../../build/style.css" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* global wp, wpStrava */ | ||
import SOMOverride from '../components/som-override'; | ||
|
||
const { __ } = wp.i18n; | ||
const { Component } = wp.element; | ||
const { InspectorControls } = wp.editor; | ||
const { PanelBody, ToggleControl, ServerSideRender } = wp.components; | ||
|
||
class Edit extends Component { | ||
|
||
constructor() { | ||
super( ...arguments ); | ||
this.overrideSOM = this.overrideSOM.bind( this ); | ||
|
||
this.state = { | ||
som: this.props.attributes.som, | ||
}; | ||
} | ||
|
||
overrideSOM( newSOM ) { | ||
this.setState( { som: newSOM } ); | ||
this.props.setAttributes( { som: newSOM } ); | ||
} | ||
|
||
render() { | ||
const { | ||
som | ||
} = this.state; | ||
|
||
return ( | ||
<> | ||
<ServerSideRender | ||
block="wp-strava/activitieslist" | ||
attributes={ { | ||
som: som, | ||
} } | ||
/> | ||
<InspectorControls> | ||
<PanelBody | ||
title={ __( 'Display Options', 'wp-strava' ) } | ||
> | ||
<SOMOverride | ||
onChange={ this.overrideSOM } | ||
/> | ||
</PanelBody> | ||
</InspectorControls> | ||
</> | ||
); | ||
} | ||
} | ||
|
||
export default Edit; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* global wp, wpStrava */ | ||
import { registerBlockType } from '@wordpress/blocks'; | ||
import edit from './edit'; | ||
import metadata from './block.json'; | ||
|
||
metadata.edit = edit; | ||
metadata.save = () => null; | ||
|
||
registerBlockType( metadata.name, metadata ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
import './blocks/activity'; | ||
import './blocks/route'; | ||
import './blocks/activitieslist'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters