Completed
Push — master ( be9484...2a1678 )
by Dwain
04:58
created

__construct()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 21
Code Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 21
rs 8.7624
cc 6
eloc 8
nc 12
nop 3
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
3
/**
4
 *
5
 * Renders the [sensei_unpurchased_courses] shortcode when a user is logged in. If the user is not logged in
6
 * it will show all courses.
7
 * This class is loaded int WP by the shortcode loader class.
8
 *
9
 * @class Sensei_Shortcode_Unpurchased_Courses
10
 * @since 1.9.0
11
 * @package Sensei
12
 * @category Shortcodes
13
 * @author 	WooThemes
14
 */
15
class Sensei_Shortcode_Unpurchased_Courses implements Sensei_Shortcode_Interface {
16
17
    /**
18
     * @var WP_Query to help setup the query needed by the render method.
19
     */
20
    protected $query;
21
22
    /**
23
     * @var string number of items to show on the current page
24
     * Default: all.
25
     */
26
    protected $number;
27
28
    /**
29
     * @var string ordery by course field
30
     * Default: date
31
     */
32
    protected $orderby;
33
34
    /**
35
     * @var string ASC or DESC
36
     * Default: 'DESC'
37
     */
38
    protected  $order;
39
40
    /**
41
     * Setup the shortcode object
42
     *
43
     * @since 1.9.0
44
     * @param array $attributes
45
     * @param string $content
46
     * @param string $shortcode the shortcode that was called for this instance
47
     */
48
    public function __construct( $attributes, $content, $shortcode ){
49
50
        // set up all argument need for constructing the course query
51
        $this->number = isset( $attributes['number'] ) ? $attributes['number'] : '10';
52
        $this->orderby = isset( $attributes['orderby'] ) ? $attributes['orderby'] : 'title';
53
54
        // set the default for menu_order to be ASC
55
        if( 'menu_order' == $this->orderby && !isset( $attributes['order']  ) ){
56
57
            $this->order =  'ASC';
58
59
        }else{
60
61
            // for everything else use the value passed or the default DESC
62
            $this->order = isset( $attributes['order']  ) ? $attributes['order'] : 'DESC';
63
64
        }
65
66
        // setup the course query that will be used when rendering
67
        $this->setup_course_query();
68
    }
69
70
    /**
71
     * Sets up the object course query
72
     * that will be used int he render method.
73
     *
74
     * @since 1.9.0
75
     */
76
    protected function setup_course_query(){
77
78
79
        // course query parameters to be used for all courses
80
        $query_args = array(
81
            'post_type'        => 'course',
82
            'post_status'      => 'publish',
83
            // the number specified by the user will be used later in this function
84
            'posts_per_page' => 1000,
85
            'orderby'          => $this->orderby,
86
            'order'            => $this->order
87
        );
88
89
        // get all the courses that has a product attached
90
        $all_courses_query = new WP_Query( $query_args );
91
92
        $paid_courses_not_taken = array();
93
        // look through all course and find the purchasable ones that user has not purchased
94
        foreach( $all_courses_query->posts as $course ){
95
96
            // only keep the courses with a product including only  courses that the user not taking
97
            $course_product_id = get_post_meta( $course->ID, '_course_woocommerce_product',true );
98
            if( is_numeric( $course_product_id )
99
                &&
100
                ! Sensei_Utils::user_started_course( $course->ID , get_current_user_id()  )
0 ignored issues
show
Bug Best Practice introduced by
The expression \Sensei_Utils::user_star... get_current_user_id()) of type false|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
101
            ){
102
103
                    $paid_courses_not_taken[] = $course->ID;
104
105
                }
106
107
        } // end foreach
108
109
        // setup the course query again and only use the course the user has not purchased.
110
        // this query will be loaded into the global WP_Query in the render function.
111
        $query_args[ 'post__in' ] = $paid_courses_not_taken;
112
        $query_args[ 'posts_per_page' ] = $this->number;
113
114
        $this->query = new WP_Query( $query_args );
115
116
    }// end setup _course_query
117
118
    /**
119
     * Rendering the shortcode this class is responsible for.
120
     *
121
     * @return string $content
122
     */
123
    public function render(){
124
125
        global $wp_query;
126
127
        if ( ! is_user_logged_in() ) {
128
129
            $anchor_before = '<a href="' . esc_url( sensei_user_login_url() ) . '" >';
130
            $anchor_after = '</a>';
131
            $notice = sprintf(
132
                __('You must be logged in to view the non-purchased courses. Click here to %slogin%s.'),
133
                $anchor_before,
134
                $anchor_after
135
            );
136
137
            Sensei()->notices->add_notice( $notice, 'info' );
138
            Sensei()->notices->print_notices();
139
140
            return '';
141
142
        }
143
144
        // keep a reference to old query
145
        $current_global_query = $wp_query;
146
        // assign the query setup in $this-> setup_course_query
147
        $wp_query = $this->query;
148
149
        ob_start();
150
        Sensei()->notices->print_notices();
151
        Sensei_Templates::get_template('loop-course.php');
152
        $shortcode_output =  ob_get_clean();
153
154
        //restore old query
155
        $wp_query = $current_global_query;
156
157
        return $shortcode_output;
158
159
    }// end render
160
161
}