edit.js
770 lines
| 1 | import { escape_html, unescape_html } from '../utils'; |
| 2 | |
| 3 | const { serverSideRender: ServerSideRender } = wp; |
| 4 | const { Component, Fragment } = wp.element; |
| 5 | const { BlockControls } = wp.blockEditor; |
| 6 | const { CheckboxControl, Disabled, SelectControl, Spinner, TextareaControl, TextControl, Toolbar, ToolbarButton } = wp.components; |
| 7 | const { __ } = wp.i18n; |
| 8 | const endpoint = 'wordpress-popular-posts/v1'; |
| 9 | |
| 10 | export class WPPWidgetBlockEdit extends Component |
| 11 | { |
| 12 | constructor(props) |
| 13 | { |
| 14 | super(props); |
| 15 | |
| 16 | this.state = { |
| 17 | error: null, |
| 18 | editMode: true, |
| 19 | themes: null, |
| 20 | imgSizes: null, |
| 21 | taxonomies: null |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | componentDidMount() |
| 26 | { |
| 27 | const { attributes } = this.props; |
| 28 | |
| 29 | this.getThemes(); |
| 30 | this.getImageSizes(); |
| 31 | this.getTaxonomies(); |
| 32 | |
| 33 | this.setState({ editMode: attributes._editMode }); |
| 34 | } |
| 35 | |
| 36 | getThemes() |
| 37 | { |
| 38 | wp.apiFetch({ path: endpoint + '/themes' }) |
| 39 | .then( |
| 40 | ( themes ) => { |
| 41 | this.setState({ |
| 42 | themes |
| 43 | }); |
| 44 | }, |
| 45 | ( error ) => { |
| 46 | this.setState({ |
| 47 | error, |
| 48 | themes: null |
| 49 | }); |
| 50 | } |
| 51 | ); |
| 52 | } |
| 53 | |
| 54 | getImageSizes() |
| 55 | { |
| 56 | wp.apiFetch({ path: endpoint + '/thumbnails' }) |
| 57 | .then( |
| 58 | ( imgSizes ) => { |
| 59 | this.setState({ |
| 60 | imgSizes |
| 61 | }); |
| 62 | }, |
| 63 | ( error ) => { |
| 64 | this.setState({ |
| 65 | error, |
| 66 | imgSizes: null |
| 67 | }); |
| 68 | } |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | getTaxonomies() |
| 73 | { |
| 74 | const { attributes } = this.props; |
| 75 | |
| 76 | wp.apiFetch({ path: endpoint + '/taxonomies' }) |
| 77 | .then( |
| 78 | ( taxonomies ) => { |
| 79 | if ( taxonomies ) { |
| 80 | let tax = attributes.tax.split(';'), |
| 81 | term_id = attributes.term_id.split(';'); |
| 82 | |
| 83 | if ( tax.length && tax.length == term_id.length ) { |
| 84 | let selected_taxonomies = {}; |
| 85 | |
| 86 | for( var t = 0; t < tax.length; t++ ) { |
| 87 | selected_taxonomies[tax[t]] = term_id[t]; |
| 88 | } |
| 89 | |
| 90 | for( const tax in taxonomies ) { |
| 91 | taxonomies[tax]._terms = 'undefined' != typeof selected_taxonomies[tax] ? selected_taxonomies[tax] : ''; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | this.setState({ |
| 97 | taxonomies |
| 98 | }); |
| 99 | }, |
| 100 | ( error ) => { |
| 101 | this.setState({ |
| 102 | error, |
| 103 | taxonomies: null |
| 104 | }); |
| 105 | } |
| 106 | ); |
| 107 | } |
| 108 | |
| 109 | getBlockControls() |
| 110 | { |
| 111 | const { setAttributes } = this.props; |
| 112 | const _self = this; |
| 113 | |
| 114 | function onPreviewChange() |
| 115 | { |
| 116 | let editMode = ! _self.state.editMode; |
| 117 | _self.setState({ editMode: editMode }); |
| 118 | setAttributes({ _editMode: editMode }); |
| 119 | } |
| 120 | |
| 121 | return ( |
| 122 | <BlockControls> |
| 123 | <Toolbar label="{ __('Settings') }"> |
| 124 | <ToolbarButton |
| 125 | label={ this.state.editMode ? __('Preview', 'wordpress-popular-posts') : __('Edit', 'wordpress-popular-posts') } |
| 126 | icon={ this.state.editMode ? "format-image" : "edit" } |
| 127 | onClick={onPreviewChange} |
| 128 | /> |
| 129 | </Toolbar> |
| 130 | </BlockControls> |
| 131 | ); |
| 132 | } |
| 133 | |
| 134 | getMainFields() |
| 135 | { |
| 136 | const { attributes, setAttributes } = this.props; |
| 137 | |
| 138 | function onTitleChange(value) |
| 139 | { |
| 140 | value = escape_html(unescape_html(value)); |
| 141 | setAttributes({ title: value }); |
| 142 | } |
| 143 | |
| 144 | function onLimitChange(value) |
| 145 | { |
| 146 | let limit = Number.isInteger(Number(value)) && Number(value) > 0 ? value : 10; |
| 147 | setAttributes({ limit: Number(limit) }); |
| 148 | } |
| 149 | |
| 150 | function onOrderByChange(value) |
| 151 | { |
| 152 | setAttributes({ order_by: value }); |
| 153 | } |
| 154 | |
| 155 | function onTimeRangeChange(value) |
| 156 | { |
| 157 | setAttributes({ range: value }); |
| 158 | } |
| 159 | |
| 160 | function onTimeQuantityChange(value) { |
| 161 | let qty = Number.isInteger(Number(value)) && Number(value) > 0 ? value : 24; |
| 162 | setAttributes({ time_quantity: Number(qty) }); |
| 163 | } |
| 164 | |
| 165 | function onTimeUnitChange(value) { |
| 166 | setAttributes({ time_unit: value }); |
| 167 | } |
| 168 | |
| 169 | function onFreshnessChange(value) |
| 170 | { |
| 171 | setAttributes({ freshness: value }); |
| 172 | } |
| 173 | |
| 174 | return <Fragment> |
| 175 | <TextControl |
| 176 | label={__('Title', 'wordpress-popular-posts')} |
| 177 | value={attributes.title} |
| 178 | onChange={onTitleChange} |
| 179 | /> |
| 180 | <TextControl |
| 181 | label={__('Limit', 'wordpress-popular-posts')} |
| 182 | value={attributes.limit} |
| 183 | onChange={onLimitChange} |
| 184 | /> |
| 185 | <SelectControl |
| 186 | label={__('Sort posts by', 'wordpress-popular-posts')} |
| 187 | value={attributes.order_by} |
| 188 | options={[ |
| 189 | {label: __('Total views', 'wordpress-popular-posts'), value: 'views'}, |
| 190 | {label: __('Avg. daily views', 'wordpress-popular-posts'), value: 'avg'}, |
| 191 | {label: __('Comments', 'wordpress-popular-posts'), value: 'comments'} |
| 192 | ]} |
| 193 | onChange={onOrderByChange} |
| 194 | /> |
| 195 | <SelectControl |
| 196 | label={__('Time Range', 'wordpress-popular-posts')} |
| 197 | value={attributes.range} |
| 198 | options={[ |
| 199 | {label: __('Last 24 Hours', 'wordpress-popular-posts'), value: 'last24hours'}, |
| 200 | {label: __('Last 7 days', 'wordpress-popular-posts'), value: 'last7days'}, |
| 201 | {label: __('Last 30 days', 'wordpress-popular-posts'), value: 'last30days'}, |
| 202 | {label: __('All-time', 'wordpress-popular-posts'), value: 'all'}, |
| 203 | {label: __('Custom', 'wordpress-popular-posts'), value: 'custom'}, |
| 204 | ]} |
| 205 | onChange={onTimeRangeChange} |
| 206 | /> |
| 207 | { 'custom' == attributes.range && |
| 208 | <div className='option-subset'> |
| 209 | <TextControl |
| 210 | label={__('Time Quantity', 'wordpress-popular-posts')} |
| 211 | value={attributes.time_quantity} |
| 212 | onChange={onTimeQuantityChange} |
| 213 | /> |
| 214 | <SelectControl |
| 215 | label={__('Time Unit', 'wordpress-popular-posts')} |
| 216 | value={attributes.time_unit} |
| 217 | options={[ |
| 218 | {label: __('Minute(s)', 'wordpress-popular-posts'), value: 'minute'}, |
| 219 | {label: __('Hour(s)', 'wordpress-popular-posts'), value: 'hour'}, |
| 220 | {label: __('Day(s)', 'wordpress-popular-posts'), value: 'day'} |
| 221 | ]} |
| 222 | onChange={onTimeUnitChange} |
| 223 | /> |
| 224 | </div> |
| 225 | } |
| 226 | <CheckboxControl |
| 227 | label={__('Display only posts published within the selected Time Range', 'wordpress-popular-posts')} |
| 228 | checked={attributes.freshness} |
| 229 | onChange={onFreshnessChange} |
| 230 | /> |
| 231 | </Fragment>; |
| 232 | } |
| 233 | |
| 234 | getFiltersFields() |
| 235 | { |
| 236 | const { attributes, setAttributes } = this.props; |
| 237 | const _self = this; |
| 238 | |
| 239 | function onPostTypeChange(value) |
| 240 | { |
| 241 | let new_value = value.replace(/[^a-z0-9-_\,]+/gi, ''); |
| 242 | setAttributes({ post_type: new_value }); |
| 243 | } |
| 244 | |
| 245 | function onPostIDExcludeChange(value) |
| 246 | { |
| 247 | let new_value = value.replace(/[^0-9\,]/g, ''); |
| 248 | setAttributes({ pid: new_value }); |
| 249 | } |
| 250 | |
| 251 | function onAuthorChange(value) |
| 252 | { |
| 253 | let new_value = value.replace(/[^0-9\,]/g, ''); |
| 254 | setAttributes({ author: new_value }); |
| 255 | } |
| 256 | |
| 257 | function onTaxChange(taxonomy_name, terms) |
| 258 | { |
| 259 | let taxonomies = _self.state.taxonomies; |
| 260 | |
| 261 | terms = terms.replace(/[^0-9-\,]/g, ''); |
| 262 | |
| 263 | if ( taxonomies && 'undefined' != typeof taxonomies[taxonomy_name] ) { |
| 264 | taxonomies[taxonomy_name]._terms = terms; |
| 265 | _self.setState({ taxonomies: taxonomies }); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | function onTaxBlur(taxonomy_name) |
| 270 | { |
| 271 | let taxonomies = _self.state.taxonomies; |
| 272 | |
| 273 | if ( taxonomies && 'undefined' != typeof taxonomies[taxonomy_name] ) { |
| 274 | let terms_arr = taxonomies[taxonomy_name]._terms.split(','); |
| 275 | |
| 276 | // Remove invalid values |
| 277 | if ( terms_arr.length ) |
| 278 | terms_arr = terms_arr.map((term) => term.trim()) |
| 279 | .filter((term) => '' != term && '-' != term); |
| 280 | |
| 281 | // Remove duplicates |
| 282 | if ( terms_arr.length ) |
| 283 | terms_arr = Array.from(new Set(terms_arr)); |
| 284 | |
| 285 | taxonomies[taxonomy_name]._terms = terms_arr.join(','); |
| 286 | |
| 287 | _self.setState({ taxonomies }); |
| 288 | |
| 289 | let tax = '', |
| 290 | term_id = ''; |
| 291 | |
| 292 | for ( let key in _self.state.taxonomies ) { |
| 293 | if ( _self.state.taxonomies.hasOwnProperty(key) ) { |
| 294 | |
| 295 | if ( ! _self.state.taxonomies[key]._terms.length ) |
| 296 | continue; |
| 297 | |
| 298 | tax += key + ';'; |
| 299 | term_id += _self.state.taxonomies[key]._terms + ';'; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | // Remove trailing semicolon |
| 304 | if ( tax && term_id ) { |
| 305 | tax = tax.replace(new RegExp(';$'), ''); |
| 306 | term_id = term_id.replace(new RegExp(';$'), ''); |
| 307 | } |
| 308 | |
| 309 | setAttributes({ tax: tax, term_id: term_id }); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | let taxonomies = []; |
| 314 | |
| 315 | if ( this.state.taxonomies ) { |
| 316 | for( const tax in this.state.taxonomies ) { |
| 317 | taxonomies.push( |
| 318 | { |
| 319 | name: this.state.taxonomies[tax].name, |
| 320 | label: this.state.taxonomies[tax].labels.singular_name + ' (' + this.state.taxonomies[tax].name + ')', |
| 321 | terms: this.state.taxonomies[tax]._terms |
| 322 | } |
| 323 | ); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | return <Fragment> |
| 328 | <p className='not-a-legend'><strong>{__('Filters', 'wordpress-popular-posts')}</strong></p> |
| 329 | <TextControl |
| 330 | label={__('Post type(s)', 'wordpress-popular-posts')} |
| 331 | help={__('Post types must be comma separated.', 'wordpress-popular-posts')} |
| 332 | value={attributes.post_type} |
| 333 | onChange={onPostTypeChange} |
| 334 | /> |
| 335 | <TextControl |
| 336 | label={__('Post ID(s) to exclude', 'wordpress-popular-posts')} |
| 337 | help={__('IDs must be comma separated.', 'wordpress-popular-posts')} |
| 338 | value={attributes.pid} |
| 339 | onChange={onPostIDExcludeChange} |
| 340 | /> |
| 341 | <TextControl |
| 342 | label={__('Author ID(s)', 'wordpress-popular-posts')} |
| 343 | help={__('IDs must be comma separated.', 'wordpress-popular-posts')} |
| 344 | value={attributes.author} |
| 345 | onChange={onAuthorChange} |
| 346 | /> |
| 347 | { taxonomies && taxonomies.filter((tax) => 'post_format' != tax.name).map((tax) => |
| 348 | { |
| 349 | return ( |
| 350 | <TextControl |
| 351 | label={tax.label} |
| 352 | help={__('Term IDs must be comma separated, prefix a minus sign to exclude.', 'wordpress-popular-posts')} |
| 353 | value={tax.terms} |
| 354 | onChange={(terms) => onTaxChange(tax.name, terms)} |
| 355 | onBlur={() => onTaxBlur(tax.name)} |
| 356 | /> |
| 357 | ); |
| 358 | } |
| 359 | )} |
| 360 | </Fragment>; |
| 361 | } |
| 362 | |
| 363 | getPostSettingsFields() |
| 364 | { |
| 365 | const { attributes, setAttributes } = this.props; |
| 366 | const _self = this; |
| 367 | |
| 368 | function onShortenTitleChange(value) { |
| 369 | if ( false == value ) |
| 370 | setAttributes({ title_length: 0, title_by_words: 0, shorten_title: value }); |
| 371 | else |
| 372 | setAttributes({ shorten_title: value, title_length: 25 }); |
| 373 | } |
| 374 | |
| 375 | function onTitleLengthChange(value) |
| 376 | { |
| 377 | let length = Number.isInteger(Number(value)) && Number(value) >= 0 ? value : 0; |
| 378 | setAttributes({ title_length: Number(length) }); |
| 379 | } |
| 380 | |
| 381 | function onDisplayExcerptChange(value) { |
| 382 | if ( false == value ) |
| 383 | setAttributes({ excerpt_length: 0, excerpt_by_words: 0, display_post_excerpt: value, excerpt_format: false }); |
| 384 | else |
| 385 | setAttributes({ display_post_excerpt: value, excerpt_length: 55 }); |
| 386 | } |
| 387 | |
| 388 | function onExcerptLengthChange(value) |
| 389 | { |
| 390 | let length = Number.isInteger(Number(value)) && Number(value) >= 0 ? value : 0; |
| 391 | setAttributes({ excerpt_length: Number(length) }); |
| 392 | } |
| 393 | |
| 394 | function onDisplayThumbnailChange(value) { |
| 395 | if ( false == value ) |
| 396 | setAttributes({ thumbnail_width: 0, thumbnail_height: 0, display_post_thumbnail: value, thumbnail_build: 'manual' }); |
| 397 | else |
| 398 | setAttributes({ thumbnail_width: 75, thumbnail_height: 75, display_post_thumbnail: value }); |
| 399 | } |
| 400 | |
| 401 | function onThumbnailDimChange(dim, value) |
| 402 | { |
| 403 | let width = Number.isInteger(Number(value)) && Number(value) >= 0 ? value : 0; |
| 404 | setAttributes(( 'width' == dim ? { thumbnail_width: Number(width) } : { thumbnail_height: Number(width) } )); |
| 405 | } |
| 406 | |
| 407 | function onThumbnailBuildChange(value) |
| 408 | { |
| 409 | if ( 'predefined' == value ) { |
| 410 | let fallback = 0; |
| 411 | |
| 412 | setAttributes({ |
| 413 | thumbnail_width: _self.state.imgSizes[sizes[fallback].value].width, |
| 414 | thumbnail_height: _self.state.imgSizes[sizes[fallback].value].height, |
| 415 | thumbnail_size: sizes[fallback].value |
| 416 | }); |
| 417 | } else { |
| 418 | setAttributes({ |
| 419 | thumbnail_width: 75, |
| 420 | thumbnail_height: 75, |
| 421 | thumbnail_size: '' |
| 422 | }); |
| 423 | } |
| 424 | setAttributes({ thumbnail_build: value }); |
| 425 | } |
| 426 | |
| 427 | function onThumbnailSizeChange(value) { |
| 428 | setAttributes({ |
| 429 | thumbnail_width: _self.state.imgSizes[value].width, |
| 430 | thumbnail_height: _self.state.imgSizes[value].height, |
| 431 | thumbnail_size: value |
| 432 | }); |
| 433 | } |
| 434 | |
| 435 | let sizes = []; |
| 436 | |
| 437 | if ( this.state.imgSizes ) { |
| 438 | for( const size in this.state.imgSizes ) { |
| 439 | sizes.push( |
| 440 | { |
| 441 | label: size, |
| 442 | value: size |
| 443 | }, |
| 444 | ); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | return <Fragment> |
| 449 | <p className='not-a-legend'><strong>{__('Posts settings', 'wordpress-popular-posts')}</strong></p> |
| 450 | <CheckboxControl |
| 451 | label={__('Shorten title', 'wordpress-popular-posts')} |
| 452 | checked={attributes.shorten_title} |
| 453 | onChange={onShortenTitleChange} |
| 454 | /> |
| 455 | { attributes.shorten_title && |
| 456 | <div className='option-subset'> |
| 457 | <TextControl |
| 458 | label={__('Shorten title to', 'wordpress-popular-posts')} |
| 459 | value={attributes.title_length} |
| 460 | onChange={onTitleLengthChange} |
| 461 | /> |
| 462 | <SelectControl |
| 463 | value={attributes.title_by_words} |
| 464 | options={[ |
| 465 | { label: __('characters', 'wordpress-popular-posts'), value: 0 }, |
| 466 | { label: __('words', 'wordpress-popular-posts'), value: 1 }, |
| 467 | ]} |
| 468 | onChange={(value) => setAttributes({ title_by_words: Number(value) })} |
| 469 | /> |
| 470 | </div> |
| 471 | } |
| 472 | <CheckboxControl |
| 473 | label={__('Display post excerpt', 'wordpress-popular-posts')} |
| 474 | checked={attributes.display_post_excerpt} |
| 475 | onChange={onDisplayExcerptChange} |
| 476 | /> |
| 477 | { attributes.display_post_excerpt && |
| 478 | <div className='option-subset'> |
| 479 | <CheckboxControl |
| 480 | label={__('Keep text format and links', 'wordpress-popular-posts')} |
| 481 | checked={attributes.excerpt_format} |
| 482 | onChange={(value) => setAttributes({ excerpt_format: value })} |
| 483 | /> |
| 484 | <TextControl |
| 485 | label={__('Excerpt length', 'wordpress-popular-posts')} |
| 486 | value={attributes.excerpt_length} |
| 487 | onChange={onExcerptLengthChange} |
| 488 | /> |
| 489 | <SelectControl |
| 490 | value={attributes.excerpt_by_words} |
| 491 | options={[ |
| 492 | { label: __('characters', 'wordpress-popular-posts'), value: 0 }, |
| 493 | { label: __('words', 'wordpress-popular-posts'), value: 1 }, |
| 494 | ]} |
| 495 | onChange={(value) => setAttributes({ excerpt_by_words: Number(value) })} |
| 496 | /> |
| 497 | </div> |
| 498 | } |
| 499 | <CheckboxControl |
| 500 | label={__('Display post thumbnail', 'wordpress-popular-posts')} |
| 501 | checked={attributes.display_post_thumbnail} |
| 502 | onChange={onDisplayThumbnailChange} |
| 503 | /> |
| 504 | { attributes.display_post_thumbnail && |
| 505 | <div className='option-subset'> |
| 506 | <SelectControl |
| 507 | value={attributes.thumbnail_build} |
| 508 | options={[ |
| 509 | { label: __('Set size manually', 'wordpress-popular-posts'), value: 'manual' }, |
| 510 | { label: __('Use predefined size', 'wordpress-popular-posts'), value: 'predefined' }, |
| 511 | ]} |
| 512 | onChange={onThumbnailBuildChange} |
| 513 | /> |
| 514 | { 'manual' == attributes.thumbnail_build && |
| 515 | <Fragment> |
| 516 | <TextControl |
| 517 | label={__('Thumbnail width', 'wordpress-popular-posts')} |
| 518 | help={__('Size in px units (pixels)', 'wordpress-popular-posts')} |
| 519 | value={attributes.thumbnail_width} |
| 520 | onChange={(value) => onThumbnailDimChange('width', value)} |
| 521 | /> |
| 522 | <TextControl |
| 523 | label={__('Thumbnail height', 'wordpress-popular-posts')} |
| 524 | help={__('Size in px units (pixels)', 'wordpress-popular-posts')} |
| 525 | value={attributes.thumbnail_height} |
| 526 | onChange={(value) => onThumbnailDimChange('height', value)} |
| 527 | /> |
| 528 | </Fragment> |
| 529 | } |
| 530 | { 'predefined' == attributes.thumbnail_build && |
| 531 | <Fragment> |
| 532 | <SelectControl |
| 533 | value={attributes.thumbnail_size} |
| 534 | options={sizes} |
| 535 | onChange={onThumbnailSizeChange} |
| 536 | /> |
| 537 | </Fragment> |
| 538 | } |
| 539 | </div> |
| 540 | } |
| 541 | { _wordpress_popular_posts.can_show_rating && |
| 542 | <CheckboxControl |
| 543 | label={__('Display post rating', 'wordpress-popular-posts')} |
| 544 | checked={attributes.rating} |
| 545 | onChange={(value) => setAttributes({ rating: value })} |
| 546 | /> |
| 547 | } |
| 548 | </Fragment>; |
| 549 | } |
| 550 | |
| 551 | getStatsTagFields() |
| 552 | { |
| 553 | const { attributes, setAttributes } = this.props; |
| 554 | |
| 555 | let taxonomies = []; |
| 556 | |
| 557 | if ( this.state.taxonomies ) { |
| 558 | for( const tax in this.state.taxonomies ) { |
| 559 | taxonomies.push( |
| 560 | { |
| 561 | label: this.state.taxonomies[tax].labels.singular_name + ' (' + this.state.taxonomies[tax].name + ')', |
| 562 | value: this.state.taxonomies[tax].name |
| 563 | }, |
| 564 | ); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | return <Fragment> |
| 569 | <p className='not-a-legend'><strong>{__('Stats Tag settings', 'wordpress-popular-posts')}</strong></p> |
| 570 | <CheckboxControl |
| 571 | label={__('Display comments count', 'wordpress-popular-posts')} |
| 572 | checked={attributes.stats_comments} |
| 573 | onChange={(value) => setAttributes({ stats_comments: value })} |
| 574 | /> |
| 575 | <CheckboxControl |
| 576 | label={__('Display views', 'wordpress-popular-posts')} |
| 577 | checked={attributes.stats_views} |
| 578 | onChange={(value) => setAttributes({ stats_views: value })} |
| 579 | /> |
| 580 | <CheckboxControl |
| 581 | label={__('Display author', 'wordpress-popular-posts')} |
| 582 | checked={attributes.stats_author} |
| 583 | onChange={(value) => setAttributes({ stats_author: value })} |
| 584 | /> |
| 585 | <CheckboxControl |
| 586 | label={__('Display date', 'wordpress-popular-posts')} |
| 587 | checked={attributes.stats_date} |
| 588 | onChange={(value) => setAttributes({ stats_date: value })} |
| 589 | /> |
| 590 | { attributes.stats_date && |
| 591 | <div className='option-subset'> |
| 592 | <SelectControl |
| 593 | label={__('Date Format', 'wordpress-popular-posts')} |
| 594 | value={attributes.stats_date_format} |
| 595 | options={[ |
| 596 | { label: __('Relative', 'wordpress-popular-posts'), value: 'relative' }, |
| 597 | { label: __('Month Day, Year', 'wordpress-popular-posts'), value: 'F j, Y' }, |
| 598 | { label: __('yyyy/mm/dd', 'wordpress-popular-posts'), value: 'Y/m/d' }, |
| 599 | { label: __('mm/dd/yyyy', 'wordpress-popular-posts'), value: 'm/d/Y' }, |
| 600 | { label: __('dd/mm/yyyy', 'wordpress-popular-posts'), value: 'd/m/Y' }, |
| 601 | { label: __('WordPress Date Format', 'wordpress-popular-posts'), value: 'wp_date_format' }, |
| 602 | ]} |
| 603 | onChange={(value) => setAttributes({ stats_date_format: value })} |
| 604 | /> |
| 605 | </div> |
| 606 | } |
| 607 | <CheckboxControl |
| 608 | label={__('Display taxonomy', 'wordpress-popular-posts')} |
| 609 | checked={attributes.stats_taxonomy} |
| 610 | onChange={(value) => setAttributes({ stats_taxonomy: value })} |
| 611 | /> |
| 612 | { attributes.stats_taxonomy && |
| 613 | <div className='option-subset'> |
| 614 | <SelectControl |
| 615 | label={__('Taxonomy', 'wordpress-popular-posts')} |
| 616 | value={attributes.taxonomy} |
| 617 | options={taxonomies} |
| 618 | onChange={(value) => setAttributes({ taxonomy: value })} |
| 619 | /> |
| 620 | </div> |
| 621 | } |
| 622 | </Fragment>; |
| 623 | } |
| 624 | |
| 625 | getHTMLMarkupFields() |
| 626 | { |
| 627 | const { attributes, setAttributes } = this.props; |
| 628 | const _self = this; |
| 629 | |
| 630 | function onThemeChange(value) |
| 631 | { |
| 632 | if ( 'undefined' != typeof _self.state.themes[value] ) { |
| 633 | let config = _self.state.themes[value].json.config; |
| 634 | |
| 635 | setAttributes({ |
| 636 | shorten_title: config.shorten_title.active, |
| 637 | title_length: config.shorten_title.length, |
| 638 | title_by_words: config.shorten_title.words ? 1 : 0, |
| 639 | display_post_excerpt: config['post-excerpt'].active, |
| 640 | excerpt_format: config['post-excerpt'].format, |
| 641 | excerpt_length: config['post-excerpt'].length, |
| 642 | excerpt_by_words: config['post-excerpt'].words ? 1 : 0, |
| 643 | display_post_thumbnail: config.thumbnail.active, |
| 644 | thumbnail_build: config.thumbnail.build, |
| 645 | thumbnail_width: config.thumbnail.width, |
| 646 | thumbnail_height: config.thumbnail.height, |
| 647 | stats_comments: config.stats_tag.comment_count, |
| 648 | stats_views: config.stats_tag.views, |
| 649 | stats_author: config.stats_tag.author, |
| 650 | stats_date: config.stats_tag.date.active, |
| 651 | stats_date_format: config.stats_tag.date.format, |
| 652 | stats_taxonomy: config.stats_tag.taxonomy.active, |
| 653 | taxonomy: config.stats_tag.taxonomy.name, |
| 654 | custom_html: true, |
| 655 | wpp_start: config.markup['wpp-start'], |
| 656 | wpp_end: config.markup['wpp-end'], |
| 657 | post_html: config.markup['post-html'], |
| 658 | theme: value |
| 659 | }); |
| 660 | } else { |
| 661 | setAttributes({ theme: value }); |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | let themes = [ |
| 666 | { |
| 667 | label: __('None', 'wordpress-popular-posts'), |
| 668 | value: '' |
| 669 | }, |
| 670 | ]; |
| 671 | |
| 672 | if ( this.state.themes ) { |
| 673 | for( const theme in this.state.themes ) { |
| 674 | themes.push( |
| 675 | { |
| 676 | label: this.state.themes[theme].json.name, |
| 677 | value: theme |
| 678 | }, |
| 679 | ); |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | return <Fragment> |
| 684 | <p className='not-a-legend'><strong>{__('HTML Markup settings', 'wordpress-popular-posts')}</strong> <small>(<a href="https://github.com/cabrerahector/wordpress-popular-posts/wiki/5.-FAQ#how-can-i-use-my-own-html-markup-with-your-plugin" target="_blank">{__('What is this?', 'wordpress-popular-posts')}</a>)</small></p> |
| 685 | <CheckboxControl |
| 686 | label={__('Use custom HTML Markup', 'wordpress-popular-posts')} |
| 687 | checked={attributes.custom_html} |
| 688 | onChange={(value) => setAttributes({ custom_html: value })} |
| 689 | /> |
| 690 | { attributes.custom_html && |
| 691 | <div className='option-subset'> |
| 692 | <TextareaControl |
| 693 | rows="1" |
| 694 | label={__('Before title', 'wordpress-popular-posts')} |
| 695 | value={attributes.header_start} |
| 696 | onChange={(value) => setAttributes({ header_start: value })} |
| 697 | /> |
| 698 | <TextareaControl |
| 699 | rows="1" |
| 700 | label={__('After title', 'wordpress-popular-posts')} |
| 701 | value={attributes.header_end} |
| 702 | onChange={(value) => setAttributes({ header_end: value })} |
| 703 | /> |
| 704 | <TextareaControl |
| 705 | rows="1" |
| 706 | label={__('Before popular posts', 'wordpress-popular-posts')} |
| 707 | value={attributes.wpp_start} |
| 708 | onChange={(value) => setAttributes({ wpp_start: value })} |
| 709 | /> |
| 710 | <TextareaControl |
| 711 | rows="1" |
| 712 | label={__('After popular posts', 'wordpress-popular-posts')} |
| 713 | value={attributes.wpp_end} |
| 714 | onChange={(value) => setAttributes({ wpp_end: value })} |
| 715 | /> |
| 716 | <TextareaControl |
| 717 | label={__('Post HTML markup', 'wordpress-popular-posts')} |
| 718 | value={attributes.post_html} |
| 719 | onChange={(value) => setAttributes({ post_html: value })} |
| 720 | /> |
| 721 | <small><a href="https://github.com/cabrerahector/wordpress-popular-posts/wiki/2.-Template-tags#content-tags" target="_blank">{__('Content Tags List', 'wordpress-popular-posts')}</a></small> |
| 722 | </div> |
| 723 | } |
| 724 | <SelectControl |
| 725 | label={__('Theme', 'wordpress-popular-posts')} |
| 726 | value={attributes.theme} |
| 727 | options={themes} |
| 728 | onChange={onThemeChange} |
| 729 | /> |
| 730 | </Fragment>; |
| 731 | } |
| 732 | |
| 733 | render() |
| 734 | { |
| 735 | if ( ! this.state.taxonomies || ! this.state.themes || ! this.state.imgSizes ) |
| 736 | return <Spinner />; |
| 737 | |
| 738 | const { isSelected, className, attributes } = this.props; |
| 739 | |
| 740 | let classes = className; |
| 741 | classes += this.state.editMode ? ' in-edit-mode' : ' in-preview-mode'; |
| 742 | classes += isSelected ? ' is-selected' : ''; |
| 743 | |
| 744 | return ([ |
| 745 | this.getBlockControls(), |
| 746 | <div className={classes}> |
| 747 | { this.state.editMode && |
| 748 | <Fragment> |
| 749 | {this.getMainFields()} |
| 750 | {this.getFiltersFields()} |
| 751 | {this.getPostSettingsFields()} |
| 752 | {this.getStatsTagFields()} |
| 753 | {this.getHTMLMarkupFields()} |
| 754 | </Fragment> |
| 755 | } |
| 756 | { ! this.state.editMode && |
| 757 | <Disabled> |
| 758 | <ServerSideRender |
| 759 | block={this.props.name} |
| 760 | className={className} |
| 761 | attributes={attributes} |
| 762 | urlQueryArgs={{isSelected: isSelected}} |
| 763 | /> |
| 764 | </Disabled> |
| 765 | } |
| 766 | </div> |
| 767 | ]); |
| 768 | } |
| 769 | } |
| 770 |