Sensei_Shortcode_User_Messages::render()   B
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 34
Code Lines 17

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 34
rs 8.439
cc 5
eloc 17
nc 12
nop 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 18 and the first side effect is on line 2.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
3
/**
4
 *
5
 * Renders the [sensei_user_messages] shortcode. The current users messages.
6
 * If none exists nothing will be shown.
7
 *
8
 * This class is loaded int WP by the shortcode loader class.
9
 *
10
 * @class Sensei_Shortcode_Teachers
11
 *
12
 * @package Content
13
 * @subpackage Shortcode
14
 * @author Automattic
15
 *
16
 * @since 1.9.0
17
 */
18
class Sensei_Shortcode_User_Messages implements Sensei_Shortcode_Interface {
19
20
    /**
21
     * @var WP_Query
22
     * messages for the current user
23
     */
24
    protected $messages_query;
25
26
    /**
27
     * Setup the shortcode object
28
     *
29
     * @since 1.9.0
30
     * @param array $attributes
31
     * @param string $content
32
     * @param string $shortcode the shortcode that was called for this instance
33
     */
34
    public function __construct( $attributes, $content, $shortcode ){
35
36
        $this->setup_messages_query();
37
38
    }
39
40
    /**
41
     * create the messages query .
42
     *
43
     * @return mixed
44
     */
45
    public function setup_messages_query(){
46
47
        $user = wp_get_current_user();
48
49
        $args = array(
50
            'post_type' => 'sensei_message',
51
            'posts_per_page' => 500,
52
            'orderby' => 'date',
53
            'order' => 'DESC',
54
            'post_status' => 'publish',
55
            'meta_query' => array(
56
                array(
57
                    'key'     => '_sender',
58
                    'value'   => $user->user_login,
59
                    'compare' => '=',
60
                ),
61
            ),
62
        );
63
64
        $this->messages_query  = new WP_Query( $args );
65
    }
66
67
    /**
68
     * Rendering the shortcode this class is responsible for.
69
     *
70
     * @return string $content
71
     */
72
    public function render(){
73
74
        if( !is_user_logged_in() ){
75
76
            Sensei()->notices->add_notice( __('Please login to view your messages.','woothemes-sensei') , 'alert'  );
77
78
        } elseif( 0 == $this->messages_query->post_count ){
79
80
            Sensei()->notices->add_notice( __( 'You do not have any messages.', 'woothemes-sensei') , 'alert'  );
81
        }
82
83
        $messages_disabled_in_settings =  ! ( ! isset( Sensei()->settings->settings['messages_disable'] )
84
                                            || ! Sensei()->settings->settings['messages_disable'] ) ;
85
86
        // don't show anything if messages are disable
87
        if( $messages_disabled_in_settings ){
88
            return '';
89
        }
90
91
        //set the wp_query to the current messages query
92
        global $wp_query;
93
        $wp_query = $this->messages_query;
94
95
        ob_start();
96
        Sensei()->notices->maybe_print_notices();
97
        Sensei_Templates::get_part('loop', 'message');
98
        $messages_html = ob_get_clean();
99
100
        // set back the global query
101
        wp_reset_query();
102
103
        return $messages_html;
104
105
    }// end render
106
107
}// end class