Skip to content

Commit

Permalink
updates posts shortcode
Browse files Browse the repository at this point in the history
  • Loading branch information
picocodes committed Jan 12, 2024
1 parent 767e595 commit 102b4a4
Show file tree
Hide file tree
Showing 12 changed files with 226 additions and 55 deletions.
2 changes: 1 addition & 1 deletion build/Emails/assets/js/email-editor.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('lodash', 'react', 'wp-api-fetch', 'wp-autop', 'wp-blob', 'wp-block-editor', 'wp-blocks', 'wp-commands', 'wp-components', 'wp-compose', 'wp-core-data', 'wp-data', 'wp-date', 'wp-deprecated', 'wp-dom', 'wp-editor', 'wp-element', 'wp-format-library', 'wp-hooks', 'wp-html-entities', 'wp-i18n', 'wp-keyboard-shortcuts', 'wp-keycodes', 'wp-media-utils', 'wp-notices', 'wp-plugins', 'wp-preferences', 'wp-primitives', 'wp-rich-text', 'wp-url'), 'version' => '46b49db24e1bda579d52');
<?php return array('dependencies' => array('lodash', 'react', 'wp-api-fetch', 'wp-autop', 'wp-blob', 'wp-block-editor', 'wp-blocks', 'wp-commands', 'wp-components', 'wp-compose', 'wp-core-data', 'wp-data', 'wp-date', 'wp-deprecated', 'wp-dom', 'wp-editor', 'wp-element', 'wp-format-library', 'wp-hooks', 'wp-html-entities', 'wp-i18n', 'wp-keyboard-shortcuts', 'wp-keycodes', 'wp-media-utils', 'wp-notices', 'wp-plugins', 'wp-preferences', 'wp-primitives', 'wp-rich-text', 'wp-url'), 'version' => 'c4e1c6273f0e4e1248fd');
54 changes: 27 additions & 27 deletions build/Emails/assets/js/email-editor.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion includes/class-noptin-dynamic-content-tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ protected function register() {
'callback' => array( $this, 'get_post_property' ),
'example' => "post property='ID'",
);

}

/**
Expand Down
4 changes: 1 addition & 3 deletions includes/libraries/noptin-com/class-noptin-com-updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public static function load() {
add_filter( 'noptin_email_types', array( __CLASS__, 'add_email_types' ) );
add_filter( 'noptin_email_settings_misc', array( __CLASS__, 'filter_email_settings' ) );
add_filter( 'noptin_automation_sub_types', array( __CLASS__, 'upsell_automation_types' ), 5 );
add_filter( 'noptin_object_can_list', array( __CLASS__, 'can_list' ), PHP_INT_MAX, 2 );
}

/**
Expand Down Expand Up @@ -251,7 +252,6 @@ public static function has_extension_update( $slug ) {
$local_plugin = current( wp_list_filter( Noptin_COM::get_installed_addons(), array( 'slug' => $slug ) ) );

return ! empty( $local_plugin ) && version_compare( $local_plugin['Version'], $update_data[ $slug ]['version'], '<' );

}

/**
Expand Down Expand Up @@ -350,7 +350,6 @@ public static function plugins_api( $response, $action, $args ) {
$new_response[ $git_url ]['slug'] = $args->slug;

return (object) $new_response[ $git_url ];

}

/**
Expand Down Expand Up @@ -388,7 +387,6 @@ public static function need_license_message( $plugin_data, $r ) {
)
);
}

}

/**
Expand Down
95 changes: 87 additions & 8 deletions src/Objects/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ abstract class Collection {
*/
public $can_list = false;

/**
* @var Record|null $current_item Current item.
*/
public $current_item = null;

/**
* Class constructor.
*/
Expand All @@ -85,7 +90,7 @@ public function __construct() {
// Register shortcode.
if ( $this->can_list ) {
$name = $this->plural_type();
//add_shortcode( "noptin_{$name}_tag", array( $this, 'handle_list_shortcode' ) );
add_shortcode( 'noptin_' . $this->plural_type() . '_list', array( $this, 'handle_list_shortcode' ) );
}
}

Expand Down Expand Up @@ -347,27 +352,101 @@ protected function prepare_query_filter( $filters, $key ) {
* @return string $template The shortcode HTML.
*/
public function handle_list_shortcode( $atts, $template ) {
noptin_dump( $atts ); exit;

$atts = shortcode_atts(
array(
'format' => 'label',
'query' => 'number=10&order=desc&orderby=date',
'columns' => 1,
'skiponempty' => 'no',
'responsive' => 'yes',
),
$atts,
'noptin_' . $this->plural_type() . '_tag'
'noptin_' . $this->plural_type() . '_list'
);

$items = $this->get_all( array() );
parse_str( rawurldecode( html_entity_decode( $atts['query'] ) ), $query );

$items = $this->get_all( $query );

if ( empty( $items ) ) {

if ( 'yes' === $atts['skiponempty'] ) {
$GLOBALS['noptin_email_force_skip'] = true;
}

return '';
}

$html = '';
$html = '<div class="noptin-records__wrapper wp-block-noptin-' . sanitize_html_class( $this->type ) . '-template">';
$post = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null;
$tags = new Tags( $this->type );
$columns = absint( $atts['columns'] );
$rows = ceil( count( $items ) / $columns );

for ( $row = 0; $row < $rows; $row++ ) {
$row_class = 'noptin-records__row';

if ( $columns > 1 ) {
$row_class .= ' noptin-columns';

if ( 'yes' === $atts['responsive'] ) {
$row_class .= ' noptin-is-stacked-on-mobile';
}
}

$html .= '<div class="' . esc_attr( $row_class ) . '">';

for ( $col = 0; $col < $columns; $col++ ) {
$index = $row * $columns + $col;

if ( ! isset( $items[ $index ] ) ) {
continue;
}

// Prepare item.
$item = $items[ $index ];
$this->prepare_item( $item );

// Generate template.
$html .= $tags->replace_record_fields( $this->current_item, $template );

foreach ( $items as $item ) {
$html .= $this->get_shortcode_item( $item, $atts );
// Cleanup item.
$this->cleanup_item( $item );
}

$html .= '</div>';
}

$html .= '</div>';

// Restore post.
if ( 'post_type' === $this->object_type ) {
if ( ! empty( $post ) ) {
$GLOBALS['post'] = $post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
setup_postdata( $post );
} else {
wp_reset_postdata();
}
}

return $html;
}

/**
* Prepares a single item.
*
* @param int $item The item.
*/
protected function prepare_item( $item ) {
$this->current_item = $this->get( $item );
}

/**
* Cleans up after a single item.
*
* @param Record|null $previous_item The item.
*/
protected function cleanup_item( $previous_item ) {
$this->current_item = $previous_item;
}
}
2 changes: 1 addition & 1 deletion src/Objects/Generic_Post_Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ public function get_all( $filters ) {
// If date query is specified, ensure it is enabled.
$filters = $this->prepare_query_filter( $filters, 'date_query' );

return get_posts( $filters );
return get_posts( array_filter( $filters ) );
}

/**
Expand Down
14 changes: 14 additions & 0 deletions src/Objects/Post_Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,18 @@ public function get_test_object_id( $rule ) {
)
);
}

/**
* Prepares a single item.
*
* @param int $item The item.
*/
protected function prepare_item( $item ) {
$this->current_item = $this->get( $item );

if ( $this->current_item->exists() ) {
$GLOBALS['post'] = $this->current_item->external;
setup_postdata( $this->current_item->external );
}
}
}
2 changes: 1 addition & 1 deletion src/Objects/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public static function handle_field_smart_tag( $args, $field, $config = array()
return '';
}

// If object type has a dot, it's a prefix, remove id.
// If object type has a dot, it's a prefix, remove it.
if ( false !== strpos( $config['object_type'], '.' ) ) {
$field = substr( $field, strlen( $config['object_type'] ) + 1 );
} else {
Expand Down
72 changes: 72 additions & 0 deletions src/Objects/Tags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Hizzle\Noptin\Objects;

defined( 'ABSPATH' ) || exit;

/**
* Allows users to use dynamic tags in shortcodes.
*
* @internal
* @access private
* @since 3.0.0
* @ignore
*/
class Tags extends \Noptin_Dynamic_Content_Tags {

/**
* @var string $object_type The object type for the tags.
*/
private $object_type;

/**
* Parses a record's tags.
*
* @param string $object_type The object type.
* @param array $fields
*/
public function __construct( $object_type ) {
$this->object_type = $object_type;
$this->tags = Store::smart_tags( $object_type, true );
}

/**
* @param Record $record The record.
* @param string $content The content containing dynamic content tags.
* @param string $escape_function Escape mode for the replacement value. Leave empty for no escaping.
* @return string
*/
public function replace_record_fields( $record, $content, $escape_function = 'wp_kses_post' ) {
global $noptin_current_objects;

if ( ! is_string( $content ) || empty( $content ) ) {
return $content;
}

// Store the current record.
if ( ! is_array( $noptin_current_objects ) ) {
$noptin_current_objects = array();
}

$old_record = isset( $noptin_current_objects[ $this->object_type ] ) ? $noptin_current_objects[ $this->object_type ] : null;

$noptin_current_objects[ $this->object_type ] = $record;

$this->escape_function = $escape_function;

// Replace strings like this: [[tagname attr="value"]].
$content = preg_replace_callback( '/\[\[([\w\.\/-]+)(\ +(?:(?!\[)[^\]\n])+)*\]\]/', array( $this, 'replace_tag' ), $content );

// Call again to take care of nested variables.
$content = preg_replace_callback( '/\[\[([\w\.\/-]+)(\ +(?:(?!\[)[^\]\n])+)*\]\]/', array( $this, 'replace_tag' ), $content );

// Restore the old record.
if ( null === $old_record ) {
unset( $noptin_current_objects[ $this->object_type ] );
} else {
$noptin_current_objects[ $this->object_type ] = $old_record;
}

return $content;
}
}
20 changes: 20 additions & 0 deletions templates/email-templates/email-styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,26 @@
border-radius: 6px;
}

.noptin-columns {
display: table;
width: 100%;
}

.noptin-column {
display: table-cell;
}

@media only screen and (max-width: 360px) {
.noptin-is-stacked-on-mobile {
display: block !important;
}

.noptin-is-stacked-on-mobile.noptin-column {
vertical-align: top !important;
width: 100% !important;
}
}

<?php
// Note that esc_html() cannot be used because `div &gt; span` is not interpreted properly.
echo strip_tags( get_noptin_option( 'custom_css', '' ) ); // phpcs:ignore
Expand Down
14 changes: 2 additions & 12 deletions templates/email-templates/noptin-visual/styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
line-height: <?php echo esc_attr( $settings['line_height'] ); ?>;
font-weight: <?php echo esc_attr( $settings['font_weight'] ); ?>;
font-style: <?php echo esc_attr( $settings['font_style'] ); ?>;
word-wrap: break-word;
word-break: break-all;
}

div,
Expand All @@ -57,18 +59,6 @@
color: <?php echo esc_attr( $settings['button_color'] ); ?>;
}

@media only screen and (max-width: 360px) {
.wp-block-noptin-columns.is-stacked-on-mobile {
display: block !important;
}

.wp-block-noptin-columns.is-stacked-on-mobile .wp-block-noptin-column {
display: block !important;
vertical-align: top !important;
width: 100% !important;
}
}

/**
* Collapse table borders to avoid space between cells.
*/
Expand Down
1 change: 0 additions & 1 deletion templates/post-digests/email-posts-grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

defined( 'ABSPATH' ) || exit;

// TODO: Update post digest guide and show how to modify the posts list and grid.
?>

<style type="text/css">
Expand Down

0 comments on commit 102b4a4

Please sign in to comment.