Skip to content

Instantly share code, notes, and snippets.

@fernandoacosta
Created May 27, 2021 00:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fernandoacosta/c97c35294816f77d3c2da9dc4cc16470 to your computer and use it in GitHub Desktop.
Save fernandoacosta/c97c35294816f77d3c2da9dc4cc16470 to your computer and use it in GitHub Desktop.
extra triggers learndash
<?php
add_filter( 'learndash_notifications_email_content', function( $message, $notification_id ) {
if ( ! function_exists( 'WC' ) ) {
return $message;
}
// load the mailer class.
$mailer = WC()->mailer();
// create a new email.
$email = new \WC_Email();
$email_heading = get_post_meta(
$notification_id,
'_ld_notifications_notification_heading',
true
);
$email_heading = $email_heading ? $email_heading : 'Olá,';
$message = apply_filters( 'woocommerce_mail_content', $email->style_inline( $mailer->wrap_message( $email_heading, $message ) ) );
return $message;
}, 550, 2 );
add_filter( 'learndash_notification_settings', function( $fields ) {
$fields['notification_heading'] = array(
'type' => 'text',
'title' => __( 'Cabeçalho do e-mail', 'learndash-notifications' ),
'help_text' => __( 'Título do e-mail enviado com template do WooCommerce.', 'learndash-notifications' ),
'hide' => 0,
'disabled' => 0,
'hide_on' => array(),
'default' => 'Olá,',
);
return $fields;
});
add_filter( 'learndash_notifications_triggers', function( $triggers ) {
$triggers['new_lesson_added'] = 'Nova aula adicionada ao curso';
return $triggers;
});
add_action( 'save_post', 'abc_test_google', 99, 3 );
function abc_test_google( $n_id, $post, $update )
{
error_log(print_r($post, true) . ' == é update? ' . wc_bool_to_string( $update ));
}
use LearnDash_Notification\Notification;
use LearnDash_Notification\Trigger;
class Minha_Classe_de_Trigger_Nova_Aula extends Trigger {
/**
* @var string
*/
protected $trigger = 'new_lesson_added';
public function monitor( $post_ID, $post, $update = false ) {
// não está pronto para notificar
if ( 'publish' !== $post->post_status || empty( $post->post_content ) ) {
$this->log( 'notificação inválida... post status ou conteúdo inválido.' );
return;
}
if ( get_post_meta( $post_ID, '_notification_' . $this->trigger . '_sent', true ) ) {
$this->log( 'notificação já enviada...' );
return;
}
update_post_meta( $post_ID, '_notification_' . $this->trigger . '_sent', time() );
$course_id = get_post_meta( $post_ID, 'course_id', true );
// nenhum curso definido
if ( ! $course_id ) {
$this->log( 'curso não definido. Lesson ID: ' . $post_ID );
return;
}
$course = get_post( $course_id );
if ( ! $course ) {
$this->log( 'Curso ' . $course_id . ' não existe. Lesson ID: ' . $post_ID );
return;
}
$models = $this->get_notifications( $this->trigger );
if ( empty( $models ) ) {
return;
}
$this->log( '==========Job start========' );
$this->log( sprintf( 'Process %d notifications', count( $models ) ) );
foreach ( $models as $model ) {
if ( $model->course_id !== 0 && absint( $course->ID ) !== $model->course_id ) {
continue;
}
if ( $model->lesson_id !== 0 && $model->lesson_id !== absint( $lesson->ID ) ) {
//this is not for me, as a lesson only belong to a course, so we don't need to check the course ID
continue;
}
$user_ids = $this->get_users( $course_id );
$this->log( sprintf( 'Process %d users in this course', count( $user_ids ) ) );
foreach ( $user_ids as $user_id ) {
$emails = $model->gather_emails( $user_id, $course->ID );
$args = [
'user_id' => $user_id,
'course_id' => $course->ID,
'lesson_id' => $post_ID,
];
if ( absint( $model->delay ) ) {
$this->queue_use_db( $emails, $model, $args );
} else {
$this->send( $emails, $model, $args );
$this->log( 'Done, moving next if any' );
}
}
}
$this->log( '==========Job end========' );
}
/**
* A base point for monitoring the events
* @return void
*/
function listen() {
add_action( 'save_post_sfwd-lessons', [ &$this, 'monitor' ], 10, 3 );
add_action( 'leanrdash_notifications_send_delayed_email', [ &$this, 'send_db_delayed_email' ] );
}
/**
* @param Notification $model
* @param $args
*
* @return bool
*/
protected function can_send_delayed_email( Notification $model, $args ) {
return true;
}
/**
*
* @param $course_id
*
* @return array
*/
protected function get_users( $course_id ) {
$query = learndash_get_users_for_course( $course_id );
if ( $query instanceof \WP_User_Query ) {
return $query->get_results();
}
return [];
}
}
add_action( 'init', 'registrar_a_classe_extra_do_notifications' );
function registrar_a_classe_extra_do_notifications() {
$trigger = new Minha_Classe_de_Trigger_Nova_Aula();
$trigger->listen();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment