Passed
Pull Request — master (#126)
by Kiran
07:22 queued 03:36
created

WPInv_Shortcodes::history_output()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) {
3
    exit; // Exit if accessed directly
4
}
5
6
class WPInv_Shortcodes {
7
    /**
8
     * Init shortcodes.
9
     */
10
    public static function init() {
11
        $shortcodes = array(
12
            'wpinv_checkout'  => __CLASS__ . '::checkout',
13
            'wpinv_history'  => __CLASS__ . '::history',
14
            'wpinv_receipt'  => __CLASS__ . '::success',
15
            'wpinv_buy'  => __CLASS__ . '::buy',
16
        );
17
18
        foreach ( $shortcodes as $shortcode => $function ) {
19
            add_shortcode( apply_filters( "{$shortcode}_shortcode_tag", $shortcode ), $function );
20
        }
21
        
22
        add_shortcode( 'wpinv_messages', __CLASS__ . '::messages' );
23
    }
24
25
    public static function shortcode_wrapper( $function, $atts = array(), $content = null, $wrapper = array( 'class' => 'wpi-g', 'before' => null, 'after' => null ) ) {
26
        ob_start();
27
28
        echo empty( $wrapper['before'] ) ? '<div class="' . esc_attr( $wrapper['class'] ) . '">' : $wrapper['before'];
29
        call_user_func( $function, $atts, $content );
30
        echo empty( $wrapper['after'] ) ? '</div>' : $wrapper['after'];
31
32
        return ob_get_clean();
33
    }
34
35
    public static function checkout( $atts = array(), $content = null ) {
36
        return self::shortcode_wrapper( array( __CLASS__, 'checkout_output' ), $atts, $content );
37
    }
38
39
    public static function checkout_output( $atts = array(), $content = null ) {
40
        do_action( 'wpinv_checkout_content_before' );
41
        echo wpinv_checkout_form( $atts, $content );
0 ignored issues
show
Unused Code introduced by
The call to wpinv_checkout_form() has too many arguments starting with $atts.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
42
        do_action( 'wpinv_checkout_content_after' );
43
    }
44
45
    public static function messages( $atts, $content = null ) {
0 ignored issues
show
Unused Code introduced by
The parameter $atts is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $content is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
46
        ob_start();
47
        wpinv_print_errors();
48
        return '<div class="wpinv">' . ob_get_clean() . '</div>';
49
    }
50
    
51
    public static function history( $atts, $content = null ) {
0 ignored issues
show
Unused Code introduced by
The parameter $content is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
52
        return self::shortcode_wrapper( array( __CLASS__, 'history_output' ), $atts );
53
    }
54
55
    /**
56
     * Output the shortcode.
57
     *
58
     * @param array $atts
59
     */
60
    public static function history_output( $atts ) {
61
        do_action( 'wpinv_before_user_invoice_history' );
62
        wpinv_get_template_part( 'wpinv-invoice-history', $atts );
63
        do_action( 'wpinv_after_user_invoice_history' );
64
    }
65
    
66
    public static function success( $atts, $content = null ) {
67
        return self::shortcode_wrapper( array( __CLASS__, 'success_output' ), $atts, $content );
68
    }
69
    
70
    /**
71
     * Output the shortcode.
72
     *
73
     * @param array $atts
74
     */
75
    public static function success_output( $atts, $content = null ) {
76
        do_action( 'wpinv_success_content_before' );
77
        echo wpinv_payment_receipt( $atts, $content );
78
        do_action( 'wpinv_success_content_after' );
79
    }
80
81
    public static function buy( $atts, $content = null ) {
0 ignored issues
show
Unused Code introduced by
The parameter $content is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
82
        $a = shortcode_atts( array(
83
            'items'     => '', // should be used like: item_id|quantity,item_id|quantity,item_id|quantity
84
            'title'     => __( 'Buy Now', 'invoicing' ), // the button title
85
            'post_id'   => '', // any related post_id
86
        ), $atts );
87
88
        $post_id = isset( $a['post_id'] ) ? (int)$a['post_id'] : '';
89
90
        $html = '<div class="wpi-buy-button-wrapper wpi-g">';
91
        $html .= '<button class="button button-primary wpi-buy-button" type="button" onclick="wpi_buy(this,\'' . $a['items'] . '\',' . $post_id . ');">' . $a['title'] . '</button>';
92
        $html .= wp_nonce_field( 'wpinv_buy_items', 'wpinv_buy_nonce', true, false );
93
        $html .= '</div>';
94
95
        return $html;
96
    }
97
}
98