Advertisement
verygoodplugins

Untitled

Mar 31st, 2022
943
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.17 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.         add_filter( 'wpf_watched_meta_fields', array( $this, 'watch_meta_fields' ) );
  29.  
  30.         add_filter( 'wpf_user_update', array( $this, 'user_update' ), 10, 2 );
  31.         add_filter( 'wpf_user_register', array( $this, 'user_update' ), 10, 2 );
  32.  
  33.         // Apply tags on course completion
  34.         add_action( 'wpep_user_set_course_data', array( $this, 'apply_tags_wpep_complete' ), 10, 7 );
  35.  
  36.         // Settings
  37.         add_action( 'wpf_meta_box_content', array( $this, 'meta_box_content' ), 40, 2 );
  38.  
  39.     }
  40.  
  41.  
  42.     /**
  43.      * Adds field group for WPEP to contact fields list
  44.      *
  45.      * @access  public
  46.      * @return  array Meta fields
  47.      */
  48.  
  49.     public function add_meta_field_group( $field_groups ) {
  50.  
  51.         if ( ! isset( $field_groups['wpep'] ) ) {
  52.             $field_groups['wpep'] = array(
  53.                 'title'  => 'eLearnCommerce',
  54.                 'fields' => array(),
  55.             );
  56.         }
  57.  
  58.         return $field_groups;
  59.  
  60.     }
  61.  
  62.     /**
  63.      * Sets field labels and types for WPEP custom fields
  64.      *
  65.      * @access  public
  66.      * @return  array Meta fields
  67.      */
  68.  
  69.     public function prepare_meta_fields( $meta_fields ) {
  70.  
  71.         $meta_fields[ WPEP_USER_META_AUTO_LOGIN_TOKEN ] = array(
  72.             'label' => 'Auto Login Token',
  73.             'type'  => 'text',
  74.             'group' => 'wpep',
  75.         );
  76.  
  77.         return $meta_fields;
  78.  
  79.     }
  80.  
  81.     /**
  82.      * Sets up token to automatically sync
  83.      *
  84.      * @access  public
  85.      * @return  array Meta Fields
  86.      */
  87.  
  88.     public function watch_meta_fields( $meta_fields ) {
  89.  
  90.         $meta_fields[] = WPEP_USER_META_AUTO_LOGIN_TOKEN;
  91.  
  92.         return $meta_fields;
  93.  
  94.     }
  95.  
  96.     /**
  97.      * Generate autologin token on push if needed
  98.      *
  99.      * @access  public
  100.      * @return  array Update Data
  101.      */
  102.  
  103.     public function user_update( $update_data, $user_id ) {
  104.  
  105.         if ( empty( $update_data[ WPEP_USER_META_AUTO_LOGIN_TOKEN ] ) ) {
  106.  
  107.             $user_token = get_user_meta( $user_id, WPEP_USER_META_AUTO_LOGIN_TOKEN, true );
  108.  
  109.             if ( $user_token === '' ) {
  110.  
  111.                 $salt       = wpep_get_setting( 'auto-login-link-salt' );
  112.                 $user_token = sha1( $salt . wpep_generate_random_token( 32 ) );
  113.                 update_user_meta( $user_id, WPEP_USER_META_AUTO_LOGIN_TOKEN, $user_token );
  114.  
  115.                 $update_data[ WPEP_USER_META_AUTO_LOGIN_TOKEN ] = $user_token;
  116.  
  117.             } else {
  118.  
  119.                 $update_data[ WPEP_USER_META_AUTO_LOGIN_TOKEN ] = $user_token;
  120.  
  121.             }
  122.         }
  123.  
  124.         return $update_data;
  125.  
  126.     }
  127.  
  128.  
  129.     /**
  130.      * Determine whether or not a user has access to content
  131.      *
  132.      * @access public
  133.      * @return bool
  134.      */
  135.  
  136.     public function has_access( $post_id ) {
  137.  
  138.         if ( ! wp_fusion()->access->user_can_access( $post_id ) ) {
  139.             return false;
  140.         } else {
  141.             return true;
  142.         }
  143.  
  144.     }
  145.  
  146.  
  147.     /**
  148.      * Displays restricted item button text in course grid
  149.      *
  150.      * @access public
  151.      * @return string Text
  152.      */
  153.  
  154.     public function get_sell_text( $post_id = 0 ) {
  155.  
  156.         if ( ! $this->has_access( $post_id ) ) {
  157.  
  158.             $settings = get_post_meta( $post_id, 'wpf-settings', true );
  159.  
  160.             if ( ! empty( $settings['restricted_button_text'] ) ) {
  161.                 return $settings['restricted_button_text'];
  162.             }
  163.         }
  164.  
  165.         return $this->sell_button_text;
  166.  
  167.     }
  168.  
  169.  
  170.     /**
  171.      * Applies tags when a WPEP course is completed
  172.      *
  173.      * @access public
  174.      * @return void
  175.      */
  176.  
  177.     public function apply_tags_wpep_complete( $key, $progress, $course_id, $section_id, $lesson_id, $user_id, $updated ) {
  178.  
  179.         if ( $key == 'course_completed' && $progress == 1 ) {
  180.  
  181.             $wpf_settings = get_post_meta( $course_id, 'wpf-settings', true );
  182.  
  183.             if ( ! empty( $wpf_settings['apply_tags_wpep'] ) ) {
  184.                 wp_fusion()->user->apply_tags( $wpf_settings['apply_tags_wpep'], $user_id );
  185.             }
  186.         }
  187.  
  188.     }
  189.  
  190.  
  191.     /**
  192.      * Adds WPEP fields to WPF meta box
  193.      *
  194.      * @access public
  195.      * @return void
  196.      */
  197.  
  198.     public function meta_box_content( $post, $settings ) {
  199.  
  200.         if ( $post->post_type != WPEP_POST_TYPE_COURSE ) {
  201.             return;
  202.         }
  203.  
  204.         echo '<hr/>';
  205.  
  206.         echo '<strong style="margin-top: 5px; display: inline-block;">eLearnCommerce Course:</strong>';
  207.  
  208.         echo '<p><label for="wpf-apply-tags-wpep"><small>Apply these tags when marked complete:</small></label>';
  209.  
  210.         wpf_render_tag_multiselect(
  211.             array(
  212.                 'setting'   => $settings['apply_tags_wpep'],
  213.                 'meta_name' => 'wpf-settings',
  214.                 'field_id'  => 'apply_tags_wpep',
  215.             )
  216.         );
  217.  
  218.         echo '</p>';
  219.  
  220.         echo '<p><label for="wpf-restricted-course-message"><small>Button text to display when course is restricted:</small></label>';
  221.  
  222.         if ( ! isset( $settings['restricted_button_text'] ) ) {
  223.             $settings['restricted_button_text'] = '';
  224.         }
  225.  
  226.         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'] . '">';
  227.  
  228.         echo '</p>';
  229.  
  230.     }
  231.  
  232. }
  233.  
  234. new WPF_WPEP();
  235.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement