Advertisement
verygoodplugins

Untitled

Aug 12th, 2019
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.69 KB | None | 0 0
  1. <?php
  2.  
  3. if ( ! defined( 'ABSPATH' ) ) {
  4.     exit; // Exit if accessed directly
  5. }
  6.  
  7. class WPF_WPEP extends WPEP_Content_Library_Integration {
  8.  
  9.     public $service_name    = 'WP Fusion';
  10.     public $options_prefix  = 'wpep_wpf_';
  11.  
  12.     /**
  13.      * Gets things started
  14.      *
  15.      * @access  public
  16.      * @return  void
  17.      */
  18.  
  19.     public function __construct() {
  20.  
  21.         wp_fusion()->integrations->wpep = $this;
  22.  
  23.         $this->init();
  24.  
  25.         // Add meta field group
  26.         add_filter( 'wpf_meta_field_groups', array( $this, 'add_meta_field_group' ), 10 );
  27.         add_filter( 'wpf_meta_fields', array( $this, 'prepare_meta_fields' ), 20 );
  28.  
  29.         add_filter( 'wpf_user_update', array( $this, 'user_update' ), 10, 2 );
  30.  
  31.         // Apply tags on course completion
  32.         add_action( 'wpep_user_set_course_data', array( $this, 'apply_tags_wpep_complete' ), 10, 7 );
  33.  
  34.         // Settings
  35.         add_action( 'wpf_meta_box_content', array( $this, 'meta_box_content' ), 40, 2 );
  36.  
  37.     }
  38.  
  39.  
  40.     /**
  41.      * Adds field group for WPEP to contact fields list
  42.      *
  43.      * @access  public
  44.      * @return  array Meta fields
  45.      */
  46.  
  47.     public function add_meta_field_group( $field_groups ) {
  48.  
  49.         if( !isset( $field_groups['wpep'] ) ) {
  50.             $field_groups['wpep'] = array( 'title' => 'eLearnCommerce', 'fields' => array() );
  51.         }
  52.  
  53.         return $field_groups;
  54.  
  55.     }
  56.  
  57.     /**
  58.      * Sets field labels and types for WPEP custom fields
  59.      *
  60.      * @access  public
  61.      * @return  array Meta fields
  62.      */
  63.  
  64.     public function prepare_meta_fields( $meta_fields ) {
  65.  
  66.         $meta_fields[ WPEP_USER_META_AUTO_LOGIN_TOKEN ] = array( 'label' => 'Auto Login Token', 'type' => 'text', 'group' => 'wpep' );
  67.  
  68.         return $meta_fields;
  69.  
  70.     }
  71.  
  72.     /**
  73.      * Generate autologin token on push if needed
  74.      *
  75.      * @access  public
  76.      * @return  array Update Data
  77.      */
  78.  
  79.     public function user_update( $update_data, $user_id ) {
  80.  
  81.         if ( empty( $update_data[ WPEP_USER_META_AUTO_LOGIN_TOKEN ] ) ) {
  82.  
  83.             $user_token = get_user_meta( $user_id, WPEP_USER_META_AUTO_LOGIN_TOKEN, true );
  84.  
  85.             if ( $user_token === '' ) {
  86.  
  87.                 $salt = wpep_get_setting( 'auto-login-link-salt' );
  88.                 $user_token = sha1( $salt . wpep_generate_random_token( 32 ) );
  89.                 update_user_meta( $user_id, WPEP_USER_META_AUTO_LOGIN_TOKEN, $user_token );
  90.  
  91.                 $update_data[ WPEP_USER_META_AUTO_LOGIN_TOKEN ] = $user_token;
  92.  
  93.             } else {
  94.  
  95.                 $update_data[ WPEP_USER_META_AUTO_LOGIN_TOKEN ] = $user_token;
  96.  
  97.             }
  98.  
  99.         }
  100.  
  101.         return $update_data;
  102.  
  103.     }
  104.  
  105.  
  106.     /**
  107.      * Determine whether or not a user has access to content
  108.      *
  109.      * @access public
  110.      * @return bool
  111.      */
  112.  
  113.     public function has_access( $post_id ) {
  114.  
  115.         if( ! wp_fusion()->access->user_can_access( $post_id ) ) {
  116.             return false;
  117.         } else {
  118.             return true;
  119.         }
  120.  
  121.     }
  122.  
  123.  
  124.     /**
  125.      * Displays restricted item button text in course grid
  126.      *
  127.      * @access public
  128.      * @return string Text
  129.      */
  130.  
  131.     public function get_sell_text( $post_id = 0 ) {
  132.  
  133.         if( ! $this->has_access( $post_id ) ) {
  134.  
  135.             $settings = get_post_meta( $post_id, 'wpf-settings', true );
  136.  
  137.             if( ! empty( $settings['restricted_button_text'] ) ) {
  138.                 return $settings['restricted_button_text'];
  139.             }
  140.  
  141.         }
  142.  
  143.         return $this->sell_button_text;
  144.  
  145.     }
  146.  
  147.  
  148.     /**
  149.      * Applies tags when a WPEP course is completed
  150.      *
  151.      * @access public
  152.      * @return void
  153.      */
  154.  
  155.     public function apply_tags_wpep_complete( $key, $progress, $course_id, $section_id, $lesson_id, $user_id, $updated ) {
  156.  
  157.         if( $key == 'course_completed' && $progress == 1 ) {
  158.  
  159.             $wpf_settings = get_post_meta( $course_id, 'wpf-settings', true );
  160.  
  161.             if ( ! empty( $wpf_settings['apply_tags_wpep'] ) ) {
  162.                 wp_fusion()->user->apply_tags( $wpf_settings['apply_tags_wpep'], $user_id );
  163.             }
  164.  
  165.         }
  166.  
  167.     }
  168.  
  169.  
  170.     /**
  171.      * Adds WPEP fields to WPF meta box
  172.      *
  173.      * @access public
  174.      * @return void
  175.      */
  176.  
  177.     public function meta_box_content( $post, $settings ) {
  178.  
  179.         if ( $post->post_type != WPEP_POST_TYPE_COURSE ) {
  180.             return;
  181.         }
  182.  
  183.         echo '<hr/>';
  184.  
  185.         echo '<strong style="margin-top: 5px; display: inline-block;">eLearnCommerce Course:</strong>';
  186.  
  187.         echo '<p><label for="wpf-apply-tags-wpep"><small>Apply these tags when marked complete:</small></label>';
  188.  
  189.         wpf_render_tag_multiselect( array( 'setting' => $settings['apply_tags_wpep'], 'meta_name' => 'wpf-settings', 'field_id' => 'apply_tags_wpep' ) );
  190.  
  191.         echo '</p>';
  192.  
  193.         echo '<p><label for="wpf-restricted-course-message"><small>Button text to display when course is restricted:</small></label>';
  194.  
  195.         if( !isset( $settings['restricted_button_text'] ) ) {
  196.             $settings['restricted_button_text'] = '';
  197.         }
  198.  
  199.         echo '<input type="text" id="wpf-restricted-course-message" placeholder="' . $this->sell_button_text . '" name="wpf-settings[restricted_button_text]" value="' . $settings['restricted_button_text']  . '">';
  200.  
  201.         echo '</p>';
  202.  
  203.     }
  204.  
  205. }
  206.  
  207. new WPF_WPEP;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement