Skip to content

Instantly share code, notes, and snippets.

@robertuniqid
Last active April 20, 2020 22:39
Show Gist options
  • Save robertuniqid/238d0e57d1c43bc806258a13cab62b1e to your computer and use it in GitHub Desktop.
Save robertuniqid/238d0e57d1c43bc806258a13cab62b1e to your computer and use it in GitHub Desktop.
eLearnCommerce - Abstract WPEP_Integration_Email_Marketing
<?php
class WPEP_Integration_Email_Marketing_Coordinator_Administration {
/**
* @var $controller WPEP_Integration_Email_Marketing
*/
protected $_controller;
/**
* @var $controller WPEP_Integration_Email_Marketing
*/
public function __construct( WPEP_Integration_Email_Marketing $controller ) {
$this->_controller = $controller;
}
public function init() {
if( $this->_controller->is_configured() != true )
return;
add_filter( 'wpep_admin_script_variables', [ $this, '_admin_script_variables' ], 10, 2 );
add_action( 'wpep_administration_section_option_field', [ $this, 'section_option_field' ], 10, 2 );
add_action( 'wpep_administration_lesson_option_field', [ $this, 'lesson_option_field' ], 10, 2 );
add_filter( 'wpep_meta_boxes', [ $this, 'entity_meta_boxes' ] );
add_action( 'wpep_administration_assessment_meta_options_block', [ $this, 'assessment_meta_options_block' ], 10, 2 );
}
public function get_controller() {
return $this->_controller;
}
public function _admin_script_variables( $variables, $hook ) {
if( !in_array( $hook, [ "post-new.php", "post.php" ] ) )
return $variables;
global $post;
if( isset($post->post_type) && in_array( $post->post_type, $this->_controller->post_types ) )
$variables[ $this->_controller->get_alias() . '_tags' ] = $this->_controller->get_tag_list_cached( false );
return $variables;
}
public function section_option_field( $field_prefix, $section_options ) {
$option_name = $this->_controller->section_option_name;
echo '<div class="wpep-form-group wpep-form-group-section-' . $this->_controller->get_slug() . '-settings">';
echo '<div class="wpep-form-group-row">';
echo '<label for="wpep-section-title">' . $this->_controller->get_name() . " " . $this->_controller->entity_name . ':</label>';
echo $this->_controller->get_tag_input_html(
$field_prefix . '[' . $option_name . ']',
( isset( $section_options->$option_name ) ? $section_options->$option_name : '' )
);
echo '</div>';
echo '</div>';
}
public function lesson_option_field( $field_prefix, $lesson_options ) {
$option_name = $this->_controller->lesson_option_name;
echo '<div class="wpep-form-group wpep-form-group-lesson-' . $this->_controller->get_slug() . '-settings">';
echo '<div class="wpep-form-group-row">';
echo '<label for="wpep-section-title">' . $this->_controller->get_name() . " " . $this->_controller->entity_name . ':</label>';
echo $this->_controller->get_tag_input_html(
$field_prefix . '[' . $option_name . ']',
( isset( $lesson_options->$option_name ) ? $lesson_options->$option_name : '' )
);
echo '</div>';
echo '</div>';
}
public function assessment_meta_options_block( $field_prefix, $assessment_id = false ) {
echo '<h3>' . sprintf( __( "Assessment Status Student %s - %s", 'wpep' ), $this->_controller->entity_name, $this->_controller->get_name() ) . '</h3>';
echo '<div class="wpep-form-group wpep-form-group-assessment-' . $this->_controller->get_slug() . '-settings">';
echo '<div class="wpep-form-group-row">';
echo '<label for="wpep-section-title">' . __( "Status Pending", 'wpep' ) . ':</label>';
echo $this->_controller->get_tag_input_html(
$field_prefix . '[' . $this->_controller->post_meta_prefix . 'tag_status_pending]',
get_post_meta( $assessment_id, $this->_controller->post_meta_prefix . 'tag_status_pending', true )
);
echo '</div>';
echo '<div class="wpep-form-group-row">';
echo '<label for="wpep-section-title">' . __( "Status Pending Review", 'wpep' ) . ':</label>';
echo $this->_controller->get_tag_input_html(
$field_prefix . '[' . $this->_controller->post_meta_prefix . 'tag_status_pending_review]',
get_post_meta( $assessment_id, $this->_controller->post_meta_prefix . 'tag_status_pending_review', true )
);
echo '</div>';
echo '<div class="wpep-form-group-row">';
echo '<label for="wpep-section-title">' . __( "Status Completed", 'wpep' ) . ':</label>';
echo $this->_controller->get_tag_input_html(
$field_prefix . '[' . $this->_controller->post_meta_prefix . 'tag_status_completed]',
get_post_meta( $assessment_id, $this->_controller->post_meta_prefix . 'tag_status_completed', true )
);
echo '</div>';
echo '<div class="wpep-form-group-row">';
echo '<label for="wpep-section-title">' . __( "Status Failed", 'wpep' ) . ':</label>';
echo $this->_controller->get_tag_input_html(
$field_prefix . '[' . $this->_controller->post_meta_prefix . 'tag_status_failed]',
get_post_meta( $assessment_id, $this->_controller->post_meta_prefix . 'tag_status_failed', true )
);
echo '</div>';
echo '</div>';
}
public function entity_meta_boxes( $meta_boxes ) {
$meta_boxes[ $this->_controller->get_slug() . '-course-settings'] = [
'id' => 'wpep_' . $this->_controller->get_alias() . '_course_settings',
'title' => sprintf( __( '%s Integration', 'wpep' ), $this->_controller->get_name() ),
'post_types' => [ WPEP_POST_TYPE_COURSE ],
'context' => 'side',
'priority' => 'low',
'show_names' => true,
'fields' => [
$this->_controller->get_tag_input_metabox_default() + [
'name' => sprintf( __( 'Started %s', 'wpep' ), $this->_controller->entity_name ),
'desc' => sprintf( __( 'When starting the course the user will be given this %s.', 'wpep' ), $this->_controller->entity_name ),
'id' => $this->_controller->post_meta_started
],
$this->_controller->get_tag_input_metabox_default() + [
'name' => sprintf( __( 'Completed %s', 'wpep' ), $this->_controller->entity_name ),
'desc' => sprintf( __( 'After finishing the course the user will be given this %s.', 'wpep' ), $this->_controller->entity_name ),
'id' => $this->_controller->post_meta_completed
]
]
];
return $meta_boxes;
}
}
<?php
require_once( WPEP_BASE_PATH . "/lib/integrations/Email_Marketing/Frontend.php" );
require_once( WPEP_BASE_PATH . "/lib/integrations/Email_Marketing/Administration.php" );
abstract class WPEP_Integration_Email_Marketing {
protected $_tag_list = NULL;
public $post_types = [ WPEP_POST_TYPE_COURSE, WPEP_POST_TYPE_ASSESSMENT ];
public $entity_name = "Tag";
public $input_placeholder = "Start typing to autocomplete...";
public $input_class = 'regular-text wpep-tag-cloud-input';
public $has_statistics_log = true;
public $has_text_based_tags = false;
public $post_meta_prefix = '';
public $post_meta_started = '';
public $post_meta_completed = '';
public $section_option_name = '';
public $lesson_option_name = '';
public $log_option_name = '';
public $administrationCoordinator;
public $frontendCoordinator;
public function __construct() {
$this->_setup_vars();
do_action( "wpep_email_marketing_integration_init_before", $this );
if( $this->is_configured() ) {
$this->frontendCoordinator = new WPEP_Integration_Email_Marketing_Coordinator_Frontend( $this );
$this->frontendCoordinator->init();
wpep_controller()->emailMarketing->register( $this );
}
if( is_admin() ) {
$this->administrationCoordinator = new WPEP_Integration_Email_Marketing_Coordinator_Administration( $this );
$this->administrationCoordinator->init();
}
}
abstract public function get_name();
abstract public function get_alias();
abstract public function get_slug();
abstract public function is_configured();
abstract public function get_tag_list();
public function get_tag_list_cached( $from_transient = true, $force_refresh = false ) {
if( !$force_refresh ) {
if( $this->_tag_list !== null )
return $this->_tag_list;
if( $from_transient ) {
$this->_tag_list = get_transient( 'wpep_email_marketing_' . $this->get_alias() . '_tags' );
if( is_array( $this->_tag_list ) )
return $this->_tag_list;
}
}
$this->_tag_list = $this->get_tag_list();
set_transient( 'wpep_email_marketing_' . $this->get_alias() . '_tags', $this->_tag_list, time() + DAY_IN_SECONDS );
return $this->_tag_list;
}
abstract public function add_tag( $tag, $user_id );
abstract public function remove_tag( $tag, $user_id );
private function _setup_vars() {
$this->section_option_name = $this->get_alias() . '_tag';
$this->lesson_option_name = $this->get_alias() . '_tag';
$this->post_meta_prefix = 'wpep_' . $this->get_alias() . '_';
$this->post_meta_started = $this->post_meta_prefix . 'tag_started';
$this->post_meta_completed = $this->post_meta_prefix . 'tag';
if( $this->log_option_name == '' )
$this->log_option_name = 'wpep_addon_' . $this->get_alias() . '_log';
}
public function get_tag_input_metabox_default() {
$storage_index_data = [
'storage-index' => $this->get_alias() . "_tags"
];
return [
'type' => 'text',
'class' => $this->input_class,
'data' => ( strpos( $this->input_class, 'wpep-tag-cloud-input' ) === false ? [] : $storage_index_data ),
'placeholder' => $this->input_placeholder
];
}
public function get_tag_input_html( $input_name, $value = '' ) {
return '<input type="text"
class="' . $this->input_class . '"
' . ( strpos( $this->input_class, 'wpep-tag-cloud-input' ) === false ? '' : 'data-storage-index="' . $this->get_alias() . '_tags"' ) . '
style="min-width:400px;"
placeholder="' . $this->input_placeholder . '"
name="' . $input_name . '"
value="' . $value . '">';
}
}
<?php
class WPEP_Integration_Email_Marketing_Coordinator_Frontend {
/**
* @var $controller WPEP_Integration_Email_Marketing
*/
protected $_controller;
/**
* @var $controller WPEP_Integration_Email_Marketing
*/
public function __construct( WPEP_Integration_Email_Marketing $controller ) {
$this->_controller = $controller;
}
public function init() {
add_action( 'wpep_user_activity_lesson_status', [ $this, 'user_lesson_activity' ], 10, 3 );
add_action( 'wpep_user_set_course_data', [ $this, 'user_course_data'], 10, 7 );
add_action( 'wpep_user_set_assessment_data', [ $this, 'user_assessment_data' ], 10, 7 );
}
public function get_controller() {
return $this->_controller;
}
public function user_lesson_activity($lesson_id, $is_completed, $user_id) {
$option_name = $this->_controller->lesson_option_name;
$lesson = WPEP\Entity\Course::instance()->get_lesson( $lesson_id );
if( isset( $lesson->options->$option_name ) && $lesson->options->$option_name !== '' ) {
if( $is_completed )
$this->_controller->add_tag( explode(",", $lesson->options->$option_name ), $user_id );
else
$this->_controller->remove_tag( explode(",", $lesson->options->$option_name ), $user_id );
}
$section = WPEP\Entity\Course::instance()->get_section( $lesson->section_id );
$this->_user_lesson_activity_section( $section, $is_completed, $user_id );
return true;
}
private function _user_lesson_activity_section( $section, $is_completed, $user_id ) {
$option_name = $this->_controller->section_option_name;
if( !isset( $section->options->$option_name ) || $section->options->$option_name == '' )
return;
$removeTags = true;
if( $is_completed ) {
$progress = wpep_user_course_progress_percentage( $section->post_id, $user_id, $section->id );
$removeTags = ( $progress == 100 ? 0 : 1 );
}
if( !$removeTags )
$this->_controller->add_tag( explode(",", $section->options->$option_name ), $user_id );
else
$this->_controller->remove_tag( explode(",", $section->options->$option_name ), $user_id );
}
public function user_course_data( $key, $value, $course_id, $section_id, $lesson_id, $user_id, $existent_entry ) {
if( $key === WPEP_USER_COURSE_STARTED ) {
$this->_user_course_data_started_at( $value, $course_id, $section_id, $lesson_id, $user_id, $existent_entry );
return;
}
if( $key === WPEP_USER_COURSE_COMPLETED ) {
$this->_user_course_data_completed( $value, $course_id, $section_id, $lesson_id, $user_id, $existent_entry );
return;
}
}
private function _user_course_data_started_at( $value, $course_id, $section_id, $lesson_id, $user_id, $existent_entry ) {
if( $value === 0 )
return;
$tag = get_post_meta( $course_id, $this->_controller->post_meta_started, true );
if( $tag === null || $tag == '' )
return;
$this->_controller->add_tag( explode(",", $tag ), $user_id );
}
private function _user_course_data_completed( $course_completed, $course_id, $section_id, $lesson_id, $user_id, $existent_entry ) {
$tag = get_post_meta( $course_id, $this->_controller->post_meta_completed, true );
if( $tag === null || $tag === '' )
return;
if( $course_completed )
$this->_controller->add_tag( explode(",", $tag ), $user_id );
else
$this->_controller->remove_tag( explode(",", $tag ), $user_id );
}
public function user_assessment_data( $key, $value, $post_id, $question_id, $question_answer_id, $user_id, $updated ) {
if( $key != WPEP_USER_ASSESSMENT_STATUS )
return;
$status_meta_map = [
WPEP_USER_ASSESSMENT_STATUS_PENDING => $this->_controller->post_meta_prefix . 'tag_status_pending',
WPEP_USER_ASSESSMENT_STATUS_PENDING_REVIEW => $this->_controller->post_meta_prefix . 'tag_status_pending_review',
WPEP_USER_ASSESSMENT_STATUS_COMPLETED => $this->_controller->post_meta_prefix . 'tag_status_completed',
WPEP_USER_ASSESSMENT_STATUS_FAILED => $this->_controller->post_meta_prefix . 'tag_status_failed',
];
$remove_tags = [];
foreach( $status_meta_map as $status => $meta_name ) {
if( $status == $value )
continue;
$tag = get_post_meta( $post_id, $meta_name, true );
if( $tag === null || $tag === '' )
continue;
$remove_tags = array_merge( $remove_tags, explode(",", $tag ) );
}
$remove_tags = array_unique( $remove_tags );
if( !empty( $remove_tags ) )
$this->_controller->remove_tag( $remove_tags, $user_id );
if( !isset( $status_meta_map[ $value ] ) )
return;
$tag = get_post_meta( $post_id, $status_meta_map[ $value ], true );
if( $tag === null || $tag == '' )
return;
$this->_controller->add_tag( explode(",", $tag ), $user_id );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment