diff --git a/dist/blocks.build.js b/dist/blocks.build.js index c424227..a8a725a 100644 --- a/dist/blocks.build.js +++ b/dist/blocks.build.js @@ -1 +1 @@ -!function(e){function t(a){if(n[a])return n[a].exports;var r=n[a]={i:a,l:!1,exports:{}};return e[a].call(r.exports,r,r.exports,t),r.l=!0,r.exports}var n={};t.m=e,t.c=n,t.d=function(e,n,a){t.o(e,n)||Object.defineProperty(e,n,{configurable:!1,enumerable:!0,get:a})},t.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(n,"a",n),n},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=1)}([function(e,t){e.exports=React},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var a=n(2),r=(n(3),n(5));n.n(r);(0,wp.blocks.updateCategory)("a3rev-blocks",{icon:a.a})},function(e,t,n){"use strict";function a(){return a=Object.assign||function(e){for(var t=1;tregister_dynamic_style_file(); + + $js_deps = apply_filters( 'pvc_block_js_deps', array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-editor' ) ); + + wp_register_script( 'page_views_count-cgb-block-js', // Handle. - plugins_url( '/dist/blocks.build.js', dirname( __FILE__ ) ), // Block.build.js: We register the block here. Built with Webpack. - array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-editor' ), // Dependencies, defined above. - // filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.build.js' ), // Version: File modification time. - true // Enqueue the script in the footer. + plugins_url( '/dist/blocks.build.js', dirname( __FILE__ ) ), + $js_deps, + A3_PVC_VERSION, + array( + 'strategy' => 'defer', + 'in_footer' => true, + ) ); wp_localize_script( 'page_views_count-cgb-block-js', 'pvcblock', array( 'preview' => A3_PVC_URL. '/src/blocks/stats/preview.jpg', ) ); - - // Styles. - // wp_enqueue_style( - // 'page_views_count-cgb-block-editor-css', // Handle. - // plugins_url( 'dist/blocks.editor.build.css', dirname( __FILE__ ) ), // Block editor CSS. - // array( 'wp-edit-blocks' ) // Dependency to include the CSS after it. - // // filemtime( plugin_dir_path( __DIR__ ) . 'dist/blocks.editor.build.css' ) // Version: File modification time. - // ); } public function create_a3blocks_section() { @@ -101,114 +84,6 @@ public function create_a3blocks_section() { } public function register_block() { - $this->create_a3blocks_section(); - - if ( ! function_exists( 'register_block_type' ) ) { - // Gutenberg is not active. - return; - } - - // Create Dynamic Block via PHP render callback - $block_args = array( - 'attributes' => array( - 'align' => array( - 'type' => 'string' - ), - 'className' => array( - 'type' => 'string' - ), - 'postID' => array( - 'type' => 'string', - 'default' => 0, - ), - 'isDisabled' => array( - 'type' => 'boolean', - 'default' => true, - ), - 'isPreview' => array( - 'type' => 'boolean', - 'default' => false, - ), - ), - 'render_callback' => array( $this, 'render' ) - ); - - if ( $GLOBALS[A3_PVC_PREFIX.'less']->register_dynamic_style_file() ) { - $block_args = array_merge( $block_args, array( 'editor_style' => 'a3pvc' ) ); - } - - register_block_type( 'page-views-count/stats', $block_args ); - - // Create Block for show on Editor so that this plugin support AJAX load - register_block_type( 'page-views-count/stats-editor', array( - 'attributes' => array( - 'align' => array( - 'type' => 'string' - ), - 'className' => array( - 'type' => 'string' - ), - 'postID' => array( - 'type' => 'string', - 'default' => 0, - ), - 'isDisabled' => array( - 'type' => 'boolean', - 'default' => true, - ), - 'isPreview' => array( - 'type' => 'boolean', - 'default' => false, - ), - ), - 'render_callback' => array( $this, 'render_editor' ) - ) ); - - if ( function_exists( 'wp_set_script_translations' ) ) { - - wp_set_script_translations( 'page_views_count-cgb-block-js', 'page-views-count' ); - } elseif ( function_exists( 'wp_get_jed_locale_data' ) || function_exists( 'gutenberg_get_jed_locale_data' ) ) { - - $locale_data = function_exists( 'wp_get_jed_locale_data' ) ? wp_get_jed_locale_data( 'page-views-count' ) : gutenberg_get_jed_locale_data( 'page-views-count' ); - - wp_add_inline_script( - 'wp-i18n', - 'wp.i18n.setLocaleData( ' . wp_json_encode( $locale_data ) . ', "page-views-count" );', - 'after' - ); - } - } - - public function render( $attributes ) { - - if ( is_admin() ) { - return ''; - } - - if ( ! empty( $attributes['postID'] ) ) { - return '
' . pvc_stats( $attributes['postID'], 0 ) . '
'; - } elseif( isset( $attributes['isDisabled'] ) && false === $attributes['isDisabled'] ) { - - global $post; - - $post_id = $post->ID; - - return '
' . pvc_stats_update( $post_id, 0, $attributes ) . '
'; - } - - return ''; - } - - public function render_editor( $attributes ) { - - if ( empty( $attributes['postID'] ) ) { - return ''; - } - - $attributes['in_editor'] = true; - - return pvc_stats( $attributes['postID'], 0, $attributes ); - } } diff --git a/src/blocks/stats-editor/block.js b/src/blocks/stats-editor/block.js deleted file mode 100644 index d7a3bfb..0000000 --- a/src/blocks/stats-editor/block.js +++ /dev/null @@ -1,78 +0,0 @@ -/** - * BLOCK: page-views-count - * - * Registering a basic block with Gutenberg. - * Simple block, renders and saves the same content without any interactivity. - */ - -// Import CSS. -//import './style.scss'; -//import './editor.scss'; - -const { __ } = wp.i18n; // Import __() from wp.i18n -const { registerBlockType } = wp.blocks; // Import registerBlockType() from wp.blocks - -/** - * Register: aa Gutenberg Block. - * - * Registers a new block provided a unique name and an object defining its - * behavior. Once registered, the block is made editor as an option to any - * editor interface where blocks are implemented. - * - * @link https://wordpress.org/gutenberg/handbook/block-api/ - * @param {string} name Block name. - * @param {Object} settings Block settings. - * @return {?WPBlock} The block, if it has been successfully - * registered; otherwise `undefined`. - */ -registerBlockType('page-views-count/stats-editor', { - // Block name. Block names must be string that contains a namespace prefix. Example: my-plugin/my-custom-block. - title: __('Page Views', 'page-views-count'), // Block title. - - attributes: { - align: { - type: 'string', - }, - postID: { - type: 'string', - }, - isDisabled: { - type: 'boolean', - default: true, - }, - /** - * For previewing? - */ - isPreview: { - type: 'boolean', - default: false, - }, - }, - - /** - * The edit function describes the structure of your block in the context of the editor. - * This represents what the editor will render when the block is used. - * - * The "edit" property must be a valid function. - * - * @link https://wordpress.org/gutenberg/handbook/block-api/block-edit-save/ - */ - edit: ({ attributes, setAttributes }) => { - - // Rendering in PHP - return null; - }, - - /** - * The save function defines the way in which the different attributes should be combined - * into the final markup, which is then serialized by Gutenberg into post_content. - * - * The "save" property must be specified and must be a valid function. - * - * @link https://wordpress.org/gutenberg/handbook/block-api/block-edit-save/ - */ - save() { - // Rendering in PHP - return null; - }, -}); diff --git a/src/blocks/stats/block.js b/src/blocks/stats/block.js index 97856bd..b86bd50 100644 --- a/src/blocks/stats/block.js +++ b/src/blocks/stats/block.js @@ -5,164 +5,41 @@ * Simple block, renders and saves the same content without any interactivity. */ -// Import CSS. -//import './style.scss'; -//import './editor.scss'; +// import './style.scss'; +// import './editor.scss'; -const { __ } = wp.i18n; // Import __() from wp.i18n -const { registerBlockType } = wp.blocks; // Import registerBlockType() from wp.blocks -const { BlockControls, AlignmentToolbar, InspectorControls } = wp.blockEditor; -const { Placeholder, PanelBody, PanelRow, ToggleControl } = wp.components; -const { serverSideRender: ServerSideRender } = wp; -const { Fragment } = wp.element; -const { select } = wp.data; +import edit from './edit'; +import metadata from './block.json'; -import IconPageView from './icon.svg'; +const { registerBlockType } = wp.blocks; +const { name, attributes } = metadata; /** - * Register: aa Gutenberg Block. + * Register: a3 Gutenberg Block. * * Registers a new block provided a unique name and an object defining its * behavior. Once registered, the block is made editor as an option to any * editor interface where blocks are implemented. * - * @link https://wordpress.org/gutenberg/handbook/block-api/ * @param {string} name Block name. * @param {Object} settings Block settings. * @return {?WPBlock} The block, if it has been successfully * registered; otherwise `undefined`. */ -registerBlockType('page-views-count/stats', { - // Block name. Block names must be string that contains a namespace prefix. Example: my-plugin/my-custom-block. - title: __('Page Views', 'page-views-count'), // Block title. - description: __('Show all time views and views today', 'page-views-count'), - icon: { - src: IconPageView, - foreground: '#24b6f1', - }, // Block icon, can get from Dashicons → https://developer.wordpress.org/resource/dashicons/. - category: 'a3rev-blocks', // Block category — Group blocks together based on common traits E.g. common, formatting, layout widgets, embed. - supports: { - multiple: false, - }, - keywords: [ - __('Page Views Count', 'page-views-count'), - __('Views', 'page-views-count'), - __('Stats', 'page-views-count'), - ], - example: { - attributes: { - isPreview: true, - }, - }, - - attributes: { - align: { - type: 'string', - }, - postID: { - type: 'string', - }, - isDisabled: { - type: 'boolean', - default: true, - }, - /** - * For previewing? - */ - isPreview: { - type: 'boolean', - default: false, - }, - }, - - /** - * The edit function describes the structure of your block in the context of the editor. - * This represents what the editor will render when the block is used. - * - * The "edit" property must be a valid function. - * - * @link https://wordpress.org/gutenberg/handbook/block-api/block-edit-save/ - */ - edit: ({ attributes, setAttributes }) => { - - if ( attributes.isPreview ) { - return ( { ); - } - const { align, isDisabled } = attributes; - const postID = select('core/editor').getCurrentPostId(); +import icon from './icon.svg'; - function onChangeAlign(newAlign) { - setAttributes({ align: newAlign }); - } - - function toggleManualShow(isDisabled) { - setAttributes({ isDisabled: !isDisabled }); - const activateOption = document.querySelector('#a3_pvc_activated'); - - if (isDisabled) { - activateOption.removeAttribute('checked'); - activateOption.setAttribute('disabled', true); - } else { - activateOption.setAttribute('checked', true); - activateOption.removeAttribute('disabled'); - } - } - - attributes.postID = postID; - - return ( - - - - - - - - - {isDisabled ? ( - - {__('Need to active from Settings of this block', 'page-views-count')} - - ) : ( - - - - - - - - )} - - ); +export const settings = { + icon: { + src: icon, + foreground: '#24b6f1', }, - /** - * The save function defines the way in which the different attributes should be combined - * into the final markup, which is then serialized by Gutenberg into post_content. - * - * The "save" property must be specified and must be a valid function. - * - * @link https://wordpress.org/gutenberg/handbook/block-api/block-edit-save/ - */ - save() { - // Rendering in PHP + attributes, + edit, + save: () => { return null; }, -}); +}; + +registerBlockType( { name, ...metadata }, settings ); diff --git a/src/blocks/stats/block.json b/src/blocks/stats/block.json index 89f190a..dc13997 100644 --- a/src/blocks/stats/block.json +++ b/src/blocks/stats/block.json @@ -1,14 +1,17 @@ { + "$schema": "https://schemas.wp.org/trunk/block.json", + "apiVersion": 3, "name": "page-views-count/stats", "title": "Page Views", "description": "Show all time views and views today", "textdomain": "page-views-count", - "icon": { - "foreground": "#24b6f1" - }, "category": "a3rev-blocks", "supports": { - "multiple": false + "multiple": false, + "spacing": { + "margin": true, + "padding": false + } }, "keywords": [ "Page Views Count", "Views", "Stats" ], "example": { @@ -18,7 +21,8 @@ }, "attributes": { "align": { - "type": "string" + "type": "string", + "default": "left" }, "postID": { "type": "string", @@ -32,5 +36,10 @@ "type": "boolean", "default": false } - } + }, + "selectors": { + "root": ".wp-block-apage-views-count-stats" + }, + "editorScript": "page_views_count-cgb-block-js", + "editorStyle": [ "a3pvc" ] } diff --git a/src/blocks/stats/block.php b/src/blocks/stats/block.php new file mode 100644 index 0000000..683e559 --- /dev/null +++ b/src/blocks/stats/block.php @@ -0,0 +1,58 @@ +ID; + $html_output = pvc_stats_update( $post_id, 0, $attributes ); + } + + $wrapper_attributes = get_block_wrapper_attributes(); + return sprintf( '
%2$s
', $wrapper_attributes, $html_output ); + } + + return ''; +} + +/** + * Registers the `core/post-title` block on the server. + */ +function register_block_a3_pvc() { + register_block_type( + __DIR__ . '/block.json', + array( + 'render_callback' => 'render_block_a3_pvc', + ) + ); + + if ( function_exists( 'wp_set_script_translations' ) ) { + wp_set_script_translations( 'page_views_count-cgb-block-js', 'page-views-count' ); + } elseif ( function_exists( 'wp_get_jed_locale_data' ) || function_exists( 'gutenberg_get_jed_locale_data' ) ) { + $locale_data = function_exists( 'wp_get_jed_locale_data' ) ? wp_get_jed_locale_data( 'page-views-count' ) : gutenberg_get_jed_locale_data( 'page-views-count' ); + + wp_add_inline_script( + 'wp-i18n', + 'wp.i18n.setLocaleData( ' . wp_json_encode( $locale_data ) . ', "page-views-count" );', + 'after' + ); + } +} +add_action( 'init', 'register_block_a3_pvc' ); diff --git a/src/blocks/stats/edit.js b/src/blocks/stats/edit.js new file mode 100644 index 0000000..8905b72 --- /dev/null +++ b/src/blocks/stats/edit.js @@ -0,0 +1,85 @@ +/** + * External dependencies + */ + +/** + * Internal dependencies + */ + +const { __ } = wp.i18n; // Import __() from wp.i18n +const { useBlockProps, BlockControls, AlignmentToolbar, InspectorControls } = wp.blockEditor || wp.editor; +const { Placeholder, PanelBody, PanelRow, ToggleControl } = wp.components; +const { serverSideRender: ServerSideRender } = wp; +const { Fragment } = wp.element; +const { select } = wp.data; + +export default function BlockEdit( { attributes, setAttributes } ) { + + const blockProps = useBlockProps(); + + if ( attributes.isPreview ) { + return ( { ); + } + + const { align, isDisabled } = attributes; + const postID = select('core/editor').getCurrentPostId(); + + function onChangeAlign(newAlign) { + setAttributes({ align: newAlign }); + } + + function toggleManualShow(isDisabled) { + setAttributes({ isDisabled: !isDisabled }); + const activateOption = document.querySelector('#a3_pvc_activated'); + + if (isDisabled) { + activateOption.removeAttribute('checked'); + activateOption.setAttribute('disabled', true); + } else { + activateOption.setAttribute('checked', true); + activateOption.removeAttribute('disabled'); + } + } + + return ( + + + + + + + + + {isDisabled ? ( + + {__('Need to active from Settings of this block', 'page-views-count')} + + ) : ( + + + + +
+ +
+
+ )} +
+ ); +}