WordPress Custom Email Trigger - wordpress

I'm using Crocoblocks/JetReviews to add reviews to my site (on a custom post type).
I can see there is a setting to require admin approval for it to be published, but I want to trigger an email to one of the admins when a review is submitted, and I don't see any settings for that so I'll have to do it myself.
I think I can handle the function to send the email, but can't sort out what to hook into or trigger it against?
Using something like this:
if ( function_exists( 'notification' ) ) :
notification( [
'title' => 'Admin Notification for Review Approval', // For internal reference.
'trigger' => 'trigger_slug', // Trigger slug (can be a Triggerable object).
'carriers' => [ // An array with format: carrier_slug => data array
'email' => [
'activated' => true, // Must be true.
'enabled' => true, // Must be true.
'subject' => 'A New Review Needs Approval',
'body' => 'There is a new review awaiting your approval.',
'recipients' => [
[
'type' => 'administrator',
'recipient' => 'admin#adminemail.com',
],
],
],
],
'enabled' => true, // Must be true.
] );
endif;
But right now that's not connected to any action...and that's where I'm lost.
Any thoughts of where to look? I couldn't figure it out from their docs so hoping someone here might have an idea of where to start at least.
Thanks!
Chad

Related

Drupal 9 How to add pager with entityQuery

We've a Drupal 9 installation and are trying to add a pager using the pagerer module for articles entityQuery, the aim is to list tagged articles in a tag page, but it’s not working. It returns null.
When we dump the data without the pager, using default drupal query, it returns the data of all tagged articles properly.
The code is added in the theme file themeName_preprocess_page hook and being called in page--page.html.twig template file.
Here’s our code:
$query = \Drupal::entityQuery('node')
->condition('status', 1)
->condition('type', 'article');
->pager(2);
$nids = $query->sort('created', 'DESC')
->execute();
if($nids):
$nodesNews = \Drupal\node\Entity\Node::loadMultiple($nids);
$pathNews = base_path();
$pager = [
'articles_data' => $nodesNews,
'results' => [
'#theme' => 'news_pagination',
'#items' => $nodesNews,
'#path' => $pathNews,
'#tag' => $tag
],
'pager' => [
'#type' => 'pager',
'#quantity' => 5
],
];
return $pager;
endif;
And here is the code that calls the query:
<div>
{{ articles_data }}
{{ pager }}
</div>
The above code returns only one page in the navigation and we’ve 10 articles, so given that we set 2 articles per page, the output should be 5 pages instead of 1.
Also articles_data attribute returns null. Could you please help me to find what’s wrong with the code? Happy to share more information as needed, thank you.
Just reading the docs for this module here,
it would seem that you are missing at least the #theme and #style keys in your render array for the pager.
A more likely to succeed version of the above render array would be
$pager = [
'articles_data' => $nodesNews,
'results' => [
'#theme' => 'news_pagination',
'#items' => $nodesNews,
'#path' => $pathNews,
'#tag' => $tag
],
'pager' => [
'#type' => 'pager',
'#theme' => 'pagerer_base',
'#style' => 'standard',
'#config' => [
'display_restriction' => 0,
],
'#quantity' => 5
],
];

WooCommerce Shipping Plugin - Checkout Shipping Cost Not Updating(AJAX)

I've searched to find any solution but nothing worked.
Here what I want to do:
Add new field in checkout page called Shipping show as selectbox filled with Preorder and Non-preorder
If preorder then would show Location field show as selectbox
If non-preorder then would show Location field same as step 2 but different list option.
When Shipping and Location filled correctly, shipping cost would show with correct shipping cost.
Problem
When I filled all required field and Shipping, Location shipping cost wound calculate again. It do AJAX request, but shipping cost keep the same.
But, when I change Name or Street Address shipping cost updated correctly, which is bad behavior. When user click Place Order immediately would POST wrong shipping cost.
Here my Youtube Video to make clear what I ask for help.
What I did to create my own plugin:
Create custom field using hook something like this
public function woocommerce_checkout_fields( $fields )
{
// var_dump(have_rows('location', 'option'));
// die('test');
$options = [];
$options[''] = 'Select Shipping';
$options['pre-order'] = 'Pre Order';
if (!$this->hasPreorderItem()) {
$options['non-pre-order'] = 'Non Pre Order';
}
// Add custom billing shipping
$fields['billing']['billing_shipping'] = array (
'type' => 'select',
'label' => 'Shipping',
'placeholder' => 'Select shipping',
'class' => array ('address-field', 'update_totals_on_change' ),
'required' => true,
'options' => $options
// 'options' => $this->getLocations()
);
$fields['billing']['billing_location_pre_order'] = array (
'type' => 'select',
'label' => 'Location(Pre Order)',
'placeholder' => 'Select Preorder Location',
'class' => array ('address-field', 'update_totals_on_change' ),
'required' => false,
'options' => $this->preorderLocations->getLocations()
// 'options' => $this->getLocations()
);
// Add custom billing location
$fields['billing']['billing_location'] = array (
'type' => 'select',
'label' => 'Location',
'placeholder' => 'Select location',
'class' => array ('address-field', 'update_totals_on_change' ),
'required' => false,
'options' => $this->locations->getLocations()
// 'options' => $this->getLocations()
);
return $fields;
}
Create class to help calculate cost based on Shipping and Location.
What I have tried
Using this answer but no luck. Click here for the question.
Tried to make field to required, as I saw someone said it only update when all required fields filled.
Tried to disable cache in wp-config.php
I found solution from this link
I might help someone that has similar problem. You just need to clear the session. So, WooCommerce would re-calculate and recall your calculate_shipping() function.
To do that add woocommerce_checkout_update_order_review hook. It would look something like this
add_action('woocommerce_checkout_update_order_review', 'action_woocommerce_checkout_update_order_review', 10, 1);
function action_woocommerce_checkout_update_order_review( $posted_data )
{
global $woocommerce;
$packages = $woocommerce->cart->get_shipping_packages();
foreach( $packages as $package_key => $package ) {
$session_key = 'shipping_for_package_'.$package_key;
$stored_rates = WC()->session->__unset( $session_key );
}
}

Update User Meta with spaces using REST API - WordPress

I'm trying to update the user meta with spaces registered on my website. I am using register_meta hook to register user meta. And for updating users using WordPress REST API I use wp_remote_post but the problem is when I tried to update a user meta with spaces it will add as array in response then it will add as new user meta.
For example the value of user meta is - community test
Sample Response:
[meta] => Array
(
[agent_community] => Array
(
[0] => community
[1] => test
)
)
Sample Code for registering user meta.
register_meta('user', 'agent_community', array(
"type" => "string",
"show_in_rest" => true
));
Update using wp_remote_post
$update_data = array('email'=> 'test#mail.com', 'meta[agent_community]' => 'community test');
$args = array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( 'username:password' ),
),
'body' => array_filter($update_data));
$update_url = 'https://test-url.com/wp-json/wp/v2/users/17';
$response = wp_remote_post( $update_url, $args);
Database:
Is there a way that can merge the array on user meta?
implode() might help you. And you can change spaces with HTML entities. Or use function like urledncode() / urlendecode() or something like this.

My CPT taxonomy meta_fields not showing in Wordpress REST API

I added a custom meta field to my cpt taxonomy with "{$taxonomy}_add_form_fields".
So far it is working fine (add, edit and save) but I cant find this field in the API /wp-json/wp/v2/rest_base.
Is this a filter issue or do i "ADD" this field to the API?
... This answers a slightly different question than the one above ...
You need to enable the REST API where you defined the taxonomy.
Just add 'show_in_rest_api' => true
Something like this:
<?php
add_action( 'init', 'create_book_tax' );
function create_book_tax() {
register_taxonomy(
'genre',
'book',
array(
'label' => __( 'Genre' ),
'rewrite' => array( 'slug' => 'genre' ),
'hierarchical' => true,
'show_in_rest_api' => true // <-- Do This!
)
);
}
?>
You register the meta field calling register_term_meta, observe that using show_in_rest key not only set to true, but at least with an schema description, it is available both in queries to get the data, and to get the schema of that endpoint with OPTIONS method.
register_term_meta('replica', 'nice_field', [
'type' => 'string',
'description' => 'a nice description',
'single' => true,
'show_in_rest' => [
'schema' => [
'type' => 'string',
'format' => 'url',
'context' => ['view', 'edit'],
'readonly' => true,
]
],
]);
Using the schema key its currently documented only for non scalar types, but it's valid for scalar values too.
I had a similar issue. The answers here seem to point to the way to expose the taxonomy itself via the REST API, but not the custom fields created via ${taxonomy}_add_form_fields.
After some more research, I concluded that one should extend the REST API to modify the responses and explicitly add these custom taxonomy fields.
So, in my case, this was the solution:
// Add a custom field to the taxonomy form
function my_add_extra_fields_func()
{
$tpl = '
<div class="form-field form-required">
<label for="link">Field Label</label>
<input type="text" name="link" id="link" />
<p>Some help text</p>
</div>
';
echo $tpl;
}
add_action('taxonomySlug_add_form_fields', 'my_add_extra_fields_func');
// Save the custom field to database in the options table
function my_save_extra_fields_func($term_id)
{
$term_item = get_term($term_id, 'taxonomySlug');
$term_slug = $term_item->slug;
$link = sanitize_text_field($_POST['link']);
update_option('taxonomySlug_link_' . $term_slug, $link);
}
add_action('create_taxonomySlug', 'my_save_extra_fields_func');
/**
* This is the part that addresses the question
* of making the custom field visible in the API
*/
// Expose the taxonomy custom field via REST API
add_action( 'rest_api_init', function () {
register_rest_field( 'taxonomySlug', 'extra_fields', array(
'get_callback' => function( $term_as_arr ) {
$term_slug = $term_as_arr['slug'];
$link = get_option('taxonomySlug_link_' . $term_slug);
return $link;
})
);
});
* taxonomySlug = the slug of my taxonomy

How to add a custom event item in Buddypress Activity Stream?

Does anybody know the basic steps on how to add a custom event item to the Buddypress activity stream? I've got a custom post type of "activity" that my members can click on in the post and it records in a database table that that user has "completed" that activity. More or less like a "to do" list. I'm trying to figure out how I can integrate this with the buddypress activity stream so when a user clicks the button, it inserts something like, "John Doe has completed Permalinked Post Title Here." I've looked through the Buddypress codex & have Googled quite a bit, but I can't seem to find any good tutorials on this. If anybody can point me in the right direction or give me a simple plain English step by step I would appreciate it. Please know that I'm not looking for any code written, just a general idea of the steps involved in making this happen.
For anyone who is searching, this is what I wrote in my php file that was called in the ajax request for the button that "completes" the activity...
// Begin Code for Custom Activity Stream
global $wp, $wp_query;
$this_activity_object = get_post( $activity_post_id_complete );
$title = $this_activity_object->post_title;
$activity_content = $title;
$this_user_profile_url = bp_core_get_user_domain($wp_user_id);
$action_String = "#" . bp_core_get_username($wp_user_id)." just completed a custom activity!";
global $bp;
bp_activity_add( array(
'user_id' => $wp_user_id,
'item_id' => $activity_db_id_complete,
'action' => $action_String,
'content' => $activity_content,
'component' => 'activity',
'primary_link' => '' . $title . '',
'type' => 'custom_activity_update',
'hide_sitewide' => false
));
You can use bp_activity_add(). For example, you'd do it like this:
// record an activity item to the activity table
bp_activity_add( array(
'id' => $id
'user_id' => $user_id,
'item_id' => $item_id,
'action' => $action,
'content' => $content,
'component' => $component,
'primary_link' => $link,
'type' => $type,
'secondary_item_id' => $secondary_item_id,
'recorded_time' => $time,
'hide_sitewide' => false,
'is_spam' => $spam
));
Ref explaining what the parameters mean:
http://codex.buddypress.org/developer/function-examples/bp_activity_add/

Resources