PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.7.4
Post Views Counter v1.7.4
1.7.13 1.7.12 1.7.11 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.2 1.3.2.1 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.7.0 1.7.1 1.7.10 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.7.8 1.7.9
post-views-counter / blocks / most-viewed-posts / src / edit.js
post-views-counter / blocks / most-viewed-posts / src Last commit date
block.json 1 year ago edit.js 1 year ago index.js 1 year ago
edit.js
146 lines
1 import { useState } from 'react';
2 import { Notice, Spinner, PanelBody, TextControl, CheckboxControl, BaseControl, RadioControl, SelectControl } from '@wordpress/components';
3 import { __ } from '@wordpress/i18n';
4 import { InspectorControls, useBlockProps } from '@wordpress/block-editor';
5 import ServerSideRender from '@wordpress/server-side-render';
6
7 export default function Edit( { attributes, setAttributes } ) {
8 // attributes
9 const { title, postTypes, period, numberOfPosts, noPostsMessage, order, listType, displayPostViews, displayPostExcerpt, displayPostAuthor, displayPostThumbnail, thumbnailSize } = attributes;
10
11 // initialize post types state
12 const [checkedState, setCheckedState] = useState( ! postTypes ? pvcBlockEditorData.postTypesKeys : postTypes );
13
14 // spinner
15 const spinner = () => {
16 return <Spinner />;
17 }
18
19 const error = ( value ) => {
20 return <Notice status="error">{ __( 'Something went wrong. Try again or refresh the page.', 'post-views-counter' ) }</Notice>;
21 }
22
23 return (
24 <>
25 <InspectorControls>
26 <PanelBody title={ __( 'Settings', 'post-views-counter' ) }>
27 <TextControl
28 __nextHasNoMarginBottom
29 label={ __( 'Title', 'post-views-counter' ) }
30 value={ title }
31 onChange={ ( value ) => setAttributes( { title: value } ) }
32 help={ __( 'Enter empty text to hide title.', 'post-views-counter' ) }
33 />
34 <BaseControl
35 __nextHasNoMarginBottom
36 label={ __( 'Post types', 'post-views-counter' ) }
37 >
38 { (
39 Object.keys( pvcBlockEditorData.postTypes ).map( ( postType ) => (
40 <CheckboxControl
41 key={ postType }
42 label={ pvcBlockEditorData.postTypes[postType] }
43 checked={ checkedState[postType] }
44 onChange={ ( value ) => {
45 // clone postTypes, we cant change attribute value directly
46 let newValue = {...postTypes}
47
48 // set new value
49 newValue[postType] = value
50
51 // set state and attribute
52 setCheckedState( ( prevState ) => ( { ...prevState, [postType]: ! prevState[postType] } ) )
53 setAttributes( { postTypes: newValue } )
54 } }
55 />
56 ) )
57 ) }
58 </BaseControl>
59 <SelectControl
60 __nextHasNoMarginBottom
61 disabled={ pvcBlockEditorData.periods.length === 1 }
62 label={ __( 'Views period', 'post-views-counter' ) }
63 value={ period }
64 options={ pvcBlockEditorData.periods }
65 onChange={ ( value ) => setAttributes( { period: value } ) }
66 />
67 <TextControl
68 __nextHasNoMarginBottom
69 label={ __( 'Number of posts to show', 'post-views-counter' ) }
70 value={ numberOfPosts }
71 onChange={ ( value ) => setAttributes( { numberOfPosts: Number( value ) } ) }
72 />
73 <TextControl
74 __nextHasNoMarginBottom
75 label={ __( 'No posts message', 'post-views-counter' ) }
76 value={ noPostsMessage }
77 onChange={ ( value ) => setAttributes( { noPostsMessage: value } ) }
78 />
79 <RadioControl
80 label={ __( 'Order', 'post-views-counter' ) }
81 selected={ order }
82 options={ [
83 { label: __( 'Ascending', 'post-views-counter' ), value: 'asc' },
84 { label: __( 'Descending', 'post-views-counter' ), value: 'desc' }
85 ] }
86 onChange={ ( value ) => setAttributes( { order: value } ) }
87 />
88 <RadioControl
89 label={ __( 'Display style', 'post-views-counter' ) }
90 selected={ listType }
91 options={ [
92 { label: __( 'Unordered list', 'post-views-counter' ), value: 'unordered' },
93 { label: __( 'Ordered list', 'post-views-counter' ), value: 'ordered' }
94 ] }
95 onChange={ ( value ) => setAttributes( { listType: value } ) }
96 />
97 <BaseControl
98 __nextHasNoMarginBottom
99 label={ __( 'Display Data', 'post-views-counter' ) }
100 >
101 <CheckboxControl
102 __nextHasNoMarginBottom
103 label={ __( 'Post views', 'post-views-counter' ) }
104 checked={ displayPostViews }
105 onChange={ ( value ) => setAttributes( { displayPostViews: value } ) }
106 />
107 <CheckboxControl
108 __nextHasNoMarginBottom
109 label={ __( 'Post excerpt', 'post-views-counter' ) }
110 checked={ displayPostExcerpt }
111 onChange={ ( value ) => setAttributes( { displayPostExcerpt: value } ) }
112 />
113 <CheckboxControl
114 __nextHasNoMarginBottom
115 label={ __( 'Post author', 'post-views-counter' ) }
116 checked={ displayPostAuthor }
117 onChange={ ( value ) => setAttributes( { displayPostAuthor: value } ) }
118 />
119 <CheckboxControl
120 __nextHasNoMarginBottom
121 label={ __( 'Post thumbnail', 'post-views-counter' ) }
122 checked={ displayPostThumbnail }
123 onChange={ ( value ) => setAttributes( { displayPostThumbnail: value } ) }
124 />
125 { displayPostThumbnail && <SelectControl
126 __nextHasNoMarginBottom
127 label={ __( 'Thumbnail size', 'post-views-counter' ) }
128 value={ thumbnailSize }
129 options={ pvcBlockEditorData.imageSizes }
130 onChange={ ( value ) => setAttributes( { thumbnailSize: value } ) }
131 /> }
132 </BaseControl>
133 </PanelBody>
134 </InspectorControls>
135 <div { ...useBlockProps() }>
136 <ServerSideRender
137 httpMethod="POST"
138 block="post-views-counter/most-viewed-posts"
139 attributes={ attributes }
140 LoadingResponsePlaceholder={ spinner }
141 ErrorResponsePlaceholder={ error }
142 />
143 </div>
144 </>
145 )
146 }