Advertisement
verygoodplugins

Untitled

Jan 26th, 2021
960
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.89 KB | None | 0 0
  1. <?php
  2.  
  3. if ( ! defined( 'ABSPATH' ) ) {
  4.     exit; // Exit if accessed directly
  5. }
  6.  
  7. class WPF_CoursePress extends WPF_Integrations_Base {
  8.  
  9.     /**
  10.      * Gets things started.
  11.      *
  12.      * @since 3.23.1
  13.      */
  14.  
  15.     public function init() {
  16.  
  17.         $this->slug = 'coursepress';
  18.  
  19.         add_action( 'coursepress_student_unit_completed', array( $this, 'unit_completed' ), 10, 4 );
  20.  
  21.         add_action( 'coursepress_student_course_completed', array( $this, 'course_completed' ), 10, 3 );
  22.  
  23.         add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ), 20 );
  24.  
  25.         add_action( 'wp_ajax_wpf_coursepress_save', array( $this, 'save_meta_box_data' ) );
  26.  
  27.     }
  28.  
  29.     /**
  30.      * Apply tags on unit completion.
  31.      *
  32.      * @since 3.23.1
  33.      *
  34.      * @param int    $user_id    The user ID.
  35.      * @param int    $unit_id    The unit ID.
  36.      * @param string $unit_title The unit title.
  37.      * @param int    $course_id  The course ID.
  38.      */
  39.     public function unit_completed( $user_id, $unit_id, $unit_title, $course_id ) {
  40.  
  41.         $settings = get_post_meta( $unit_id, 'wpf_settings_coursepress', true );
  42.  
  43.         if ( ! empty( $settings ) && ! empty( $settings['apply_tags_complete'] ) ) {
  44.  
  45.             wp_fusion()->user->apply_tags( $settings['apply_tags_complete'], $user_id );
  46.  
  47.         }
  48.  
  49.     }
  50.  
  51.  
  52.     /**
  53.      * Apply tags on course completion.
  54.      *
  55.      * @since 3.23.1
  56.      *
  57.      * @param int    $user_id    The user ID.
  58.      * @param int    $course_id  The course ID.
  59.      * @param string $unit_title The course title.
  60.      */
  61.  
  62.     public function course_completed( $user_id, $course_id, $course_title ) {
  63.  
  64.         $settings = get_post_meta( $course_id, 'wpf_settings_coursepress', true );
  65.  
  66.         if ( ! empty( $settings ) && ! empty( $settings['apply_tags_complete'] ) ) {
  67.  
  68.             wp_fusion()->user->apply_tags( $settings['apply_tags_complete'], $user_id );
  69.  
  70.         }
  71.  
  72.     }
  73.  
  74.  
  75.     /**
  76.      * Adds meta box to Course post type.
  77.      *
  78.      * @since 3.23.1
  79.      */
  80.  
  81.     /**
  82.      * Adds meta box to Course post type.
  83.      *
  84.      * @since 3.23.1
  85.      *
  86.      * @param int $post_id The post ID.
  87.      */
  88.     public function add_meta_box( $post_id ) {
  89.  
  90.         add_meta_box( 'wpf-coursepress-meta', 'WP Fusion - Course Settings', array( $this, 'meta_box_callback' ), 'course' );
  91.  
  92.     }
  93.  
  94.  
  95.     /**
  96.      * Displays meta box content.
  97.      *
  98.      * @since 3.23.1
  99.      *
  100.      * @param WP_Post $post   The post object.
  101.      * @return mixed HTML settings output.
  102.      */
  103.     public function meta_box_callback( $post ) {
  104.  
  105.         wp_nonce_field( 'wpf_meta_box_coursepress', 'wpf_meta_box_coursepress_nonce' );
  106.  
  107.         $settings = array(
  108.             'apply_tags_complete' => array(),
  109.         );
  110.  
  111.         if ( get_post_meta( $post->ID, 'wpf_settings_coursepress', true ) ) {
  112.             $settings = array_merge( $settings, get_post_meta( $post->ID, 'wpf_settings_coursepress', true ) );
  113.         }
  114.  
  115.         echo '<h3>Course settings:</h3>';
  116.  
  117.         echo '<table class="form-table"><tbody>';
  118.  
  119.         echo '<tr>';
  120.  
  121.         echo '<th scope="row"><label for="apply_tags_complete">Apply tags:</label></th>';
  122.         echo '<td>';
  123.  
  124.         $args = array(
  125.             'setting'   => $settings['apply_tags_complete'],
  126.             'meta_name' => 'wpf_settings_coursepress',
  127.             'field_id'  => 'apply_tags_complete',
  128.         );
  129.  
  130.         wpf_render_tag_multiselect( $args );
  131.  
  132.         echo '<span class="description">These tags will be applied when the course is marked complete.</span>';
  133.         echo '</td>';
  134.  
  135.         echo '</tr>';
  136.  
  137.         echo '</tbody></table>';
  138.  
  139.         echo '<hr />';
  140.  
  141.         echo '<h3>Unit Settings:</h3>';
  142.  
  143.         $units = get_post_meta( $post->ID, 'cp_structure_visible_units', true );
  144.  
  145.         if ( ! empty( $units ) ) {
  146.  
  147.             echo '<table class="form-table"><tbody>';
  148.  
  149.             foreach ( $units as $unit_id => $visible ) {
  150.  
  151.                 $title = get_the_title( $unit_id );
  152.  
  153.                 if ( empty( $title ) ) {
  154.                     continue;
  155.                 }
  156.  
  157.                 $settings = get_post_meta( $unit_id, 'wpf_settings_coursepress', true );
  158.  
  159.                 if ( empty( $settings ) ) {
  160.                     $settings = array();
  161.                 }
  162.  
  163.                 if ( ! isset( $settings['apply_tags_complete'] ) ) {
  164.                     $settings['apply_tags_complete'] = array();
  165.                 }
  166.  
  167.                 echo '<tr>';
  168.  
  169.                 echo '<th scope="row"><label for="apply_tags_complete">' . $title . ':</label></th>';
  170.                 echo '<td>';
  171.  
  172.                 $args = array(
  173.                     'setting'   => $settings['apply_tags_complete'],
  174.                     'meta_name' => 'wpf_settings_coursepress',
  175.                     'field_id'  => 'apply_tags_unit_' . $unit_id,
  176.                 );
  177.  
  178.                 wpf_render_tag_multiselect( $args );
  179.  
  180.                 echo '<span class="description">These tags will be applied when the unit <strong>' . $title . '</strong> is marked complete.</span>';
  181.                 echo '</td>';
  182.  
  183.                 echo '</tr>';
  184.  
  185.             }
  186.  
  187.             echo '</tbody></table>';
  188.  
  189.         } else {
  190.  
  191.             echo '<br/><br/>Create some units to configure unit tagging.';
  192.  
  193.         }
  194.  
  195.         echo '<table class="form-table"><tbody>';
  196.  
  197.         echo '<tr>';
  198.  
  199.         echo '<th scope="row"></th>';
  200.         echo '<td>';
  201.  
  202.             echo '<input type="hidden" id="wpf-coursepress-postid" value="' . $post->ID . '">';
  203.  
  204.             echo '<a href="#" id="wpf-coursepress-update" class="button">Update</a>';
  205.  
  206.         echo '</td>';
  207.  
  208.         echo '</tr>';
  209.  
  210.         echo '</tbody></table>';
  211.  
  212.     }
  213.  
  214.  
  215.     /**
  216.      * Save the settings to the course.
  217.      *
  218.      * @since 3.23.1
  219.      */
  220.  
  221.     public function save_meta_box_data() {
  222.  
  223.         $post_id = $_POST['id'];
  224.  
  225.         $update_values = array();
  226.  
  227.         foreach ( $_POST['data'] as $setting ) {
  228.  
  229.             if ( 'wpf_settings_coursepress[apply_tags_complete][]' == $setting['name'] ) {
  230.  
  231.                 if ( ! isset( $update_values[ $post_id ] ) ) {
  232.                     $update_values[ $post_id ] = array();
  233.                 }
  234.  
  235.                 $update_values[ $post_id ][] = $setting['value'];
  236.  
  237.             } else {
  238.  
  239.                 $unit_id = str_replace( 'wpf_settings_coursepress[apply_tags_unit_', '', $setting['name'] );
  240.  
  241.                 $unit_id = str_replace( '][]', '', $unit_id );
  242.  
  243.                 if ( ! isset( $update_values[ $unit_id ] ) ) {
  244.                     $update_values[ $unit_id ] = array();
  245.                 }
  246.  
  247.                 $update_values[ $unit_id ][] = $setting['value'];
  248.  
  249.             }
  250.         }
  251.  
  252.         foreach ( $update_values as $post_id => $tags ) {
  253.  
  254.             // Update the meta fields in the database.
  255.             update_post_meta( $post_id, 'wpf_settings_coursepress', array( 'apply_tags_complete' => $tags ) );
  256.  
  257.         }
  258.  
  259.     }
  260.  
  261.  
  262. }
  263.  
  264. new WPF_CoursePress;
  265.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement