Skip to content

Commit

Permalink
compile changes
Browse files Browse the repository at this point in the history
  • Loading branch information
picocodes committed Jan 30, 2024
1 parent 80f48b0 commit 9caaa00
Show file tree
Hide file tree
Showing 14 changed files with 264 additions and 116 deletions.
6 changes: 5 additions & 1 deletion build/Emails/Admin/Editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public static function load( $edited_campaign ) {
array(
'description' => $data['description'],
'mergeTag' => $tag,
'name' => 'merge-tag/' . preg_replace( '/[^a-z0-9\-]/', '-', strtolower( $tag ) ),
'name' => self::merge_tag_to_block_name( $tag ),
),
$data['block']
);
Expand Down Expand Up @@ -244,6 +244,10 @@ public static function load( $edited_campaign ) {
add_filter( 'admin_body_class', array( __CLASS__, 'add_block_editor_body_class' ) );
}

public static function merge_tag_to_block_name( $merge_tag ) {
return preg_replace( '/[^a-z0-9\-]/', '-', strtolower( $merge_tag ) );
}

public static function add_block_editor_body_class( $classes ) {
$classes .= ' block-editor-page is-fullscreen-mode';
return $classes;
Expand Down
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' => '1cf0db13f4a15f3a16ac');
<?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' => 'f6f43ec58565b3d941af');
31 changes: 15 additions & 16 deletions build/Emails/assets/js/email-editor.js

Large diffs are not rendered by default.

19 changes: 9 additions & 10 deletions includes/emails/class-generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,8 @@ private function remove_unused_classes_and_ids( $html ) {
preg_match_all( '/#([a-z0-9_-]+)/i', $html, $ids );

// Convert the arrays to associative arrays for faster lookup.
$classes = array_flip( $classes[1] );
$ids = array_flip( $ids[1] );
$all_classes = array_flip( $classes[1] );
$ids = array_flip( $ids[1] );

// Load the HTML.
$doc = new DOMDocument();
Expand Down Expand Up @@ -445,20 +445,19 @@ private function remove_unused_classes_and_ids( $html ) {
$class = $element->getAttribute( 'class' );

// Split the class attribute.
$classes = empty( $class ) ? array() : explode( ' ', $class );
$has_class = false;
$classes = empty( $class ) ? array() : explode( ' ', $class );
$styled = array();

// Loop over all classes.
foreach ( $classes as $class ) {
if ( isset( $classes[ $class ] ) ) {
// Class is used, stop searching.
$has_class = true;
break;
if ( isset( $all_classes[ $class ] ) ) {
$styled[] = $class;
}
}

if ( ! $has_class ) {
// Class is not used, remove it.
if ( 0 < count( $styled ) ) {
$element->setAttribute( 'class', implode( ' ', $styled ) );
} else {
$element->removeAttribute( 'class' );
}
}
Expand Down
36 changes: 36 additions & 0 deletions src/Objects/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -501,4 +501,40 @@ protected function prepare_item( $item ) {
protected function cleanup_item( $previous_item ) {
$this->current_item = $previous_item;
}

protected function meta_key_tag_config() {
return array(
'label' => __( 'Meta Value', 'newsletter-optin-box' ),
'type' => 'string',
'example' => 'key="my_key"',
'skip_smart_tag' => true,
'block' => array(
'title' => sprintf(
/* translators: %s: object type label */
__( '%s Meta', 'newsletter-optin-box' ),
$this->singular_label
),
'description' => __( 'Displays a custom field value.', 'newsletter-optin-box' ),
'icon' => 'ellipsis',
'metadata' => array(
'ancestor' => array( $this->context ),
),
'element' => 'div',
'settings' => array(
'key' => array(
'label' => __( 'Meta Key / Field Key', 'newsletter-optin-box' ),
'el' => 'input',
'type' => 'text',
'description' => __( 'The meta key or field key to display.', 'newsletter-optin-box' ),
),
'default' => array(
'label' => __( 'Default Value', 'newsletter-optin-box' ),
'el' => 'input',
'type' => 'text',
'description' => __( 'The default value to display if the meta key is not set.', 'newsletter-optin-box' ),
),
),
),
);
}
}
53 changes: 42 additions & 11 deletions src/Objects/Generic_Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function exists() {
* @param string $field The field.
* @return mixed $value The value.
*/
public function get( $field ) {
public function get( $field, $args = array() ) {

if ( ! $this->exists() ) {
return null;
Expand All @@ -57,13 +57,7 @@ public function get( $field ) {
// Check if string begins with tax_.
if ( 0 === strpos( $field, 'tax_' ) ) {
$taxonomy = substr( $field, 4 );
$terms = wp_get_post_terms( $this->external->ID, $taxonomy, array( 'fields' => 'names' ) );

if ( is_array( $terms ) && ! empty( $terms ) ) {
return implode( ', ', $terms );
}

return '';
return self::prepare_terms( $this->external->ID, $taxonomy, ! empty( $args['link'] ) );
}

// ID.
Expand Down Expand Up @@ -108,13 +102,19 @@ public function get( $field ) {

// Featured image URL.
if ( 'featured_image' === strtolower( $field ) ) {
$url = get_the_post_thumbnail_url( $this->external );
$image_size = ! empty( $args['image_size'] ) ? $args['image_size'] : 'thumbnail';
$url = get_the_post_thumbnail_url( $this->external, $image_size );
return $url ? $url : '';
}

// Meta.
if ( 0 === strpos( $field, 'meta.' ) ) {
$field = substr( $field, 5 );
if ( 'meta' === $field ) {
$field = isset( $args['key'] ) ? $args['key'] : null;
}

// Abort if no field.
if ( empty( $field ) ) {
return null;
}

return get_post_meta( $this->external->ID, $field, true );
Expand All @@ -141,4 +141,35 @@ protected function filter_content( $content ) {

return $content;
}

public static function prepare_terms( $id, $taxonomy, $link ) {
/** @var \WP_Term[] $terms */
$terms = wp_get_post_terms( $id, $taxonomy );

if ( empty( $terms ) || ! is_array( $terms ) ) {
return '';
}

$prepared = array();

foreach ( $terms as $term ) {

if ( empty( $term ) ) {
continue;
}

if ( $link ) {
$term_url = get_term_link( $term );

if ( ! is_wp_error( $term_url ) ) {
$prepared[] = sprintf( '<a href="%s">%s</a>, ', $term_url, esc_html( $term->name ) );
continue;
}
}

$prepared[] = sprintf( '<span>%s</span>, ', esc_html( $term->name ) );
}

return implode( ', ', $prepared );
}
}
31 changes: 18 additions & 13 deletions src/Objects/Generic_Post_Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,8 @@ public function get_all( $filters ) {
*/
public function get_fields() {

$fields = array(
$is_visible = is_post_type_viewable( $this->type );
$fields = array(
'id' => array(
'label' => __( 'ID', 'newsletter-optin-box' ),
'type' => 'number',
Expand Down Expand Up @@ -414,7 +415,7 @@ public function get_fields() {
'ancestor' => array( $this->context ),
),
'element' => 'heading',
'linksTo' => $this->field_to_merge_tag( 'url' ),
'linksTo' => $is_visible ? $this->field_to_merge_tag( 'url' ) : null,
),
),
'excerpt' => array(
Expand Down Expand Up @@ -498,17 +499,21 @@ public function get_fields() {
),
'defaults' => array(
'alt' => $this->field_to_merge_tag( 'name' ),
'href' => $this->field_to_merge_tag( 'url' ),
'href' => $is_visible ? $this->field_to_merge_tag( 'url' ) : '',
),
'element' => 'image',
'settings' => array(
'size' => array(
'label' => __( 'Image Size', 'newsletter-optin-box' ),
'el' => 'image_size_select',
'description' => __( 'Select the image size to display.', 'newsletter-optin-box' ),
'placeholder' => __( 'Select image size', 'newsletter-optin-box' ),
'default' => 'thumbnail',
),
),
),
),
'meta' => array(
'label' => __( 'Meta Value', 'newsletter-optin-box' ),
'type' => 'string',
'example' => 'key="my_key"',
'skip_smart_tag' => true,
),
'meta' => $this->meta_key_tag_config(),
);

$taxonomies = wp_list_pluck(
Expand Down Expand Up @@ -556,7 +561,7 @@ public function get_fields() {
unset( $fields['featured_image'] );
}

if ( ! is_post_type_viewable( $this->type ) ) {
if ( ! $is_visible ) {
unset( $fields['url'] );
}

Expand All @@ -570,19 +575,19 @@ protected function get_list_shortcode_template() {
$template = array();

if ( post_type_supports( $this->type, 'title' ) ) {
$template['heading'] = $this->field_to_merge_tag( 'title' );
$template['heading'] = \Hizzle\Noptin\Emails\Admin\Editor::merge_tag_to_block_name( $this->field_to_merge_tag( 'title' ) );
}

if ( post_type_supports( $this->type, 'editor' ) ) {
$template['description'] = $this->field_to_merge_tag( 'excerpt' );
}

if ( post_type_supports( $this->type, 'thumbnail' ) ) {
$template['image'] = $this->field_to_merge_tag( 'image' );
$template['image'] = \Hizzle\Noptin\Emails\Admin\Editor::merge_tag_to_block_name( $this->field_to_merge_tag( 'featured_image' ) );
}

if ( ! is_post_type_viewable( $this->type ) ) {
$template['action_url'] = $this->field_to_merge_tag( 'url' );
$template['button'] = \Hizzle\Noptin\Emails\Admin\Editor::merge_tag_to_block_name( $this->field_to_merge_tag( 'url' ) );
}
return $template;
}
Expand Down
74 changes: 47 additions & 27 deletions src/Objects/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ public function exists() {
* Retrieves a given field's value.
*
* @param string $field The field.
* @param array $args The arguments.
* @return mixed $value The value.
*/
public function get( $field ) {
public function get( $field, $args = array() ) {

if ( ! $this->exists() ) {
return null;
Expand All @@ -62,51 +63,34 @@ public function get( $field ) {

// categories.
if ( 'categories' === strtolower( $field ) ) {
$categories = $this->external->get_category_ids();

// If we have categories, return category names.
if ( is_array( $categories ) ) {
$categories = array_map( 'get_term_by', array_fill( 0, count( $categories ), 'id' ), $categories, array_fill( 0, count( $categories ), 'product_cat' ) );
$categories = array_map( 'wp_list_pluck', array_fill( 0, count( $categories ), 'name' ), $categories );
$categories = array_map( 'reset', $categories );
}

return is_array( $categories ) ? implode( ', ', array_unique( $categories ) ) : '';
return $this->prepare_terms( $this->external->get_category_ids(), 'product_cat', ! empty( $args['link'] ) );
}

// tags.
if ( 'tags' === strtolower( $field ) ) {
$tags = $this->external->get_tag_ids();

// If we have tags, return tag names.
if ( is_array( $tags ) ) {
$tags = array_map( 'get_term_by', array_fill( 0, count( $tags ), 'id' ), $tags, array_fill( 0, count( $tags ), 'product_tag' ) );
$tags = array_map( 'wp_list_pluck', array_fill( 0, count( $tags ), 'name' ), $tags );
$tags = array_map( 'reset', $tags );
}

return is_array( $tags ) ? implode( ', ', array_unique( $tags ) ) : '';
return $this->prepare_terms( $this->external->get_tag_ids(), 'product_tag', ! empty( $args['link'] ) );
}

// URL.
if ( 'url' === strtolower( $field ) ) {
return get_permalink( $this->external );
return $this->external->get_permalink();
}

// Image url.
if ( 'image' === strtolower( $field ) ) {
$image_size = ! empty( $args['image_size'] ) ? $args['image_size'] : 'woocommerce_thumbnail';
if ( $this->external->get_image_id() ) {
return wp_get_attachment_url( $this->external->get_image_id() );
return wp_get_attachment_image_url( $this->external->get_image_id(), $image_size );
}

if ( $this->external->get_parent_id() ) {
$parent_product = wc_get_product( $this->external->get_parent_id() );
if ( $parent_product && $parent_product->get_image_id() ) {
return wp_get_attachment_url( $parent_product->get_image_id() );
return wp_get_attachment_image_url( $parent_product->get_image_id(), $image_size );
}
}

return wc_placeholder_img_src();
return wc_placeholder_img_src( $image_size );
}

// Check if we have a method get_$field.
Expand All @@ -126,10 +110,46 @@ public function get( $field ) {
}

// Meta.
if ( 0 === strpos( $field, 'meta.' ) ) {
$field = substr( $field, 5 );
if ( 'meta' === $field ) {
$field = isset( $args['key'] ) ? $args['key'] : null;
}

// Abort if no field.
if ( empty( $field ) ) {
return null;
}

return get_post_meta( $this->external->ID, $field, true );
}

private function prepare_terms( $term_ids, $taxonomy, $link ) {

if ( empty( $term_ids ) || ! is_array( $term_ids ) ) {
return '';
}

$terms = array_map( 'get_term_by', array_fill( 0, count( $term_ids ), 'id' ), $term_ids, array_fill( 0, count( $term_ids ), $taxonomy ) );
$prepared = array();

/** @var \WP_Term $term */
foreach ( $terms as $term ) {

if ( empty( $term ) ) {
continue;
}

if ( $link ) {
$term_url = get_term_link( $term );

if ( ! is_wp_error( $term_url ) ) {
$prepared[] = sprintf( '<a href="%s">%s</a>, ', $term_url, esc_html( $term->name ) );
continue;
}
}

$prepared[] = sprintf( '<span>%s</span>, ', esc_html( $term->name ) );
}

return implode( ', ', $prepared );
}
}
Loading

0 comments on commit 9caaa00

Please sign in to comment.