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

Enhancement/add atts to pmpro levels block #2977

Draft
wants to merge 3 commits into
base: dev
Choose a base branch
from
Draft
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
2 changes: 0 additions & 2 deletions blocks/src/levels-page/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
"category": "pmpro",
"description": "Dynamic page section that displays a list of membership levels and pricing, linked to membership checkout. To reorder the display, navigate to Memberships > Settings > Levels.",
"keywords": [ "level", "price", "pricing table", "paid memberships pro", "pmpro" ],
"attributes" : {
},
"supports": {
"align": [ "wide", "full" ],
"ariaLabel": true,
Expand Down
29 changes: 25 additions & 4 deletions blocks/src/levels-page/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import { __ } from '@wordpress/i18n';
/**
* WordPress dependencies
*/
import { useBlockProps } from '@wordpress/block-editor';
import { useBlockProps, InspectorControls } from '@wordpress/block-editor';
import { PanelBody } from '@wordpress/components';

import Levels from './levels';
import Groups from './groups';


/**
* Render the Membership Levels and Pricing Table block in the editor.
Expand All @@ -17,13 +22,29 @@ import { useBlockProps } from '@wordpress/block-editor';
*
* @return {WPElement} Element to render.
*/
export default function Edit() {
export default function Edit(props) {
const blockProps = useBlockProps( {} );

return [
<div className="pmpro-block-element" { ...blockProps }>
<span className="pmpro-block-title">{ __( 'Paid Memberships Pro', 'paid-memberships-pro' ) }</span>
<span className="pmpro-block-subtitle">{ __( 'Membership Levels List', 'paid-memberships-pro' ) }</span>
</div>
</div>,
<InspectorControls>
<PanelBody
title={ __( 'Membership Groups', 'paid-memberships-pro' ) }
initialOpen={ true }
>
{ Groups( props ) }
</PanelBody>

<PanelBody
title={ __( 'Membership Levels', 'paid-memberships-pro' ) }
initialOpen={ true }
>
{ Levels( props ) }
</PanelBody>
</InspectorControls>

];
}
41 changes: 41 additions & 0 deletions blocks/src/levels-page/groups.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Retrieves the translation of text.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-i18n/
*/
import { __ } from '@wordpress/i18n';


/**
* WordPress dependencies
*/
import { CheckboxControl } from '@wordpress/components';

export default function groups ( props ) {
const { attributes: { groups }, setAttributes } = props;

// Build an array of Groups checkboxes.
const checkboxes = pmpro.all_groups.map(( group ) => {
function setGroupsAttribute( nowChecked ) {
if ( nowChecked ) {
// Add the group.
const newGroups = groups.slice();
newGroups.push( group );
setAttributes({ groups: newGroups });
} else {
// Remove the group.
const newGroups = groups.filter( item => item.id !== group.id );
setAttributes({ groups: newGroups });
}
}
return [
<CheckboxControl
label={ group.name }
checked={ groups.some( item => item.id === group.id ) }
onChange={ setGroupsAttribute }
/>
];
});

return checkboxes;
}
46 changes: 46 additions & 0 deletions blocks/src/levels-page/levels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Retrieves the translation of text.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-i18n/
*/
import { __ } from '@wordpress/i18n';


/**
* WordPress dependencies
*/
import { CheckboxControl} from '@wordpress/components';

//Build a checkbox for each level
export default function levels ( props ) {

const { attributes: { groups, levels }, setAttributes } = props;


// Build an array of checkboxes for each level.
const checkboxes = pmpro.all_level_values_and_labels.map(( level ) => {
function setLevelsAttribute( nowChecked ) {
if ( nowChecked ) {
// Add the level.
const newLevels = levels.slice();
newLevels.push( level );
setAttributes({ levels: newLevels });
} else {
// Remove the level.
const newLevels = levels.filter( item => item.value !== level.value );
setAttributes({ levels: newLevels });
}
}
return [
<CheckboxControl
label={ level.label }
checked={ levels.some( item => item.value == level.value ) && groups.some( item => item.id == level.group_id )}
onChange={ setLevelsAttribute }
disabled={ ! ( groups.some( item => item.id == level.group_id ) ) }
/>
];
});

return checkboxes;

}
19 changes: 15 additions & 4 deletions blocks/src/levels-page/render.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
<?php
/**
* Render the Membership Levels and Pricing Table block on the frontend.
*/
$output = pmpro_loadTemplate( 'levels', 'local', 'pages' );
foreach ( $attributes['groups'] as $group ) {
$group_ids[] = intval( $group['id'] );
}
foreach ( $attributes['levels'] as $level ) {
$level_ids[] = intval( $level['value'] );
}

$atts = array(
'groups' => implode( ',', $group_ids ),
'levels' => implode( ',', $level_ids ),
);


$output = pmpro_loadTemplate( 'levels', 'local', 'pages', 'php', $atts );

?>
<div <?php echo get_block_wrapper_attributes(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>>
<?php echo $output ; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
Expand Down
28 changes: 27 additions & 1 deletion includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ function pmpro_block_categories( $categories ) {
*/
function pmpro_register_block_types() {
if ( function_exists( 'register_block_type' ) ) {

//Get all groups and levels to add as a default attribute to the levels-page block.
$all_groups = pmpro_get_level_groups_in_order();
$to_return = array();
foreach( $all_groups as $group ) {
$group->levels = pmpro_get_level_ids_for_group( $group->id );
$to_return[] = array( 'id' => $group->id, 'name' => $group->name, 'levels' => $group->levels );
}

$all_levels = pmpro_getAllLevels( true, true );
$all_level_values_and_labels = array();
foreach( $all_levels as $level ) {
$all_level_values_and_labels[] = array( 'value' => $level->id, 'label' => $level->name, 'group_id' => pmpro_get_group_id_for_level( $level->id ) );
}

register_block_type( PMPRO_DIR . '/blocks/build/account-invoices-section' );
register_block_type( PMPRO_DIR . '/blocks/build/account-profile-section' );
register_block_type( PMPRO_DIR . '/blocks/build/account-links-section' );
Expand All @@ -40,7 +55,18 @@ function pmpro_register_block_types() {
register_block_type( PMPRO_DIR . '/blocks/build/checkout-page' );
register_block_type( PMPRO_DIR . '/blocks/build/confirmation-page' );
register_block_type( PMPRO_DIR . '/blocks/build/invoice-page' );
register_block_type( PMPRO_DIR . '/blocks/build/levels-page' );
register_block_type( PMPRO_DIR . '/blocks/build/levels-page' , array(
'attributes' => array(
'levels' => array(
'type' => 'array',
'default' => $all_level_values_and_labels,
),
'groups' => array(
'type' => 'array',
'default' => $to_return,
),
),
) );
register_block_type( PMPRO_DIR . '/blocks/build/login' );
register_block_type( PMPRO_DIR . '/blocks/build/member-profile-edit' );
register_block_type( PMPRO_DIR . '/blocks/build/membership' );
Expand Down
4 changes: 2 additions & 2 deletions includes/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function pmpro_wp()
//run the appropriate preheader function
foreach($pmpro_core_pages as $pmpro_page_name => $pmpro_page_id)
{
if( ! empty( $post->post_content ) && ( strpos( $post->post_content, "[pmpro_" . $pmpro_page_name . "]" ) !== false || ( function_exists( 'has_block' ) && has_block( 'pmpro/' . $pmpro_page_name . '-page', $post ) ) ) )
if( ! empty( $post->post_content ) && ( strpos( $post->post_content, "[pmpro_" . $pmpro_page_name ) !== false || ( function_exists( 'has_block' ) && has_block( 'pmpro/' . $pmpro_page_name . '-page', $post ) ) ) )
{
//preheader
require_once(PMPRO_DIR . "/preheaders/" . $pmpro_page_name . ".php");
Expand All @@ -82,7 +82,7 @@ function pmpro_wp()
function pmpro_pages_shortcode($atts, $content=null, $code="")
{
global $pmpro_page_name;
$temp_content = pmpro_loadTemplate($pmpro_page_name, 'local', 'pages');
$temp_content = pmpro_loadTemplate( $pmpro_page_name, 'local', 'pages', 'php', $atts );
return apply_filters("pmpro_pages_shortcode_" . $pmpro_page_name, $temp_content);
}
add_shortcode("pmpro_" . $pmpro_page_name, "pmpro_pages_shortcode");
Expand Down
4 changes: 3 additions & 1 deletion includes/page-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@ function pmpro_get_template_path_to_load( $page_name = null, $where = 'local', $
* @param string $where - `local` or `url` (whether to load from FS or over http)
* @param string $type - Type of template (valid: 'email' or 'pages', 'adminpages', 'preheader')
* @param string $ext - File extension ('php', 'html', 'htm', etc)
* @param array $atts - Array of attributes to pass to the template
* @return string - The HTML for the template.
*
* TODO - Allow localized template files to be loaded?
*
* @since 1.8.9
* @since TBD Added $atts parameter to pass attributes to the template.
*/
function pmpro_loadTemplate( $page_name = null, $where = 'local', $type = 'pages', $ext = 'php' ) {
function pmpro_loadTemplate( $page_name = null, $where = 'local', $type = 'pages', $ext = 'php', $atts = array() ) {
// Get the path of the template to load.
$path = pmpro_get_template_path_to_load( $page_name, $where, $type, $ext );

Expand Down
43 changes: 25 additions & 18 deletions includes/scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
function pmpro_enqueue_scripts() {
global $pmpro_level, $pmpro_pages;

// Figure out which frontend.css file to load.
if( file_exists( get_stylesheet_directory() . "/paid-memberships-pro/css/frontend.css" ) ) {
$frontend_css = get_stylesheet_directory_uri() . "/paid-memberships-pro/css/frontend.css";
Expand All @@ -14,7 +14,7 @@ function pmpro_enqueue_scripts() {
$frontend_css = plugins_url( 'css/frontend.css',dirname(__FILE__) );
}
wp_enqueue_style( 'pmpro_frontend', $frontend_css, array(), PMPRO_VERSION, "screen" );

// Figure out which frontend-rlt.css file to load if applicable.
if( is_rtl() ) {
if( file_exists( get_stylesheet_directory() . "/paid-memberships-pro/css/frontend-rtl.css" ) ) {
Expand All @@ -35,7 +35,7 @@ function pmpro_enqueue_scripts() {
else
$print_css = plugins_url('css/print.css',dirname(__FILE__) );
wp_enqueue_style('pmpro_print', $print_css, array(), PMPRO_VERSION, "print");

// Checkout page JS
if ( pmpro_is_checkout() ) {
wp_register_script( 'pmpro_checkout',
Expand All @@ -52,7 +52,7 @@ function pmpro_enqueue_scripts() {
));
wp_enqueue_script( 'pmpro_checkout' );
}

// Change Password page JS
$is_change_pass_page = ! empty( $pmpro_pages['member_profile_edit'] )
&& is_page( $pmpro_pages['member_profile_edit'] )
Expand Down Expand Up @@ -120,10 +120,16 @@ function pmpro_admin_enqueue_scripts() {

wp_enqueue_script( 'pmpro_confetti' );
}

$all_levels = pmpro_getAllLevels( true, true );
$all_level_values_and_labels = array();
$all_levels_formatted_text = array();
$all_groups = pmpro_get_level_groups_in_order();

//Iterate over groups and add levels
foreach( $all_groups as $group ) {
$group->levels = pmpro_get_level_ids_for_group( $group->id );
}

// Enqueue pmpro-admin.js.
wp_register_script( 'pmpro_admin',
Expand All @@ -133,7 +139,7 @@ function pmpro_admin_enqueue_scripts() {
$all_levels = pmpro_getAllLevels( true, true );
$all_level_values_and_labels = array();
foreach( $all_levels as $level ) {
$all_level_values_and_labels[] = array( 'value' => $level->id, 'label' => $level->name );
$all_level_values_and_labels[] = array( 'value' => $level->id, 'label' => $level->name, 'group_id' => pmpro_get_group_id_for_level( $level->id ) );
$level->formatted_price = trim( pmpro_no_quotes( pmpro_getLevelCost( $level, true, true ) ) );
$level->formatted_expiration = trim( pmpro_no_quotes( pmpro_getLevelExpiration( $level ) ) );
$all_levels_formatted_text[$level->id] = $level;
Expand All @@ -147,16 +153,17 @@ function pmpro_admin_enqueue_scripts() {
pmpro_get_field_html();
$empty_field_html = ob_get_clean();

wp_localize_script( 'pmpro_admin', 'pmpro', array(
'all_levels' => $all_levels,
'all_levels_formatted_text' => $all_levels_formatted_text,
'all_level_values_and_labels' => $all_level_values_and_labels,
'checkout_url' => pmpro_url( 'checkout' ),
'user_fields_blank_group' => $empty_field_group_html,
'user_fields_blank_field' => $empty_field_html,
// We want the core WP translation so we can check for it in JS.
'plugin_updated_successfully_text' => __( 'Plugin updated successfully.' ),
));
wp_localize_script( 'pmpro_admin', 'pmpro', array(
'all_levels' => $all_levels,
'all_levels_formatted_text' => $all_levels_formatted_text,
'all_level_values_and_labels' => $all_level_values_and_labels,
'all_groups' => $all_groups,
'checkout_url' => pmpro_url( 'checkout' ),
'user_fields_blank_group' => $empty_field_group_html,
'user_fields_blank_field' => $empty_field_html,
// We want the core WP translation so we can check for it in JS.
'plugin_updated_successfully_text' => __( 'Plugin updated successfully.' ),
));
wp_enqueue_script( 'pmpro_admin' );

// Enqueue styles.
Expand All @@ -168,15 +175,15 @@ function pmpro_admin_enqueue_scripts() {
} else {
$admin_css = plugins_url( 'css/admin.css', __DIR__ );
}

// Figure out which admin-rtl.css to load if applicable.
if ( file_exists( get_stylesheet_directory() . '/paid-memberships-pro/css/admin-rtl.css' ) ) {
$admin_css_rtl = get_stylesheet_directory_uri() . '/paid-memberships-pro/css/admin-rtl.css';
} elseif( file_exists( get_template_directory() . '/paid-memberships-pro/css/admin-rtl.css' ) ) {
$admin_css_rtl = get_template_directory_uri() . '/paid-memberships-pro/css/admin-rtl.css';
} else {
$admin_css_rtl = plugins_url( 'css/admin-rtl.css', __DIR__ );
}
}

wp_register_style( 'pmpro_admin', $admin_css, [], PMPRO_VERSION, 'screen' );
wp_register_style( 'pmpro_admin_rtl', $admin_css_rtl, [], PMPRO_VERSION, 'screen' );
Expand Down
Loading