Skip to content

Commit

Permalink
Merge branch 'blocks' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
everaldomatias committed Apr 29, 2024
2 parents ca5d11d + 4c3ee91 commit a17462d
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 7 deletions.
16 changes: 13 additions & 3 deletions themes/midia-ninja-theme/library/blocks/src/high-spot/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "ninja/high-spot",
"version": "0.0.2",
"version": "0.0.3",
"title": "Destaque principal",
"category": "widgets",
"icon": "format-image",
"description": "Exibe uma imagem de destaque com texto sobreposto.",
"description": "Exibe uma imagem de destaque com texto sobreposto, a partir de um post específico ou o último de um termo.",
"attributes": {
"blockId": {
"type": "string"
Expand Down Expand Up @@ -42,7 +42,17 @@
"postId": {
"type": "number",
"default": 0
}
},
"postType": {
"type": "string",
"default": "post"
},
"queryTerms": {
"type": "array"
},
"taxonomy": {
"type": "string"
}
},
"supports": {
"html": false,
Expand Down
72 changes: 69 additions & 3 deletions themes/midia-ninja-theme/library/blocks/src/high-spot/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import { __ } from '@wordpress/i18n'
const { useEffect, useState } = wp.element

import ServerSideRender from '@wordpress/server-side-render'
import apiFetch from '@wordpress/api-fetch'
import { useBlockProps, InspectorControls } from '@wordpress/block-editor'
import ImageSelector from './components/ImageSelector'
import LinkSelector from './components/LinkSelector'
import SelectPostType from "../../shared/components/SelectPostType"
import SelectTerms from "../../shared/components/SelectTerms"
import SelectPost from './components/SelectPost'

import {
Expand Down Expand Up @@ -36,9 +39,11 @@ export default function Edit( { attributes, clientId, setAttributes } ) {
imageAlt,
imageId,
imageUrl,
linkUrl,
postId,
tag
postType,
queryTerms,
tag,
taxonomy
} = attributes

useEffect(() => {
Expand All @@ -51,6 +56,33 @@ export default function Edit( { attributes, clientId, setAttributes } ) {
setAttributes({ postId: value })
}

const onChangePostType = ( value ) => {
setAttributes( { postType: value } )
setAttributes( { taxonomy: '' } )
setAttributes( { queryTerms: [] } )
}

const onChangeSelectTerm = ( value ) => {
setAttributes( { queryTerms: value.length > 0 ? value : undefined } )
}

const onChangeTaxonomy = ( value ) => {
setAttributes( { taxonomy: value } )
setAttributes( { queryTerms: [] } )
}

// Get taxonomies from the post type selected
const [taxonomies, setTaxonomies] = useState([])

useEffect(() => {
if (postType) {
apiFetch({ path: `/ninja/v1/taxonomias/${postType}` })
.then(taxonomies => {
setTaxonomies(taxonomies)
})
}
}, [postType])

return (
<>
<InspectorControls>
Expand All @@ -65,9 +97,13 @@ export default function Edit( { attributes, clientId, setAttributes } ) {
value={blockModel}
options={[
{
label: __( 'Post', 'ninja' ),
label: __( 'Specific post', 'ninja' ),
value: "post"
},
{
label: __( 'Latest post', 'ninja' ),
value: "latest"
},
{
label: __( 'Manual', 'ninja' ),
value: "manual"
Expand All @@ -85,6 +121,36 @@ export default function Edit( { attributes, clientId, setAttributes } ) {
</>
) }

{ ( blockModel === 'latest' ) && (
<>
<PanelRow>
<SelectPostType postType={postType} onChangePostType={onChangePostType} />
</PanelRow>

<PanelRow>
<SelectControl
label={ __( 'Taxonomy to filter', 'ninja' ) }
value={ taxonomy }
options={ taxonomies.map( taxonomy => ( {
label: taxonomy.label,
value: taxonomy.value
}))}
onChange={ onChangeTaxonomy }
help={ __(
'Leave blank to not filter by taxonomy',
'ninja'
) }
/>
</PanelRow>

{ taxonomy && (
<PanelRow>
<SelectTerms onChangeSelectTerm={ onChangeSelectTerm } selectedTerms={ queryTerms } taxonomy={ taxonomy } />
</PanelRow>
) }
</>
) }

{ ( blockModel ==='manual' ) && (
<>
<PanelRow>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,40 @@ function high_spot_callback( $attributes ) {

$block_classes[] = 'high-spot-block';
$block_classes[] = 'model-' . $block_model;

if ( $block_model === 'latest' ) {
$attributes['postsToShow'] = 1;
$args = build_posts_query( $attributes );

$get_posts = get_posts( $args );

if ( isset( $get_posts[0] ) ) {
$get_post = $get_posts[0];
$post_id = $get_post->ID;
$description = isset( $get_post->post_excerpt ) ? $get_post->post_excerpt : '';
$heading = isset( $get_post->post_title ) ? apply_filters( 'the_title', $get_post->post_title ) : '';
$link_url = esc_url( get_permalink( $post_id ) );
$image_id = has_post_thumbnail( $post_id ) ? get_post_thumbnail_id( $post_id ) : '';
$image_url = has_post_thumbnail( $post_id ) ? get_the_post_thumbnail_url( $post_id, 'large' ) : get_stylesheet_directory_uri() . '/assets/images/default-image.png';
$image_alt = get_post_meta( $image_id, '_wp_attachment_image_alt', true );
$primary_category = get_post_meta( $post_id, '_yoast_wpseo_primary_category', true );

$get_term = '';

if ( $primary_category ) {
$get_term = get_term( $primary_category, 'category' );
$tag = ! empty( $get_term->name ) ? $get_term->name : '';
} else {
$get_terms = get_the_terms( $post_id, 'category' );
if ( $get_terms ) {
$tag = ( ! empty( $get_term[0] ) && ! empty( $get_term[0]->name ) ) ? $get_term[0]->name : '';
}
}
} else {
return;
}
}

if ( $block_model === 'post' ) {

if(is_archive()){
Expand All @@ -34,7 +68,7 @@ function high_spot_callback( $attributes ) {

if ( $post_ids && is_array( $post_ids ) ) {
$post_id = $post_ids[0];
}
}
}

if(!$post_id){
Expand Down

0 comments on commit a17462d

Please sign in to comment.