-
Notifications
You must be signed in to change notification settings - Fork 198
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 #4155 from Automattic/add/view-results-button
Add View Results button
- Loading branch information
Showing
4 changed files
with
99 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { BlockStyles, createButtonBlockType } from '../button'; | ||
import ToggleLegacyCourseMetaboxesWrapper from '../toggle-legacy-course-metaboxes-wrapper'; | ||
|
||
/** | ||
* View results button block. | ||
*/ | ||
export default createButtonBlockType( { | ||
tagName: 'a', | ||
EditWrapper: ToggleLegacyCourseMetaboxesWrapper, | ||
settings: { | ||
name: 'sensei-lms/button-view-results', | ||
description: __( | ||
'Allow an enrolled user to navigate to the course results page. The block is only displayed if the user is enrolled to the course.', | ||
'sensei-lms' | ||
), | ||
title: __( 'View Results', 'sensei-lms' ), | ||
attributes: { | ||
text: { | ||
default: __( 'View Results', 'sensei-lms' ), | ||
}, | ||
}, | ||
styles: [ | ||
BlockStyles.Fill, | ||
{ ...BlockStyles.Outline, isDefault: true }, | ||
BlockStyles.Link, | ||
], | ||
}, | ||
} ); |
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,60 @@ | ||
<?php | ||
/** | ||
* File containing the Sensei_Block_View_Results class. | ||
* | ||
* @package sensei | ||
*/ | ||
|
||
if ( ! defined( 'ABSPATH' ) ) { | ||
exit; | ||
} | ||
|
||
/** | ||
* Block for View Results button. | ||
*/ | ||
class Sensei_Block_View_Results { | ||
|
||
/** | ||
* Sensei_Block_View_Results constructor. | ||
*/ | ||
public function __construct() { | ||
$this->register_block(); | ||
} | ||
|
||
/** | ||
* Register View Results button block. | ||
* | ||
* @access private | ||
*/ | ||
public function register_block() { | ||
Sensei_Blocks::register_sensei_block( | ||
'sensei-lms/button-view-results', | ||
[ | ||
'render_callback' => [ $this, 'render' ], | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* Render the View Results button. | ||
* | ||
* @param array $attributes Block attributes. | ||
* @param string $content Block HTML. | ||
* | ||
* @return string The HTML of the block. | ||
*/ | ||
public function render( $attributes, $content ): string { | ||
if ( ! Sensei()->course::is_user_enrolled( get_the_ID() ) ) { | ||
return ''; | ||
} | ||
|
||
$results_link = esc_url( Sensei()->course_results->get_permalink( get_the_ID() ) ); | ||
|
||
return preg_replace( | ||
'/<a(.*)>/', | ||
'<a href="' . $results_link . '" $1>', | ||
$content, | ||
1 | ||
); | ||
} | ||
} |
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