From a08ed9b6c1510ebbfadc51e9818d472ac5e7e145 Mon Sep 17 00:00:00 2001 From: ashubawork Date: Wed, 29 Mar 2023 14:38:11 +0300 Subject: [PATCH] - reset password block --- .../assets/js/um-admin-blocks-shortcode.js | 172 +----------------- includes/blocks/um-forms/block.json | 15 ++ includes/blocks/um-forms/src/index.js | 105 +++++++++++ includes/blocks/um-password-reset/block.json | 10 + .../blocks/um-password-reset/src/index.js | 19 ++ includes/core/class-blocks.php | 31 ++++ includes/core/class-shortcodes.php | 32 ++-- package.json | 4 +- 8 files changed, 200 insertions(+), 188 deletions(-) create mode 100644 includes/blocks/um-forms/block.json create mode 100644 includes/blocks/um-forms/src/index.js create mode 100644 includes/blocks/um-password-reset/block.json create mode 100644 includes/blocks/um-password-reset/src/index.js diff --git a/includes/admin/assets/js/um-admin-blocks-shortcode.js b/includes/admin/assets/js/um-admin-blocks-shortcode.js index 88038cf3..0ad57c4b 100644 --- a/includes/admin/assets/js/um-admin-blocks-shortcode.js +++ b/includes/admin/assets/js/um-admin-blocks-shortcode.js @@ -128,176 +128,6 @@ wp.blocks.registerBlockType( 'um-block/um-forms', { }); -//-------------------------------------\\ -//-- Um Member Directories shortcode --\\ -//-------------------------------------\\ - -wp.blocks.registerBlockType( 'um-block/um-member-directories', { - title: wp.i18n.__( 'Member Directory', 'ultimate-member' ), - description: wp.i18n.__( 'Choose display directory', 'ultimate-member' ), - icon: 'groups', - category: 'um-blocks', - attributes: { - content: { - source: 'html', - selector: 'p' - }, - member_id: { - type: 'select' - } - }, - - edit: wp.data.withSelect( function( select ) { - return { - posts: select( 'core' ).getEntityRecords( 'postType', 'um_directory', { - per_page: -1 - }) - }; - } )( function( props ) { - var posts = props.posts, - className = props.className, - attributes = props.attributes, - setAttributes = props.setAttributes, - member_id = props.attributes.member_id, - content = props.attributes.content; - - function get_option( posts ) { - var option = []; - - posts.map( function( post ) { - option.push( - { - label: post.title.rendered, - value: post.id - } - ); - }); - - return option; - } - - function umShortcode( value ) { - - var shortcode = ''; - - if ( value !== undefined ) { - shortcode = '[ultimatemember form_id="' + value + '"]'; - } - - return shortcode; - } - - if ( ! posts ) { - return wp.element.createElement( - 'p', - { - className: className - }, - wp.element.createElement( - wp.components.Spinner, - null - ), - wp.i18n.__( 'Loading Forms', 'ultimate-member' ) - ); - } - - if ( 0 === posts.length ) { - return wp.element.createElement( - 'p', - null, - wp.i18n.__( 'No Posts', 'ultimate-member' ) - ); - } - - if ( member_id === undefined ) { - props.setAttributes({ member_id: posts[0]['id'] }); - var shortcode = umShortcode(posts[0]['id']); - props.setAttributes( { content: shortcode } ); - } - - var get_post = get_option( posts ); - - return wp.element.createElement( - 'div', - { - className: className - }, - wp.element.createElement( - wp.components.SelectControl, - { - label: wp.i18n.__( 'Select Directories', 'ultimate-member' ), - className: 'um_select_directory', - type: 'number', - value: member_id, - options: get_post, - onChange: function onChange( value ) { - props.setAttributes({ member_id: value }); - var shortcode = umShortcode(value); - props.setAttributes( { content: shortcode } ); - } - } - ) - ); - } // end withSelect - ), // end edit - - save: function save( props ) { - - return wp.element.createElement( - wp.editor.RichText.Content, - { - tagName: 'p', - className: props.className, - value: props.attributes.content - } - ); - } - -}); - -//-------------------------------------\\ -//--------- Um password reset ---------\\ -//-------------------------------------\\ -wp.blocks.registerBlockType( 'um-block/um-password-reset', { - title: wp.i18n.__( 'Password Reset', 'ultimate-member' ), - description: wp.i18n.__( 'Displaying the password reset form', 'ultimate-member' ), - icon: 'unlock', - category: 'um-blocks', - attributes: { - content: { - source: 'html', - selector: 'p' - } - }, - - edit: function( props ) { - var content = props.attributes.content; - props.setAttributes({ content: '[ultimatemember_password]' }); - - return [ - wp.element.createElement( - "div", - { - className: "um-password-reset-wrapper" - }, - wp.i18n.__( 'Password Reset', 'ultimate-member' ) - ) - ] - }, - - save: function( props ) { - - return wp.element.createElement( - wp.editor.RichText.Content, - { - tagName: 'p', - className: props.className, - value: props.attributes.content - } - ); - } -}); - //-------------------------------------\\ //------------ Um Account -------------\\ //-------------------------------------\\ @@ -402,4 +232,4 @@ wp.blocks.registerBlockType( 'um-block/um-account', { } ); } -}); \ No newline at end of file +}); diff --git a/includes/blocks/um-forms/block.json b/includes/blocks/um-forms/block.json new file mode 100644 index 00000000..563f7399 --- /dev/null +++ b/includes/blocks/um-forms/block.json @@ -0,0 +1,15 @@ +{ + "apiVersion": 2, + "name": "um-block/um-forms", + "title": "Form", + "description": "Choose display form", + "icon": "forms", + "category": "um-blocks", + "attributes": { + "form_id": { + "type": "integer" + } + }, + "editorScript": "file:./build/index.js", + "textdomain": "ultimate-member" +} diff --git a/includes/blocks/um-forms/src/index.js b/includes/blocks/um-forms/src/index.js new file mode 100644 index 00000000..227d98ea --- /dev/null +++ b/includes/blocks/um-forms/src/index.js @@ -0,0 +1,105 @@ +import { useSelect } from '@wordpress/data'; +import { PanelBody, SelectControl, Spinner } from '@wordpress/components'; +import { InspectorControls, useBlockProps } from '@wordpress/block-editor'; +import ServerSideRender from '@wordpress/server-side-render'; +import { registerBlockType } from "@wordpress/blocks"; + +registerBlockType('um-block/um-forms', { + edit: function (props) { + let { form_id, setAttributes } = props.attributes; + const blockProps = useBlockProps(); + const posts = useSelect((select) => { + return select('core').getEntityRecords('postType', 'um_form', { + per_page: -1, + _fields: ['id', 'title'] + }); + }); + + if (!posts) { + return ( +

