Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Magic link example upgrade #10

Merged
merged 4 commits into from
May 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
214 changes: 50 additions & 164 deletions magic-link/magic-link.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
<?php
if ( !defined( 'ABSPATH' ) ) { exit; } // Exit if accessed directly.

if ( strpos( dt_get_url_path(), 'magic_app' ) !== false ){
Disciple_Tools_Plugin_Starter_Magic_Link::instance();
}
Disciple_Tools_Plugin_Starter_Magic_Link::instance();

class Disciple_Tools_Plugin_Starter_Magic_Link extends DT_Magic_Url_Base {

class Disciple_Tools_Plugin_Starter_Magic_Link
{

public $magic = false;
public $parts = false;
public $title = 'Magic';
public $page_title = 'Magic';
public $root = "magic_app"; // @todo define the root of the url {yoursite}/root/type/key/action
public $type = 'magic_type'; // @todo define the type
public $post_type = 'contacts'; // @todo set the post type this magic link connects with.

public $post_type = 'starter_post_type'; // @todo set the post type this magic link connects with.
private $meta_key = '';

private static $_instance = null;
public static function instance() {
Expand All @@ -25,115 +23,66 @@ public static function instance() {
} // End instance()

public function __construct() {
$this->meta_key = $this->root . '_' . $this->type . '_magic_key';
parent::__construct();

// register type
$this->magic = new DT_Magic_URL( $this->root );
add_filter( 'dt_magic_url_register_types', [ $this, '_register_type' ], 10, 1 );

// register REST and REST access
add_filter( 'dt_allow_rest_access', [ $this, '_authorize_url' ], 10, 1 );
/**
* post type and module section
*/
add_action( 'dt_details_additional_section', [ $this, 'dt_details_additional_section' ], 30, 2 );
add_filter( 'dt_details_additional_tiles', [ $this, 'dt_details_additional_tiles' ], 10, 2 );
add_action( 'rest_api_init', [ $this, 'add_endpoints' ] );

/**
* Magic Url Section
*/
//don't load the magic link page for other urls
if ( !$this->check_parts_match() ){
return;
}

// fail if not valid url
$url = dt_get_url_path();
if ( strpos( $url, $this->root . '/' . $this->type ) === false ) {
return;
}

// fail to blank if not valid url
$this->parts = $this->magic->parse_url_parts();
if ( ! $this->parts ){
// @note this returns a blank page for bad url, instead of redirecting to login
add_filter( 'dt_templates_for_urls', function ( $template_for_url ) {
$url = dt_get_url_path();
$template_for_url[ $url ] = 'template-blank.php';
return $template_for_url;
}, 199, 1 );
add_filter( 'dt_blank_access', function(){ return true;
} );
add_filter( 'dt_allow_non_login_access', function(){ return true;
}, 100, 1 );
return;
}

// fail if does not match type
if ( $this->type !== $this->parts['type'] ){
return;
}

// load if valid url
add_filter( "dt_blank_title", [ $this, "_browser_tab_title" ] );
add_action( 'dt_blank_head', [ $this, '_header' ] );
add_action( 'dt_blank_footer', [ $this, '_footer' ] );
add_action( 'dt_blank_body', [ $this, 'body' ] ); // body for no post key

// load page elements
add_action( 'wp_print_scripts', [ $this, '_print_scripts' ], 1500 );
add_action( 'wp_print_styles', [ $this, '_print_styles' ], 1500 );

// register url and access
add_filter( 'dt_templates_for_urls', [ $this, '_register_url' ], 199, 1 );
add_filter( 'dt_blank_access', [ $this, '_has_access' ] );
add_filter( 'dt_allow_non_login_access', function(){ return true;
}, 100, 1 );
}

public function _register_type( array $types ) : array {
if ( ! isset( $types[$this->root] ) ) {
$types[$this->root] = [];
}
$types[$this->root][$this->type] = [
'name' => $this->title,
'root' => $this->root,
'type' => $this->type,
'meta_key' => 'public_key',
'actions' => [
'' => 'Manage',
],
'post_type' => $this->post_type,
];
return $types;
}
public function _register_url( $template_for_url ){
$parts = $this->parts;

// test 1 : correct url root and type
if ( ! $parts ){ // parts returns false
return $template_for_url;
}

// test 2 : only base url requested
if ( empty( $parts['public_key'] ) ){ // no public key present
$template_for_url[ $parts['root'] . '/'. $parts['type'] ] = 'template-blank.php';
return $template_for_url;
public function dt_details_additional_tiles( $tiles, $post_type = "" ) {
if ( $post_type === $this->post_type ){
$tiles["dt_starters_magic_url"] = [
"label" => __( "Magic Url", 'disciple_tools' ),
"description" => "The Magic URL sets up a page accessible without authentication, only the link is needed. Useful for small applications liked to this record, like quick surveys or updates."
];
}

// test 3 : no specific action requested
if ( empty( $parts['action'] ) ){ // only root public key requested
$template_for_url[ $parts['root'] . '/'. $parts['type'] . '/' . $parts['public_key'] ] = 'template-blank.php';
return $template_for_url;
}

// test 4 : valid action requested
$actions = $this->magic->list_actions( $parts['type'] );
if ( isset( $actions[ $parts['action'] ] ) ){
$template_for_url[ $parts['root'] . '/'. $parts['type'] . '/' . $parts['public_key'] . '/' . $parts['action'] ] = 'template-blank.php';
return $tiles;
}
public function dt_details_additional_section( $section, $post_type ) {
// test if campaigns post type and campaigns_app_module enabled
if ( $post_type === $this->post_type ) {
if ( 'dt_starters_magic_url' === $section ) {
$record = DT_Posts::get_post( $post_type, get_the_ID() );
if ( isset( $record[$this->meta_key] )) {
$key = $record[$this->meta_key];
} else {
$key = dt_create_unique_key();
update_post_meta( get_the_ID(), $this->meta_key, $key );
}
$link = DT_Magic_URL::get_link_url( $this->root, $this->type, $key )
?>
<p>See help <img class="dt-icon" src="<?php echo esc_html( get_template_directory_uri() . '/dt-assets/images/help.svg' ) ?>"/> for description.</p>
<a class="button" href="<?php echo esc_html( $link ); ?>" target="_blank">Open magic link</a>
<?php
}
}

return $template_for_url;
}

public function _has_access() : bool {
$parts = $this->parts;

// test 1 : correct url root and type
if ( $parts ){ // parts returns false
return true;
}

return false;
}

public function _header(){
wp_head();
Expand All @@ -143,67 +92,6 @@ public function _header(){
public function _footer(){
wp_footer();
}
public function _authorize_url( $authorized ){
if ( isset( $_SERVER['REQUEST_URI'] ) && strpos( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ), $this->root . '/v1/'.$this->type ) !== false ) {
$authorized = true;
}
return $authorized;
}

public function _print_scripts(){
// @link /disciple-tools-theme/dt-assets/functions/enqueue-scripts.php
$allowed_js = [
'jquery',
'lodash',
'moment',
'datepicker',
'site-js',
'shared-functions',
'mapbox-gl',
'mapbox-cookie',
'mapbox-search-widget',
'google-search-widget',
'jquery-cookie',
];

global $wp_scripts;

if ( isset( $wp_scripts ) ){
foreach ( $wp_scripts->queue as $key => $item ){
if ( ! in_array( $item, $allowed_js ) ){
unset( $wp_scripts->queue[$key] );
}
}
}
unset( $wp_scripts->registered['mapbox-search-widget']->extra['group'] );
}
public function _print_styles(){
// @link /disciple-tools-theme/dt-assets/functions/enqueue-scripts.php
$allowed_css = [
'foundation-css',
'jquery-ui-site-css',
'site-css',
'datepicker-css',
'mapbox-gl-css'
];

global $wp_styles;
if ( isset( $wp_styles ) ) {
foreach ($wp_styles->queue as $key => $item) {
if ( !in_array( $item, $allowed_css )) {
unset( $wp_styles->queue[$key] );
}
}
}
}


public function _browser_tab_title( $title ){
/**
* Places a title on the web browser tab.
*/
return __( "Magic", 'disciple_tools' );
}

public function header_style(){
?>
Expand Down Expand Up @@ -306,6 +194,10 @@ public function add_endpoints() {
[
'methods' => "POST",
'callback' => [ $this, 'endpoint' ],
'permission_callback' => function( WP_REST_Request $request ){
$magic = new DT_Magic_URL( $this->root );
return $magic->verify_rest_endpoint_permissions_on_post( $request );
},
],
]
);
Expand All @@ -320,13 +212,7 @@ public function endpoint( WP_REST_Request $request ) {

$params = dt_recursive_sanitize_array( $params );
$action = sanitize_text_field( wp_unslash( $params['action'] ) );

// if parsing public key
// $magic = $this->magic;
// $post_id = $magic->get_post_id( $params['parts']['meta_key'], $params['parts']['public_key'] );
// if ( ! $post_id ){
// return new WP_Error( __METHOD__, "Missing post record", [ 'status' => 400 ] );
// }
$post_id = $params["parts"]["post_id"];

switch ( $action ) {
case 'get':
Expand Down
2 changes: 1 addition & 1 deletion post-type/module-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -781,8 +781,8 @@ public static function dt_filter_access_permissions( $permissions, $post_type ){
// scripts
public function scripts(){
if ( is_singular( $this->post_type ) && get_the_ID() && DT_Posts::can_view( $this->post_type, get_the_ID() ) ){
$test = "";
// @todo add enqueue scripts
dt_write_log( __METHOD__ );
}
}
}
Expand Down