Skip to content

Commit

Permalink
various fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
HardeepAsrani committed Jan 5, 2025
1 parent 9fb538c commit 33e14a9
Show file tree
Hide file tree
Showing 7 changed files with 458 additions and 139 deletions.
19 changes: 16 additions & 3 deletions includes/gutenberg/feedzy-rss-feeds-loop-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,17 @@ public function render_callback( $attributes, $content ) {
*/
public function apply_magic_tags( $content, $item ) {
$pattern = '/\{\{feedzy_([^}]+)\}\}/';
$content = str_replace( FEEDZY_ABSURL . 'img/feedzy.svg', '{{feedzy_image}}', $content );
$content = str_replace(
array(
FEEDZY_ABSURL . 'img/feedzy.svg',
'http://{{feedzy_url}}'
),
array(
'{{feedzy_image}}',
'{{feedzy_url}}'
),
$content
);

return preg_replace_callback( $pattern, function( $matches ) use ( $item ) {
return isset( $matches[1] ) ? $this->get_value( $matches[1], $item ) : '';
Expand Down Expand Up @@ -210,8 +220,11 @@ public function get_value( $key, $item ) {
$item_date = isset( $item['item_date'] ) ? wp_date( get_option( 'time_format' ), $item['item_date'] ) : '';
return $item_date;
case 'datetime':
$item_date = isset( $item['item_date'] ) ? wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $item['item_date'] ) : '';
return $item_date;
$item_date = isset( $item['item_date'] ) ? wp_date( get_option( 'date_format' ), $item['item_date'] ) : '';
$item_time = isset( $item['item_date'] ) ? wp_date( get_option( 'time_format' ), $item['item_date'] ) : '';
/* translators: 1: date, 2: time */
$datetime = sprintf( __( '%1$s at %2$s', 'feedzy-rss-feeds' ), $item_date, $item_time );
return $datetime;
case 'author':
if ( isset( $item['item_author'] ) && is_string( $item['item_author'] ) ) {
return $item['item_author'];
Expand Down
156 changes: 156 additions & 0 deletions js/FeedzyLoop/components/FeedControl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/**
* WordPress dependencies.
*/
import { useState, useEffect, useRef } from '@wordpress/element';

const FeedControl = ({ value, options, onChange }) => {
const [isOpen, setIsOpen] = useState(false);
const [inputValue, setInputValue] = useState('');
const [selectedOption, setSelectedOption] = useState(null);
const dropdownRef = useRef(null);

// Initialize component state based on value prop
useEffect(() => {
if (value?.type === 'group' && value.source) {
const selected = options.find((opt) => opt.value === value.source);
setSelectedOption(selected || null);
setInputValue('');
} else if (value?.type === 'url' && Array.isArray(value.source)) {
setSelectedOption(null);
setInputValue(value.source.join(', '));
}
}, [value, options]);

useEffect(() => {
const handleClickOutside = (event) => {
if (
dropdownRef.current &&
!dropdownRef.current.contains(event.target)
) {
setIsOpen(false);
}
};

document.addEventListener('mousedown', handleClickOutside);
return () =>
document.removeEventListener('mousedown', handleClickOutside);
}, []);

const handleSelectOption = (option) => {
setSelectedOption(option);
setInputValue('');
setIsOpen(false);
onChange({
type: 'group',
source: option.value,
});
};

const handleInputChange = (e) => {
const current = e.target.value;
setInputValue(current);
setSelectedOption(null);
};

const handleInputBlur = () => {
onChange({
type: 'url',
source: inputValue
? inputValue
.split(',')
.map((url) => url.trim())
.filter(Boolean)
: [],
});
};

const handleClear = () => {
setSelectedOption(null);
setInputValue('');
onChange({
type: 'url',
source: [],
});
};

return (
<div className="fz-url-category-input" ref={dropdownRef}>
<input
type="text"
value={selectedOption ? selectedOption.label : inputValue}
onChange={handleInputChange}
onBlur={handleInputBlur}
placeholder="Enter URLs or select a category"
disabled={selectedOption !== null}
className="fz-input-field"
/>
<div className="fz-buttons-container">
{selectedOption && (
<button
onClick={handleClear}
className="fz-clear-button"
title="Clear selection"
>
<svg
width="14"
height="14"
viewBox="0 0 24 24"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M18 6L6 18M6 6l12 12"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
</button>
)}
<button
onClick={() => setIsOpen(!isOpen)}
className="fz-dropdown-button"
>
<svg
width="12"
height="12"
viewBox="0 0 12 12"
fill="none"
xmlns="http://www.w3.org/2000/svg"
style={{
transform: isOpen
? 'rotate(180deg)'
: 'rotate(0deg)',
transition: 'transform 0.2s',
}}
>
<path
d="M2 4L6 8L10 4"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
</button>
</div>

{isOpen && (
<div className="fz-dropdown-menu">
{options.map((option) => (
<button
key={option.value}
onClick={() => handleSelectOption(option)}
className={`fz-dropdown-item ${selectedOption?.value === option.value ? 'fz-selected' : ''}`}
>
{option.label}
</button>
))}
</div>
)}
</div>
);
};

export default FeedControl;
37 changes: 28 additions & 9 deletions js/FeedzyLoop/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,18 +170,37 @@ const Controls = ({
]}
initialOpen={false}
key="filters"
className={
window.feedzyData.isPro
? 'feedzy-item-filter'
: 'feedzy-item-filter fz-locked'
}
className="feedzy-item-filter"
>
{!window.feedzyData.isPro && (
<div className="fz-upsell-notice">
{__(
'Unlock this feature and more advanced options with',
'feedzy-rss-feeds'
)}{' '}
<ExternalLink href="https://themeisle.com/plugins/feedzy-rss-feeds/upgrade/?utm_source=wpadmin&utm_medium=blockeditor&utm_campaign=keywordsfilter&utm_content=feedzy-rss-feeds">
{__('Feedzy Pro', 'feedzy-rss-feeds')}
</ExternalLink>
</div>
)}

<ConditionsControl
conditions={
attributes?.conditions || {
conditions: [],
match: 'all',
}
window.feedzyData.isPro
? attributes?.conditions || {
conditions: [],
match: 'all',
}
: {
match: 'all',
conditions: [
{
field: 'title',
operator: 'contains',
value: 'Sports',
},
],
}
}
setConditions={(conditions) => {
setAttributes({ conditions });
Expand Down
17 changes: 15 additions & 2 deletions js/FeedzyLoop/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import {
InnerBlocks,
} from '@wordpress/block-editor';

import {
Placeholder as BlockEditorPlaceholder,
Spinner,
} from '@wordpress/components';

import { useDispatch, useSelect } from '@wordpress/data';

import { useState } from '@wordpress/element';
Expand All @@ -30,13 +35,20 @@ import Controls from './controls';

const { name } = metadata;

const LoadingResponsePlaceholder = () => (
<BlockEditorPlaceholder>
<Spinner />
</BlockEditorPlaceholder>
);

const Edit = ({ attributes, setAttributes, clientId }) => {
const blockProps = useBlockProps();

const [isEditing, setIsEditing] = useState(!attributes?.feed?.source);
const [isPreviewing, setIsPreviewing] = useState(false);

const { replaceInnerBlocks, selectBlock } = useDispatch(blockEditorStore);
const { clearSelectedBlock, replaceInnerBlocks, selectBlock } =
useDispatch(blockEditorStore);

const isSelected = useSelect(
(select) => {
Expand Down Expand Up @@ -130,6 +142,7 @@ const Edit = ({ attributes, setAttributes, clientId }) => {
...attributes,
innerBlocksContent,
}}
LoadingResponsePlaceholder={LoadingResponsePlaceholder}
/>
</div>
</>
Expand Down Expand Up @@ -165,8 +178,8 @@ const Edit = ({ attributes, setAttributes, clientId }) => {
),
true
);
clearSelectedBlock();
}
selectBlock(clientId);
}}
/>
)}
Expand Down
Loading

0 comments on commit 33e14a9

Please sign in to comment.