+ + {wp.i18n.__('Loading...', 'ultimate-member')} +

+ ); + } + + if (posts.length === 0) { + return 'No forms found.'; + } + + function get_option( posts ) { + var option = []; + + posts.map( function( post ) { + option.push( + { + label: post.title.rendered, + value: post.id + } + ); + }); + + return option; + } + + function umShortcode( value ) { + + var shortcode = ''; + + if (value !== undefined && value !== '') { + shortcode = '[ultimatemember form_id="' + value + '"]'; + } + + return shortcode; + } + + let posts_data = [{ id: '', title: '' }].concat(posts); + + let get_post = posts_data.map((post) => { + return { + label: post.title.rendered, + value: post.id + }; + }); + + return ( +
+ + + + { + props.setAttributes({ form_id: value }); + umShortcode(value); + }} + /> + + +
+ ); + + }, + save: function save(props) { + return null; + } +}); + +// jQuery(window).on( 'load', function($) { +// var observer = new MutationObserver(function(mutations) { +// mutations.forEach(function(mutation) { +// +// jQuery(mutation.addedNodes).find('.um.um-directory').each(function() { +// var directory = jQuery(this); +// um_ajax_get_members( directory ); +// }); +// }); +// }); +// +// observer.observe(document, {attributes: false, childList: true, characterData: false, subtree:true}); +// }); diff --git a/includes/blocks/um-password-reset/block.json b/includes/blocks/um-password-reset/block.json new file mode 100644 index 00000000..f73a45ee --- /dev/null +++ b/includes/blocks/um-password-reset/block.json @@ -0,0 +1,10 @@ +{ + "apiVersion": 2, + "name": "um-block/um-password-reset", + "title": "Password Reset", + "description": "Displaying the password reset form", + "icon": "unlock", + "category": "um-blocks", + "editorScript": "file:./build/index.js", + "textdomain": "ultimate-member" +} diff --git a/includes/blocks/um-password-reset/src/index.js b/includes/blocks/um-password-reset/src/index.js new file mode 100644 index 00000000..56ab795b --- /dev/null +++ b/includes/blocks/um-password-reset/src/index.js @@ -0,0 +1,19 @@ +import { registerBlockType } from '@wordpress/blocks'; +import ServerSideRender from '@wordpress/server-side-render'; +import { useBlockProps } from '@wordpress/block-editor'; + +registerBlockType('um-block/um-password-reset', { + edit: function (props) { + const blockProps = useBlockProps(); + + return ( +
+ +
+ ); + + }, + save: function save(props) { + return null; + } +}); diff --git a/includes/core/class-blocks.php b/includes/core/class-blocks.php index 89131429..86975428 100644 --- a/includes/core/class-blocks.php +++ b/includes/core/class-blocks.php @@ -45,6 +45,17 @@ if ( ! class_exists( 'um\core\Blocks' ) ) { ), ), ), + 'um-block/um-forms' => array( + 'render_callback' => array( $this, 'um_forms_render' ), + 'attributes' => array( + 'form_id' => array( + 'type' => 'string', + ), + ), + ), + 'um-block/um-password-reset' => array( + 'render_callback' => array( $this, 'um_password_reset_render' ), + ), ); foreach ( $blocks as $k => $block_data ) { @@ -65,5 +76,25 @@ if ( ! class_exists( 'um\core\Blocks' ) ) { return apply_shortcodes( $shortcode ); } + + + public function um_forms_render( $atts ) { + $shortcode = '[ultimatemember'; + + if ( isset( $atts['form_id'] ) && '' !== $atts['form_id'] ) { + $shortcode .= ' form_id="' . $atts['form_id'] . '"'; + } + + $shortcode .= ']'; + + return apply_shortcodes( $shortcode ); + } + + + public function um_password_reset_render() { + $shortcode = '[ultimatemember_password]'; + + return apply_shortcodes( $shortcode ); + } } } diff --git a/includes/core/class-shortcodes.php b/includes/core/class-shortcodes.php index cd7fa823..5cb06727 100644 --- a/includes/core/class-shortcodes.php +++ b/includes/core/class-shortcodes.php @@ -430,11 +430,11 @@ if ( ! class_exists( 'um\core\Shortcodes' ) ) { $args = ! empty( $args ) ? $args : array(); $default_login = $wpdb->get_var( - "SELECT pm.post_id - FROM {$wpdb->postmeta} pm + "SELECT pm.post_id + FROM {$wpdb->postmeta} pm LEFT JOIN {$wpdb->postmeta} pm2 ON( pm.post_id = pm2.post_id AND pm2.meta_key = '_um_is_default' ) - WHERE pm.meta_key = '_um_mode' AND - pm.meta_value = 'login' AND + WHERE pm.meta_key = '_um_mode' AND + pm.meta_value = 'login' AND pm2.meta_value = '1'" ); @@ -463,11 +463,11 @@ if ( ! class_exists( 'um\core\Shortcodes' ) ) { $args = ! empty( $args ) ? $args : array(); $default_register = $wpdb->get_var( - "SELECT pm.post_id - FROM {$wpdb->postmeta} pm + "SELECT pm.post_id + FROM {$wpdb->postmeta} pm LEFT JOIN {$wpdb->postmeta} pm2 ON( pm.post_id = pm2.post_id AND pm2.meta_key = '_um_is_default' ) - WHERE pm.meta_key = '_um_mode' AND - pm.meta_value = 'register' AND + WHERE pm.meta_key = '_um_mode' AND + pm.meta_value = 'register' AND pm2.meta_value = '1'" ); @@ -496,11 +496,11 @@ if ( ! class_exists( 'um\core\Shortcodes' ) ) { $args = ! empty( $args ) ? $args : array(); $default_profile = $wpdb->get_var( - "SELECT pm.post_id - FROM {$wpdb->postmeta} pm + "SELECT pm.post_id + FROM {$wpdb->postmeta} pm LEFT JOIN {$wpdb->postmeta} pm2 ON( pm.post_id = pm2.post_id AND pm2.meta_key = '_um_is_default' ) - WHERE pm.meta_key = '_um_mode' AND - pm.meta_value = 'profile' AND + WHERE pm.meta_key = '_um_mode' AND + pm.meta_value = 'profile' AND pm2.meta_value = '1'" ); @@ -530,11 +530,11 @@ if ( ! class_exists( 'um\core\Shortcodes' ) ) { $args = ! empty( $args ) ? $args : array(); $default_directory = $wpdb->get_var( - "SELECT pm.post_id - FROM {$wpdb->postmeta} pm + "SELECT pm.post_id + FROM {$wpdb->postmeta} pm LEFT JOIN {$wpdb->postmeta} pm2 ON( pm.post_id = pm2.post_id AND pm2.meta_key = '_um_is_default' ) - WHERE pm.meta_key = '_um_mode' AND - pm.meta_value = 'directory' AND + WHERE pm.meta_key = '_um_mode' AND + pm.meta_value = 'directory' AND pm2.meta_value = '1'" ); diff --git a/package.json b/package.json index adaf09b0..4f093087 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,9 @@ "postinstall": "node node_modules/bower/bin/bower install && grunt dev", "build": "npm-run-all build:*", "build:all-blocks": "npm-run-all --parallel build-block-*", - "build-block-um-member-directories": "cd includes/blocks/um-member-directories && wp-scripts build src/index.js" + "build-block-um-member-directories": "cd includes/blocks/um-member-directories && wp-scripts build src/index.js", + "build-block-um-forms": "cd includes/blocks/um-forms && wp-scripts build src/index.js", + "build-block-um-password-reset": "cd includes/blocks/um-password-reset && wp-scripts build src/index.js" }, "engines": { "node": ">= 0.10.0"