@@ -9,7 +9,7 @@ discard block |
||
| 9 | 9 | |
| 10 | 10 | // Load WP_List_Table if not loaded |
| 11 | 11 | if ( ! class_exists( 'WP_List_Table' ) ) { |
| 12 | - require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
|
| 12 | + require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
|
| 13 | 13 | } |
| 14 | 14 | |
| 15 | 15 | /** |
@@ -21,129 +21,129 @@ discard block |
||
| 21 | 21 | */ |
| 22 | 22 | class WPInv_Items_Report_Table extends WP_List_Table { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * @var int Number of items per page |
|
| 26 | - * @since 1.0.19 |
|
| 27 | - */ |
|
| 28 | - public $per_page = 300; |
|
| 29 | - |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Get things started |
|
| 33 | - * |
|
| 34 | - * @since 1.0.19 |
|
| 35 | - * @see WP_List_Table::__construct() |
|
| 36 | - */ |
|
| 37 | - public function __construct() { |
|
| 38 | - |
|
| 39 | - // Set parent defaults |
|
| 40 | - parent::__construct( array( |
|
| 41 | - 'singular' => 'id', |
|
| 42 | - 'plural' => 'ids', |
|
| 43 | - 'ajax' => false, |
|
| 44 | - ) ); |
|
| 45 | - |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Gets the name of the primary column. |
|
| 50 | - * |
|
| 51 | - * @since 1.0.19 |
|
| 52 | - * @access protected |
|
| 53 | - * |
|
| 54 | - * @return string Name of the primary column. |
|
| 55 | - */ |
|
| 56 | - protected function get_primary_column_name() { |
|
| 57 | - return 'item'; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * This function renders most of the columns in the list table. |
|
| 62 | - * |
|
| 63 | - * @since 1.0.19 |
|
| 64 | - * |
|
| 65 | - * @param array $item Contains all the data of the gateways |
|
| 66 | - * @param string $column_name The name of the column |
|
| 67 | - * |
|
| 68 | - * @return string Column Name |
|
| 69 | - */ |
|
| 70 | - public function column_default( $item, $column_name ) { |
|
| 71 | - return wp_kses_post( $item[ $column_name ] ); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Retrieve the table columns |
|
| 76 | - * |
|
| 77 | - * @since 1.0.19 |
|
| 78 | - * @return array $columns Array of all the list table columns |
|
| 79 | - */ |
|
| 80 | - public function get_columns() { |
|
| 81 | - |
|
| 82 | - return array( |
|
| 83 | - 'item' => __( 'Item', 'invoicing' ), |
|
| 84 | - 'sales' => __( 'Quantity Sold', 'invoicing' ), |
|
| 85 | - 'total' => __( 'Total Earnings', 'invoicing' ), |
|
| 86 | - 'discount' => __( 'Total Discounts', 'invoicing' ), |
|
| 87 | - 'tax' => __( 'Total Taxes', 'invoicing' ), |
|
| 88 | - ); |
|
| 89 | - |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Retrieve the current page number |
|
| 94 | - * |
|
| 95 | - * @since 1.0.19 |
|
| 96 | - * @return int Current page number |
|
| 97 | - */ |
|
| 98 | - public function get_paged() { |
|
| 99 | - return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Outputs the reporting views |
|
| 104 | - * |
|
| 105 | - * @since 1.0.19 |
|
| 106 | - * @return void |
|
| 107 | - */ |
|
| 108 | - public function bulk_actions( $which = '' ) { |
|
| 109 | - return array(); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Build all the reports data |
|
| 114 | - * |
|
| 115 | - * @since 1.0.19 |
|
| 116 | - * @return array $reports_data All the data for gateway reports |
|
| 117 | - */ |
|
| 118 | - public function reports_data() { |
|
| 119 | - |
|
| 120 | - $reports_data = $this->revenue_reports_data(); |
|
| 121 | - |
|
| 122 | - $prepared = array(); |
|
| 123 | - foreach ( $reports_data as $report_data ) { |
|
| 124 | - $prepared[] = array( |
|
| 125 | - 'item' => $report_data['item_name'], |
|
| 126 | - 'sales' => $report_data['sales'], |
|
| 127 | - 'total' => wpinv_price( wpinv_format_amount( $report_data['total'] ) ), |
|
| 128 | - 'discount' => wpinv_price( wpinv_format_amount( $report_data['discount'] ) ), |
|
| 129 | - 'tax' => wpinv_price( wpinv_format_amount( $report_data['tax'] ) ), |
|
| 130 | - ); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - return $prepared; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Retrieves report data. |
|
| 138 | - * |
|
| 139 | - * @since 1.0.19 |
|
| 140 | - */ |
|
| 141 | - public function revenue_reports_data() { |
|
| 142 | - global $wpdb; |
|
| 143 | - |
|
| 144 | - $table = $wpdb->prefix . 'getpaid_invoice_items'; |
|
| 145 | - return $wpdb->get_results( |
|
| 146 | - "SELECT |
|
| 24 | + /** |
|
| 25 | + * @var int Number of items per page |
|
| 26 | + * @since 1.0.19 |
|
| 27 | + */ |
|
| 28 | + public $per_page = 300; |
|
| 29 | + |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Get things started |
|
| 33 | + * |
|
| 34 | + * @since 1.0.19 |
|
| 35 | + * @see WP_List_Table::__construct() |
|
| 36 | + */ |
|
| 37 | + public function __construct() { |
|
| 38 | + |
|
| 39 | + // Set parent defaults |
|
| 40 | + parent::__construct( array( |
|
| 41 | + 'singular' => 'id', |
|
| 42 | + 'plural' => 'ids', |
|
| 43 | + 'ajax' => false, |
|
| 44 | + ) ); |
|
| 45 | + |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Gets the name of the primary column. |
|
| 50 | + * |
|
| 51 | + * @since 1.0.19 |
|
| 52 | + * @access protected |
|
| 53 | + * |
|
| 54 | + * @return string Name of the primary column. |
|
| 55 | + */ |
|
| 56 | + protected function get_primary_column_name() { |
|
| 57 | + return 'item'; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * This function renders most of the columns in the list table. |
|
| 62 | + * |
|
| 63 | + * @since 1.0.19 |
|
| 64 | + * |
|
| 65 | + * @param array $item Contains all the data of the gateways |
|
| 66 | + * @param string $column_name The name of the column |
|
| 67 | + * |
|
| 68 | + * @return string Column Name |
|
| 69 | + */ |
|
| 70 | + public function column_default( $item, $column_name ) { |
|
| 71 | + return wp_kses_post( $item[ $column_name ] ); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Retrieve the table columns |
|
| 76 | + * |
|
| 77 | + * @since 1.0.19 |
|
| 78 | + * @return array $columns Array of all the list table columns |
|
| 79 | + */ |
|
| 80 | + public function get_columns() { |
|
| 81 | + |
|
| 82 | + return array( |
|
| 83 | + 'item' => __( 'Item', 'invoicing' ), |
|
| 84 | + 'sales' => __( 'Quantity Sold', 'invoicing' ), |
|
| 85 | + 'total' => __( 'Total Earnings', 'invoicing' ), |
|
| 86 | + 'discount' => __( 'Total Discounts', 'invoicing' ), |
|
| 87 | + 'tax' => __( 'Total Taxes', 'invoicing' ), |
|
| 88 | + ); |
|
| 89 | + |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Retrieve the current page number |
|
| 94 | + * |
|
| 95 | + * @since 1.0.19 |
|
| 96 | + * @return int Current page number |
|
| 97 | + */ |
|
| 98 | + public function get_paged() { |
|
| 99 | + return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Outputs the reporting views |
|
| 104 | + * |
|
| 105 | + * @since 1.0.19 |
|
| 106 | + * @return void |
|
| 107 | + */ |
|
| 108 | + public function bulk_actions( $which = '' ) { |
|
| 109 | + return array(); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Build all the reports data |
|
| 114 | + * |
|
| 115 | + * @since 1.0.19 |
|
| 116 | + * @return array $reports_data All the data for gateway reports |
|
| 117 | + */ |
|
| 118 | + public function reports_data() { |
|
| 119 | + |
|
| 120 | + $reports_data = $this->revenue_reports_data(); |
|
| 121 | + |
|
| 122 | + $prepared = array(); |
|
| 123 | + foreach ( $reports_data as $report_data ) { |
|
| 124 | + $prepared[] = array( |
|
| 125 | + 'item' => $report_data['item_name'], |
|
| 126 | + 'sales' => $report_data['sales'], |
|
| 127 | + 'total' => wpinv_price( wpinv_format_amount( $report_data['total'] ) ), |
|
| 128 | + 'discount' => wpinv_price( wpinv_format_amount( $report_data['discount'] ) ), |
|
| 129 | + 'tax' => wpinv_price( wpinv_format_amount( $report_data['tax'] ) ), |
|
| 130 | + ); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + return $prepared; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Retrieves report data. |
|
| 138 | + * |
|
| 139 | + * @since 1.0.19 |
|
| 140 | + */ |
|
| 141 | + public function revenue_reports_data() { |
|
| 142 | + global $wpdb; |
|
| 143 | + |
|
| 144 | + $table = $wpdb->prefix . 'getpaid_invoice_items'; |
|
| 145 | + return $wpdb->get_results( |
|
| 146 | + "SELECT |
|
| 147 | 147 | SUM(quantity) as sales, |
| 148 | 148 | item_name, |
| 149 | 149 | SUM(tax) as tax, |
@@ -157,19 +157,19 @@ discard block |
||
| 157 | 157 | GROUP BY item_id |
| 158 | 158 | ORDER BY item_name ASC", ARRAY_A); |
| 159 | 159 | |
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Setup the final data for the table |
|
| 164 | - * |
|
| 165 | - * @since 1.0.19 |
|
| 166 | - * @return void |
|
| 167 | - */ |
|
| 168 | - public function prepare_items() { |
|
| 169 | - $columns = $this->get_columns(); |
|
| 170 | - $hidden = array(); // No hidden columns |
|
| 171 | - $sortable = $this->get_sortable_columns(); |
|
| 172 | - $this->_column_headers = array( $columns, $hidden, $sortable ); |
|
| 173 | - $this->items = $this->reports_data(); |
|
| 174 | - } |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Setup the final data for the table |
|
| 164 | + * |
|
| 165 | + * @since 1.0.19 |
|
| 166 | + * @return void |
|
| 167 | + */ |
|
| 168 | + public function prepare_items() { |
|
| 169 | + $columns = $this->get_columns(); |
|
| 170 | + $hidden = array(); // No hidden columns |
|
| 171 | + $sortable = $this->get_sortable_columns(); |
|
| 172 | + $this->_column_headers = array( $columns, $hidden, $sortable ); |
|
| 173 | + $this->items = $this->reports_data(); |
|
| 174 | + } |
|
| 175 | 175 | } |
@@ -9,7 +9,7 @@ discard block |
||
| 9 | 9 | |
| 10 | 10 | // Load WP_List_Table if not loaded |
| 11 | 11 | if ( ! class_exists( 'WP_List_Table' ) ) { |
| 12 | - require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
|
| 12 | + require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
|
| 13 | 13 | } |
| 14 | 14 | |
| 15 | 15 | /** |
@@ -21,148 +21,148 @@ discard block |
||
| 21 | 21 | */ |
| 22 | 22 | class WPInv_Gateways_Report_Table extends WP_List_Table { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * @var int Number of items per page |
|
| 26 | - * @since 1.0.19 |
|
| 27 | - */ |
|
| 28 | - public $per_page = 300; |
|
| 29 | - |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Get things started |
|
| 33 | - * |
|
| 34 | - * @since 1.0.19 |
|
| 35 | - * @see WP_List_Table::__construct() |
|
| 36 | - */ |
|
| 37 | - public function __construct() { |
|
| 38 | - |
|
| 39 | - // Set parent defaults |
|
| 40 | - parent::__construct( array( |
|
| 41 | - 'singular' => 'id', |
|
| 42 | - 'plural' => 'ids', |
|
| 43 | - 'ajax' => false, |
|
| 44 | - ) ); |
|
| 45 | - |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Gets the name of the primary column. |
|
| 50 | - * |
|
| 51 | - * @since 1.0.19 |
|
| 52 | - * @access protected |
|
| 53 | - * |
|
| 54 | - * @return string Name of the primary column. |
|
| 55 | - */ |
|
| 56 | - protected function get_primary_column_name() { |
|
| 57 | - return 'gateway'; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * This function renders most of the columns in the list table. |
|
| 62 | - * |
|
| 63 | - * @since 1.0.19 |
|
| 64 | - * |
|
| 65 | - * @param array $item Contains all the data of the gateways |
|
| 66 | - * @param string $column_name The name of the column |
|
| 67 | - * |
|
| 68 | - * @return string Column Name |
|
| 69 | - */ |
|
| 70 | - public function column_default( $item, $column_name ) { |
|
| 71 | - return wp_kses_post( $item[ $column_name ] ); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Retrieve the table columns |
|
| 76 | - * |
|
| 77 | - * @since 1.0.19 |
|
| 78 | - * @return array $columns Array of all the list table columns |
|
| 79 | - */ |
|
| 80 | - public function get_columns() { |
|
| 81 | - |
|
| 82 | - return array( |
|
| 83 | - 'gateway' => __( 'Gateway', 'invoicing' ), |
|
| 84 | - 'sales' => __( 'Total Sales', 'invoicing' ), |
|
| 85 | - 'total' => __( 'Total Earnings', 'invoicing' ), |
|
| 86 | - 'discount' => __( 'Total Discounts', 'invoicing' ), |
|
| 87 | - 'tax' => __( 'Total Taxes', 'invoicing' ), |
|
| 88 | - 'fees' => __( 'Total Fees', 'invoicing' ), |
|
| 89 | - ); |
|
| 90 | - |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Retrieve the current page number |
|
| 95 | - * |
|
| 96 | - * @since 1.0.19 |
|
| 97 | - * @return int Current page number |
|
| 98 | - */ |
|
| 99 | - public function get_paged() { |
|
| 100 | - return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Outputs the reporting views |
|
| 105 | - * |
|
| 106 | - * @since 1.0.19 |
|
| 107 | - * @return void |
|
| 108 | - */ |
|
| 109 | - public function bulk_actions( $which = '' ) { |
|
| 110 | - return array(); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * Build all the reports data |
|
| 115 | - * |
|
| 116 | - * @since 1.0.19 |
|
| 117 | - * @return array $reports_data All the data for gateway reports |
|
| 118 | - */ |
|
| 119 | - public function reports_data() { |
|
| 120 | - |
|
| 121 | - $reports_data = $this->revenue_reports_data(); |
|
| 122 | - $gateways = wpinv_get_payment_gateways(); |
|
| 123 | - |
|
| 124 | - foreach ( $gateways as $gateway_id => $gateway ) { |
|
| 125 | - |
|
| 126 | - if ( ! empty( $reports_data[ $gateway_id ] ) ) { |
|
| 127 | - continue; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - $reports_data[] = array( |
|
| 131 | - 'gateway' => $gateway_id, |
|
| 132 | - 'sales' => 0, |
|
| 133 | - 'total' => 0, |
|
| 134 | - 'discount' => 0, |
|
| 135 | - 'tax' => 0, |
|
| 136 | - 'fees' => 0, |
|
| 137 | - ); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - $prepared = array(); |
|
| 141 | - foreach ( $reports_data as $report_data ) { |
|
| 142 | - $prepared[] = array( |
|
| 143 | - 'gateway' => wpinv_get_gateway_admin_label( $report_data['gateway'] ), |
|
| 144 | - 'sales' => $report_data['sales'], |
|
| 145 | - 'total' => wpinv_price( wpinv_format_amount( $report_data['total'] ) ), |
|
| 146 | - 'discount' => wpinv_price( wpinv_format_amount( $report_data['discount'] ) ), |
|
| 147 | - 'tax' => wpinv_price( wpinv_format_amount( $report_data['tax'] ) ), |
|
| 148 | - 'fees' => wpinv_price( wpinv_format_amount( $report_data['fees'] ) ), |
|
| 149 | - ); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - return $prepared; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * Retrieves report data. |
|
| 157 | - * |
|
| 158 | - * @since 1.0.19 |
|
| 159 | - */ |
|
| 160 | - public function revenue_reports_data() { |
|
| 161 | - global $wpdb; |
|
| 162 | - |
|
| 163 | - $table = $wpdb->prefix . 'getpaid_invoices'; |
|
| 164 | - $data = $wpdb->get_results( |
|
| 165 | - "SELECT |
|
| 24 | + /** |
|
| 25 | + * @var int Number of items per page |
|
| 26 | + * @since 1.0.19 |
|
| 27 | + */ |
|
| 28 | + public $per_page = 300; |
|
| 29 | + |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Get things started |
|
| 33 | + * |
|
| 34 | + * @since 1.0.19 |
|
| 35 | + * @see WP_List_Table::__construct() |
|
| 36 | + */ |
|
| 37 | + public function __construct() { |
|
| 38 | + |
|
| 39 | + // Set parent defaults |
|
| 40 | + parent::__construct( array( |
|
| 41 | + 'singular' => 'id', |
|
| 42 | + 'plural' => 'ids', |
|
| 43 | + 'ajax' => false, |
|
| 44 | + ) ); |
|
| 45 | + |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Gets the name of the primary column. |
|
| 50 | + * |
|
| 51 | + * @since 1.0.19 |
|
| 52 | + * @access protected |
|
| 53 | + * |
|
| 54 | + * @return string Name of the primary column. |
|
| 55 | + */ |
|
| 56 | + protected function get_primary_column_name() { |
|
| 57 | + return 'gateway'; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * This function renders most of the columns in the list table. |
|
| 62 | + * |
|
| 63 | + * @since 1.0.19 |
|
| 64 | + * |
|
| 65 | + * @param array $item Contains all the data of the gateways |
|
| 66 | + * @param string $column_name The name of the column |
|
| 67 | + * |
|
| 68 | + * @return string Column Name |
|
| 69 | + */ |
|
| 70 | + public function column_default( $item, $column_name ) { |
|
| 71 | + return wp_kses_post( $item[ $column_name ] ); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Retrieve the table columns |
|
| 76 | + * |
|
| 77 | + * @since 1.0.19 |
|
| 78 | + * @return array $columns Array of all the list table columns |
|
| 79 | + */ |
|
| 80 | + public function get_columns() { |
|
| 81 | + |
|
| 82 | + return array( |
|
| 83 | + 'gateway' => __( 'Gateway', 'invoicing' ), |
|
| 84 | + 'sales' => __( 'Total Sales', 'invoicing' ), |
|
| 85 | + 'total' => __( 'Total Earnings', 'invoicing' ), |
|
| 86 | + 'discount' => __( 'Total Discounts', 'invoicing' ), |
|
| 87 | + 'tax' => __( 'Total Taxes', 'invoicing' ), |
|
| 88 | + 'fees' => __( 'Total Fees', 'invoicing' ), |
|
| 89 | + ); |
|
| 90 | + |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Retrieve the current page number |
|
| 95 | + * |
|
| 96 | + * @since 1.0.19 |
|
| 97 | + * @return int Current page number |
|
| 98 | + */ |
|
| 99 | + public function get_paged() { |
|
| 100 | + return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Outputs the reporting views |
|
| 105 | + * |
|
| 106 | + * @since 1.0.19 |
|
| 107 | + * @return void |
|
| 108 | + */ |
|
| 109 | + public function bulk_actions( $which = '' ) { |
|
| 110 | + return array(); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * Build all the reports data |
|
| 115 | + * |
|
| 116 | + * @since 1.0.19 |
|
| 117 | + * @return array $reports_data All the data for gateway reports |
|
| 118 | + */ |
|
| 119 | + public function reports_data() { |
|
| 120 | + |
|
| 121 | + $reports_data = $this->revenue_reports_data(); |
|
| 122 | + $gateways = wpinv_get_payment_gateways(); |
|
| 123 | + |
|
| 124 | + foreach ( $gateways as $gateway_id => $gateway ) { |
|
| 125 | + |
|
| 126 | + if ( ! empty( $reports_data[ $gateway_id ] ) ) { |
|
| 127 | + continue; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + $reports_data[] = array( |
|
| 131 | + 'gateway' => $gateway_id, |
|
| 132 | + 'sales' => 0, |
|
| 133 | + 'total' => 0, |
|
| 134 | + 'discount' => 0, |
|
| 135 | + 'tax' => 0, |
|
| 136 | + 'fees' => 0, |
|
| 137 | + ); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + $prepared = array(); |
|
| 141 | + foreach ( $reports_data as $report_data ) { |
|
| 142 | + $prepared[] = array( |
|
| 143 | + 'gateway' => wpinv_get_gateway_admin_label( $report_data['gateway'] ), |
|
| 144 | + 'sales' => $report_data['sales'], |
|
| 145 | + 'total' => wpinv_price( wpinv_format_amount( $report_data['total'] ) ), |
|
| 146 | + 'discount' => wpinv_price( wpinv_format_amount( $report_data['discount'] ) ), |
|
| 147 | + 'tax' => wpinv_price( wpinv_format_amount( $report_data['tax'] ) ), |
|
| 148 | + 'fees' => wpinv_price( wpinv_format_amount( $report_data['fees'] ) ), |
|
| 149 | + ); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + return $prepared; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * Retrieves report data. |
|
| 157 | + * |
|
| 158 | + * @since 1.0.19 |
|
| 159 | + */ |
|
| 160 | + public function revenue_reports_data() { |
|
| 161 | + global $wpdb; |
|
| 162 | + |
|
| 163 | + $table = $wpdb->prefix . 'getpaid_invoices'; |
|
| 164 | + $data = $wpdb->get_results( |
|
| 165 | + "SELECT |
|
| 166 | 166 | COUNT(posts.ID) as sales, |
| 167 | 167 | meta.gateway as gateway, |
| 168 | 168 | SUM(meta.total) as total, |
@@ -177,27 +177,27 @@ discard block |
||
| 177 | 177 | AND ( posts.post_status = 'publish' OR posts.post_status = 'renewal' ) |
| 178 | 178 | GROUP BY meta.gateway", ARRAY_A); |
| 179 | 179 | |
| 180 | - $return = array(); |
|
| 181 | - |
|
| 182 | - foreach ( $data as $gateway ) { |
|
| 183 | - $return[ $gateway ['gateway']] = $gateway; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - return $return; |
|
| 187 | - |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * Setup the final data for the table |
|
| 192 | - * |
|
| 193 | - * @since 1.0.19 |
|
| 194 | - * @return void |
|
| 195 | - */ |
|
| 196 | - public function prepare_items() { |
|
| 197 | - $columns = $this->get_columns(); |
|
| 198 | - $hidden = array(); // No hidden columns |
|
| 199 | - $sortable = $this->get_sortable_columns(); |
|
| 200 | - $this->_column_headers = array( $columns, $hidden, $sortable ); |
|
| 201 | - $this->items = $this->reports_data(); |
|
| 202 | - } |
|
| 180 | + $return = array(); |
|
| 181 | + |
|
| 182 | + foreach ( $data as $gateway ) { |
|
| 183 | + $return[ $gateway ['gateway']] = $gateway; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + return $return; |
|
| 187 | + |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * Setup the final data for the table |
|
| 192 | + * |
|
| 193 | + * @since 1.0.19 |
|
| 194 | + * @return void |
|
| 195 | + */ |
|
| 196 | + public function prepare_items() { |
|
| 197 | + $columns = $this->get_columns(); |
|
| 198 | + $hidden = array(); // No hidden columns |
|
| 199 | + $sortable = $this->get_sortable_columns(); |
|
| 200 | + $this->_column_headers = array( $columns, $hidden, $sortable ); |
|
| 201 | + $this->items = $this->reports_data(); |
|
| 202 | + } |
|
| 203 | 203 | } |
@@ -9,7 +9,7 @@ discard block |
||
| 9 | 9 | |
| 10 | 10 | // Load WP_List_Table if not loaded |
| 11 | 11 | if ( ! class_exists( 'WP_List_Table' ) ) { |
| 12 | - require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
|
| 12 | + require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
|
| 13 | 13 | } |
| 14 | 14 | |
| 15 | 15 | /** |
@@ -21,145 +21,145 @@ discard block |
||
| 21 | 21 | */ |
| 22 | 22 | class WPInv_Taxes_Reports_Table extends WP_List_Table { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * @var int Number of items per page |
|
| 26 | - * @since 1.0.19 |
|
| 27 | - */ |
|
| 28 | - public $per_page = 300; |
|
| 29 | - |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Get things started |
|
| 33 | - * |
|
| 34 | - * @since 1.0.19 |
|
| 35 | - * @see WP_List_Table::__construct() |
|
| 36 | - */ |
|
| 37 | - public function __construct() { |
|
| 38 | - |
|
| 39 | - // Set parent defaults |
|
| 40 | - parent::__construct( array( |
|
| 41 | - 'singular' => 'id', |
|
| 42 | - 'plural' => 'ids', |
|
| 43 | - 'ajax' => false, |
|
| 44 | - ) ); |
|
| 45 | - |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Gets the name of the primary column. |
|
| 50 | - * |
|
| 51 | - * @since 1.0.19 |
|
| 52 | - * @access protected |
|
| 53 | - * |
|
| 54 | - * @return string Name of the primary column. |
|
| 55 | - */ |
|
| 56 | - protected function get_primary_column_name() { |
|
| 57 | - return 'month'; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * This function renders most of the columns in the list table. |
|
| 62 | - * |
|
| 63 | - * @since 1.0.19 |
|
| 64 | - * |
|
| 65 | - * @param array $item Contains all the data of the gateways |
|
| 66 | - * @param string $column_name The name of the column |
|
| 67 | - * |
|
| 68 | - * @return string Column Name |
|
| 69 | - */ |
|
| 70 | - public function column_default( $item, $column_name ) { |
|
| 71 | - return wp_kses_post( $item[ $column_name ] ); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Retrieve the table columns |
|
| 76 | - * |
|
| 77 | - * @since 1.0.19 |
|
| 78 | - * @return array $columns Array of all the list table columns |
|
| 79 | - */ |
|
| 80 | - public function get_columns() { |
|
| 81 | - |
|
| 82 | - return array( |
|
| 83 | - 'month' => __( 'Month', 'invoicing' ), |
|
| 84 | - 'tax' => __( 'Total Taxes', 'invoicing' ), |
|
| 85 | - ); |
|
| 86 | - |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Retrieve the current page number |
|
| 91 | - * |
|
| 92 | - * @since 1.0.19 |
|
| 93 | - * @return int Current page number |
|
| 94 | - */ |
|
| 95 | - public function get_paged() { |
|
| 96 | - return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Outputs the reporting views |
|
| 101 | - * |
|
| 102 | - * @since 1.0.19 |
|
| 103 | - * @return void |
|
| 104 | - */ |
|
| 105 | - public function bulk_actions( $which = '' ) { |
|
| 106 | - return array(); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Build all the reports data |
|
| 111 | - * |
|
| 112 | - * @since 1.0.19 |
|
| 113 | - * @return array $reports_data All the data for taxes reports |
|
| 114 | - */ |
|
| 115 | - public function reports_data() { |
|
| 116 | - |
|
| 117 | - $reports_data = $this->taxes_reports_data(); |
|
| 118 | - $months = array( |
|
| 119 | - '1' => __( 'January', 'invoicing' ), |
|
| 120 | - '2' => __( 'February', 'invoicing' ), |
|
| 121 | - '3' => __( 'March', 'invoicing' ), |
|
| 122 | - '4' => __( 'April', 'invoicing' ), |
|
| 123 | - '5' => __( 'May', 'invoicing' ), |
|
| 124 | - '6' => __( 'June', 'invoicing' ), |
|
| 125 | - '7' => __( 'July', 'invoicing' ), |
|
| 126 | - '8' => __( 'August', 'invoicing' ), |
|
| 127 | - '9' => __( 'September', 'invoicing' ), |
|
| 128 | - '10' => __( 'October', 'invoicing' ), |
|
| 129 | - '11' => __( 'November', 'invoicing' ), |
|
| 130 | - '12' => __( 'December', 'invoicing' ), |
|
| 131 | - ); |
|
| 132 | - |
|
| 133 | - $prepared = array(); |
|
| 134 | - foreach ( $months as $month => $label ) { |
|
| 135 | - |
|
| 136 | - $tax = wpinv_price( 0 ); |
|
| 137 | - if ( ! empty( $reports_data[ $month ] ) ) { |
|
| 138 | - $tax = wpinv_price( wpinv_format_amount( $reports_data[ $month ] ) ); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - $prepared[] = array( |
|
| 142 | - 'month' => $label, |
|
| 143 | - 'tax' => $tax, |
|
| 144 | - ); |
|
| 145 | - |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - return $prepared; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * Retrieves taxes data. |
|
| 153 | - * |
|
| 154 | - * @since 1.0.19 |
|
| 155 | - */ |
|
| 156 | - public function taxes_reports_data() { |
|
| 157 | - global $wpdb; |
|
| 158 | - |
|
| 159 | - $table = $wpdb->prefix . 'getpaid_invoices'; |
|
| 160 | - $year = isset( $_GET['year'] ) ? absint( $_GET['year'] ) : date( 'Y' ); |
|
| 161 | - $data = $wpdb->get_results( |
|
| 162 | - "SELECT |
|
| 24 | + /** |
|
| 25 | + * @var int Number of items per page |
|
| 26 | + * @since 1.0.19 |
|
| 27 | + */ |
|
| 28 | + public $per_page = 300; |
|
| 29 | + |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Get things started |
|
| 33 | + * |
|
| 34 | + * @since 1.0.19 |
|
| 35 | + * @see WP_List_Table::__construct() |
|
| 36 | + */ |
|
| 37 | + public function __construct() { |
|
| 38 | + |
|
| 39 | + // Set parent defaults |
|
| 40 | + parent::__construct( array( |
|
| 41 | + 'singular' => 'id', |
|
| 42 | + 'plural' => 'ids', |
|
| 43 | + 'ajax' => false, |
|
| 44 | + ) ); |
|
| 45 | + |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Gets the name of the primary column. |
|
| 50 | + * |
|
| 51 | + * @since 1.0.19 |
|
| 52 | + * @access protected |
|
| 53 | + * |
|
| 54 | + * @return string Name of the primary column. |
|
| 55 | + */ |
|
| 56 | + protected function get_primary_column_name() { |
|
| 57 | + return 'month'; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * This function renders most of the columns in the list table. |
|
| 62 | + * |
|
| 63 | + * @since 1.0.19 |
|
| 64 | + * |
|
| 65 | + * @param array $item Contains all the data of the gateways |
|
| 66 | + * @param string $column_name The name of the column |
|
| 67 | + * |
|
| 68 | + * @return string Column Name |
|
| 69 | + */ |
|
| 70 | + public function column_default( $item, $column_name ) { |
|
| 71 | + return wp_kses_post( $item[ $column_name ] ); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Retrieve the table columns |
|
| 76 | + * |
|
| 77 | + * @since 1.0.19 |
|
| 78 | + * @return array $columns Array of all the list table columns |
|
| 79 | + */ |
|
| 80 | + public function get_columns() { |
|
| 81 | + |
|
| 82 | + return array( |
|
| 83 | + 'month' => __( 'Month', 'invoicing' ), |
|
| 84 | + 'tax' => __( 'Total Taxes', 'invoicing' ), |
|
| 85 | + ); |
|
| 86 | + |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Retrieve the current page number |
|
| 91 | + * |
|
| 92 | + * @since 1.0.19 |
|
| 93 | + * @return int Current page number |
|
| 94 | + */ |
|
| 95 | + public function get_paged() { |
|
| 96 | + return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Outputs the reporting views |
|
| 101 | + * |
|
| 102 | + * @since 1.0.19 |
|
| 103 | + * @return void |
|
| 104 | + */ |
|
| 105 | + public function bulk_actions( $which = '' ) { |
|
| 106 | + return array(); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Build all the reports data |
|
| 111 | + * |
|
| 112 | + * @since 1.0.19 |
|
| 113 | + * @return array $reports_data All the data for taxes reports |
|
| 114 | + */ |
|
| 115 | + public function reports_data() { |
|
| 116 | + |
|
| 117 | + $reports_data = $this->taxes_reports_data(); |
|
| 118 | + $months = array( |
|
| 119 | + '1' => __( 'January', 'invoicing' ), |
|
| 120 | + '2' => __( 'February', 'invoicing' ), |
|
| 121 | + '3' => __( 'March', 'invoicing' ), |
|
| 122 | + '4' => __( 'April', 'invoicing' ), |
|
| 123 | + '5' => __( 'May', 'invoicing' ), |
|
| 124 | + '6' => __( 'June', 'invoicing' ), |
|
| 125 | + '7' => __( 'July', 'invoicing' ), |
|
| 126 | + '8' => __( 'August', 'invoicing' ), |
|
| 127 | + '9' => __( 'September', 'invoicing' ), |
|
| 128 | + '10' => __( 'October', 'invoicing' ), |
|
| 129 | + '11' => __( 'November', 'invoicing' ), |
|
| 130 | + '12' => __( 'December', 'invoicing' ), |
|
| 131 | + ); |
|
| 132 | + |
|
| 133 | + $prepared = array(); |
|
| 134 | + foreach ( $months as $month => $label ) { |
|
| 135 | + |
|
| 136 | + $tax = wpinv_price( 0 ); |
|
| 137 | + if ( ! empty( $reports_data[ $month ] ) ) { |
|
| 138 | + $tax = wpinv_price( wpinv_format_amount( $reports_data[ $month ] ) ); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + $prepared[] = array( |
|
| 142 | + 'month' => $label, |
|
| 143 | + 'tax' => $tax, |
|
| 144 | + ); |
|
| 145 | + |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + return $prepared; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * Retrieves taxes data. |
|
| 153 | + * |
|
| 154 | + * @since 1.0.19 |
|
| 155 | + */ |
|
| 156 | + public function taxes_reports_data() { |
|
| 157 | + global $wpdb; |
|
| 158 | + |
|
| 159 | + $table = $wpdb->prefix . 'getpaid_invoices'; |
|
| 160 | + $year = isset( $_GET['year'] ) ? absint( $_GET['year'] ) : date( 'Y' ); |
|
| 161 | + $data = $wpdb->get_results( |
|
| 162 | + "SELECT |
|
| 163 | 163 | MONTH(meta.completed_date) as _month, |
| 164 | 164 | SUM(meta.tax) as tax |
| 165 | 165 | FROM $wpdb->posts as posts |
@@ -171,21 +171,21 @@ discard block |
||
| 171 | 171 | AND ( YEAR(meta.completed_date) = '$year' ) |
| 172 | 172 | GROUP BY MONTH(meta.completed_date)"); |
| 173 | 173 | |
| 174 | - return wp_list_pluck( $data, 'tax', '_month' ); |
|
| 175 | - |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Setup the final data for the table |
|
| 180 | - * |
|
| 181 | - * @since 1.0.19 |
|
| 182 | - * @return void |
|
| 183 | - */ |
|
| 184 | - public function prepare_items() { |
|
| 185 | - $columns = $this->get_columns(); |
|
| 186 | - $hidden = array(); // No hidden columns |
|
| 187 | - $sortable = $this->get_sortable_columns(); |
|
| 188 | - $this->_column_headers = array( $columns, $hidden, $sortable ); |
|
| 189 | - $this->items = $this->reports_data(); |
|
| 190 | - } |
|
| 174 | + return wp_list_pluck( $data, 'tax', '_month' ); |
|
| 175 | + |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Setup the final data for the table |
|
| 180 | + * |
|
| 181 | + * @since 1.0.19 |
|
| 182 | + * @return void |
|
| 183 | + */ |
|
| 184 | + public function prepare_items() { |
|
| 185 | + $columns = $this->get_columns(); |
|
| 186 | + $hidden = array(); // No hidden columns |
|
| 187 | + $sortable = $this->get_sortable_columns(); |
|
| 188 | + $this->_column_headers = array( $columns, $hidden, $sortable ); |
|
| 189 | + $this->items = $this->reports_data(); |
|
| 190 | + } |
|
| 191 | 191 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | - exit; |
|
| 3 | + exit; |
|
| 4 | 4 | } |
| 5 | 5 | |
| 6 | 6 | /** |
@@ -20,23 +20,23 @@ discard block |
||
| 20 | 20 | public $templates_url; |
| 21 | 21 | |
| 22 | 22 | /** |
| 23 | - * Class constructor. |
|
| 24 | - * |
|
| 25 | - * @since 1.0.19 |
|
| 26 | - */ |
|
| 27 | - public function __construct() { |
|
| 23 | + * Class constructor. |
|
| 24 | + * |
|
| 25 | + * @since 1.0.19 |
|
| 26 | + */ |
|
| 27 | + public function __construct() { |
|
| 28 | 28 | |
| 29 | 29 | $this->templates_dir = apply_filters( 'getpaid_default_templates_dir', WPINV_PLUGIN_DIR . 'templates' ); |
| 30 | 30 | $this->templates_url = apply_filters( 'getpaid_default_templates_url', WPINV_PLUGIN_URL . 'templates' ); |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | /** |
| 34 | - * Checks if this is a preview page |
|
| 35 | - * |
|
| 36 | - * @since 1.0.19 |
|
| 37 | - * @return bool |
|
| 38 | - */ |
|
| 39 | - public function is_preview() { |
|
| 34 | + * Checks if this is a preview page |
|
| 35 | + * |
|
| 36 | + * @since 1.0.19 |
|
| 37 | + * @return bool |
|
| 38 | + */ |
|
| 39 | + public function is_preview() { |
|
| 40 | 40 | return |
| 41 | 41 | $this->is_divi_preview() || |
| 42 | 42 | $this->is_elementor_preview() || |
@@ -48,73 +48,73 @@ discard block |
||
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | /** |
| 51 | - * Checks if this is an elementor preview page |
|
| 52 | - * |
|
| 53 | - * @since 1.0.19 |
|
| 54 | - * @return bool |
|
| 55 | - */ |
|
| 56 | - public function is_elementor_preview() { |
|
| 57 | - return isset( $_REQUEST['elementor-preview'] ) || ( is_admin() && isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'elementor' ) || ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'elementor_ajax' ); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Checks if this is a DIVI preview page |
|
| 62 | - * |
|
| 63 | - * @since 1.0.19 |
|
| 64 | - * @return bool |
|
| 65 | - */ |
|
| 66 | - public function is_divi_preview() { |
|
| 67 | - return isset( $_REQUEST['et_fb'] ) || isset( $_REQUEST['et_pb_preview'] ) || ( is_admin() && isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'et_pb' ); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Checks if this is a beaver builder preview page |
|
| 72 | - * |
|
| 73 | - * @since 1.0.19 |
|
| 74 | - * @return bool |
|
| 75 | - */ |
|
| 76 | - public function is_beaver_preview() { |
|
| 77 | - return isset( $_REQUEST['fl_builder'] ); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * Checks if this is a siteorigin builder preview page |
|
| 82 | - * |
|
| 83 | - * @since 1.0.19 |
|
| 84 | - * @return bool |
|
| 85 | - */ |
|
| 86 | - public function is_siteorigin_preview() { |
|
| 87 | - return ! empty( $_REQUEST['siteorigin_panels_live_editor'] ); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Checks if this is a cornerstone builder preview page |
|
| 92 | - * |
|
| 93 | - * @since 1.0.19 |
|
| 94 | - * @return bool |
|
| 95 | - */ |
|
| 96 | - public function is_cornerstone_preview() { |
|
| 97 | - return ! empty( $_REQUEST['cornerstone_preview'] ) || basename( $_SERVER['REQUEST_URI'] ) == 'cornerstone-endpoint'; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Checks if this is a fusion builder preview page |
|
| 102 | - * |
|
| 103 | - * @since 1.0.19 |
|
| 104 | - * @return bool |
|
| 105 | - */ |
|
| 106 | - public function is_fusion_preview() { |
|
| 107 | - return ! empty( $_REQUEST['fb-edit'] ) || ! empty( $_REQUEST['fusion_load_nonce'] ); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Checks if this is an oxygen builder preview page |
|
| 112 | - * |
|
| 113 | - * @since 1.0.19 |
|
| 114 | - * @return bool |
|
| 115 | - */ |
|
| 116 | - public function is_oxygen_preview() { |
|
| 117 | - return ! empty( $_REQUEST['ct_builder'] ) || ( ! empty( $_REQUEST['action'] ) && ( substr( $_REQUEST['action'], 0, 11 ) === "oxy_render_" || substr( $_REQUEST['action'], 0, 10 ) === "ct_render_" ) ); |
|
| 51 | + * Checks if this is an elementor preview page |
|
| 52 | + * |
|
| 53 | + * @since 1.0.19 |
|
| 54 | + * @return bool |
|
| 55 | + */ |
|
| 56 | + public function is_elementor_preview() { |
|
| 57 | + return isset( $_REQUEST['elementor-preview'] ) || ( is_admin() && isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'elementor' ) || ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'elementor_ajax' ); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Checks if this is a DIVI preview page |
|
| 62 | + * |
|
| 63 | + * @since 1.0.19 |
|
| 64 | + * @return bool |
|
| 65 | + */ |
|
| 66 | + public function is_divi_preview() { |
|
| 67 | + return isset( $_REQUEST['et_fb'] ) || isset( $_REQUEST['et_pb_preview'] ) || ( is_admin() && isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'et_pb' ); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Checks if this is a beaver builder preview page |
|
| 72 | + * |
|
| 73 | + * @since 1.0.19 |
|
| 74 | + * @return bool |
|
| 75 | + */ |
|
| 76 | + public function is_beaver_preview() { |
|
| 77 | + return isset( $_REQUEST['fl_builder'] ); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * Checks if this is a siteorigin builder preview page |
|
| 82 | + * |
|
| 83 | + * @since 1.0.19 |
|
| 84 | + * @return bool |
|
| 85 | + */ |
|
| 86 | + public function is_siteorigin_preview() { |
|
| 87 | + return ! empty( $_REQUEST['siteorigin_panels_live_editor'] ); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Checks if this is a cornerstone builder preview page |
|
| 92 | + * |
|
| 93 | + * @since 1.0.19 |
|
| 94 | + * @return bool |
|
| 95 | + */ |
|
| 96 | + public function is_cornerstone_preview() { |
|
| 97 | + return ! empty( $_REQUEST['cornerstone_preview'] ) || basename( $_SERVER['REQUEST_URI'] ) == 'cornerstone-endpoint'; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Checks if this is a fusion builder preview page |
|
| 102 | + * |
|
| 103 | + * @since 1.0.19 |
|
| 104 | + * @return bool |
|
| 105 | + */ |
|
| 106 | + public function is_fusion_preview() { |
|
| 107 | + return ! empty( $_REQUEST['fb-edit'] ) || ! empty( $_REQUEST['fusion_load_nonce'] ); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Checks if this is an oxygen builder preview page |
|
| 112 | + * |
|
| 113 | + * @since 1.0.19 |
|
| 114 | + * @return bool |
|
| 115 | + */ |
|
| 116 | + public function is_oxygen_preview() { |
|
| 117 | + return ! empty( $_REQUEST['ct_builder'] ) || ( ! empty( $_REQUEST['action'] ) && ( substr( $_REQUEST['action'], 0, 11 ) === "oxy_render_" || substr( $_REQUEST['action'], 0, 10 ) === "ct_render_" ) ); |
|
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | /** |
@@ -124,7 +124,7 @@ discard block |
||
| 124 | 124 | * @param string $template_path The template path relative to the theme's root dir. Defaults to 'invoicing'. |
| 125 | 125 | * @param string $default_path The root path to the default template. Defaults to invoicing/templates |
| 126 | 126 | */ |
| 127 | - public function locate_template( $template_name, $template_path = '', $default_path = '' ) { |
|
| 127 | + public function locate_template( $template_name, $template_path = '', $default_path = '' ) { |
|
| 128 | 128 | |
| 129 | 129 | // Load the defaults for the template path and default path. |
| 130 | 130 | $template_path = empty( $template_path ) ? 'invoicing' : $template_path; |
@@ -146,22 +146,22 @@ discard block |
||
| 146 | 146 | } |
| 147 | 147 | |
| 148 | 148 | /** |
| 149 | - * Loads a template |
|
| 150 | - * |
|
| 151 | - * @since 1.0.19 |
|
| 152 | - * @return bool |
|
| 153 | - */ |
|
| 154 | - protected function load_template( $template_name, $template_path, $args ) { |
|
| 149 | + * Loads a template |
|
| 150 | + * |
|
| 151 | + * @since 1.0.19 |
|
| 152 | + * @return bool |
|
| 153 | + */ |
|
| 154 | + protected function load_template( $template_name, $template_path, $args ) { |
|
| 155 | 155 | |
| 156 | 156 | if ( is_array( $args ) ){ |
| 157 | 157 | extract( $args ); |
| 158 | 158 | } |
| 159 | 159 | |
| 160 | 160 | // Fires before loading a template. |
| 161 | - do_action( 'wpinv_before_template_part', $template_name, $template_path, $args ); |
|
| 161 | + do_action( 'wpinv_before_template_part', $template_name, $template_path, $args ); |
|
| 162 | 162 | |
| 163 | 163 | // Load the template. |
| 164 | - include( $template_path ); |
|
| 164 | + include( $template_path ); |
|
| 165 | 165 | |
| 166 | 166 | // Fires after loading a template. |
| 167 | 167 | do_action( 'wpinv_after_template_part', $template_name, $template_path, $args ); |
@@ -178,7 +178,7 @@ discard block |
||
| 178 | 178 | * @param string $template_path The templates directory relative to the theme's root dir. Defaults to 'invoicing'. |
| 179 | 179 | * @param string $default_path The root path to the default template. Defaults to invoicing/templates |
| 180 | 180 | */ |
| 181 | - public function display_template( $template_name, $args = array(), $template_path = '', $default_path = '' ) { |
|
| 181 | + public function display_template( $template_name, $args = array(), $template_path = '', $default_path = '' ) { |
|
| 182 | 182 | |
| 183 | 183 | // Locate the template. |
| 184 | 184 | $located = wpinv_locate_template( $template_name, $template_path, $default_path ); |
@@ -203,7 +203,7 @@ discard block |
||
| 203 | 203 | * @param string $template_path The templates directory relative to the theme's root dir. Defaults to 'invoicing'. |
| 204 | 204 | * @param string $default_path The root path to the default template. Defaults to invoicing/templates |
| 205 | 205 | */ |
| 206 | - public function get_template( $template_name, $args = array(), $template_path = '', $default_path = '' ) { |
|
| 206 | + public function get_template( $template_name, $args = array(), $template_path = '', $default_path = '' ) { |
|
| 207 | 207 | ob_start(); |
| 208 | 208 | $this->display_template( $template_name, $args, $template_path, $default_path ); |
| 209 | 209 | return ob_get_clean(); |
@@ -31,16 +31,16 @@ discard block |
||
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | function wpinv_can_checkout() { |
| 34 | - $can_checkout = true; // Always true for now |
|
| 34 | + $can_checkout = true; // Always true for now |
|
| 35 | 35 | |
| 36 | - return (bool) apply_filters( 'wpinv_can_checkout', $can_checkout ); |
|
| 36 | + return (bool) apply_filters( 'wpinv_can_checkout', $can_checkout ); |
|
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | function wpinv_get_success_page_uri() { |
| 40 | - $page_id = wpinv_get_option( 'success_page', 0 ); |
|
| 41 | - $page_id = absint( $page_id ); |
|
| 40 | + $page_id = wpinv_get_option( 'success_page', 0 ); |
|
| 41 | + $page_id = absint( $page_id ); |
|
| 42 | 42 | |
| 43 | - return apply_filters( 'wpinv_get_success_page_uri', get_permalink( $page_id ) ); |
|
| 43 | + return apply_filters( 'wpinv_get_success_page_uri', get_permalink( $page_id ) ); |
|
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | /** |
@@ -51,22 +51,22 @@ discard block |
||
| 51 | 51 | */ |
| 52 | 52 | function wpinv_get_history_page_uri( $post_type = 'wpi_invoice' ) { |
| 53 | 53 | $post_type = sanitize_key( str_replace( 'wpi_', '', $post_type ) ); |
| 54 | - $page_id = wpinv_get_option( "{$post_type}_history_page", 0 ); |
|
| 55 | - $page_id = absint( $page_id ); |
|
| 56 | - return apply_filters( 'wpinv_get_history_page_uri', get_permalink( $page_id ), $post_type ); |
|
| 54 | + $page_id = wpinv_get_option( "{$post_type}_history_page", 0 ); |
|
| 55 | + $page_id = absint( $page_id ); |
|
| 56 | + return apply_filters( 'wpinv_get_history_page_uri', get_permalink( $page_id ), $post_type ); |
|
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | function wpinv_is_success_page() { |
| 60 | - $is_success_page = wpinv_get_option( 'success_page', false ); |
|
| 61 | - $is_success_page = ! empty( $is_success_page ) ? is_page( $is_success_page ) : false; |
|
| 60 | + $is_success_page = wpinv_get_option( 'success_page', false ); |
|
| 61 | + $is_success_page = ! empty( $is_success_page ) ? is_page( $is_success_page ) : false; |
|
| 62 | 62 | |
| 63 | - return apply_filters( 'wpinv_is_success_page', $is_success_page ); |
|
| 63 | + return apply_filters( 'wpinv_is_success_page', $is_success_page ); |
|
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | function wpinv_is_invoice_history_page() { |
| 67 | - $ret = wpinv_get_option( 'invoice_history_page', false ); |
|
| 68 | - $ret = $ret ? is_page( $ret ) : false; |
|
| 69 | - return apply_filters( 'wpinv_is_invoice_history_page', $ret ); |
|
| 67 | + $ret = wpinv_get_option( 'invoice_history_page', false ); |
|
| 68 | + $ret = $ret ? is_page( $ret ) : false; |
|
| 69 | + return apply_filters( 'wpinv_is_invoice_history_page', $ret ); |
|
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | function wpinv_is_subscriptions_history_page() { |
@@ -89,7 +89,7 @@ discard block |
||
| 89 | 89 | } |
| 90 | 90 | |
| 91 | 91 | function wpinv_send_to_failed_page( $args = null ) { |
| 92 | - $redirect = wpinv_get_failed_transaction_uri(); |
|
| 92 | + $redirect = wpinv_get_failed_transaction_uri(); |
|
| 93 | 93 | |
| 94 | 94 | if ( !empty( $args ) ) { |
| 95 | 95 | // Check for backward compatibility |
@@ -109,55 +109,55 @@ discard block |
||
| 109 | 109 | } |
| 110 | 110 | |
| 111 | 111 | function wpinv_get_checkout_uri( $args = array() ) { |
| 112 | - $uri = wpinv_get_option( 'checkout_page', false ); |
|
| 113 | - $uri = isset( $uri ) ? get_permalink( $uri ) : NULL; |
|
| 112 | + $uri = wpinv_get_option( 'checkout_page', false ); |
|
| 113 | + $uri = isset( $uri ) ? get_permalink( $uri ) : NULL; |
|
| 114 | 114 | |
| 115 | - if ( !empty( $args ) ) { |
|
| 116 | - // Check for backward compatibility |
|
| 117 | - if ( is_string( $args ) ) |
|
| 118 | - $args = str_replace( '?', '', $args ); |
|
| 115 | + if ( !empty( $args ) ) { |
|
| 116 | + // Check for backward compatibility |
|
| 117 | + if ( is_string( $args ) ) |
|
| 118 | + $args = str_replace( '?', '', $args ); |
|
| 119 | 119 | |
| 120 | - $args = wp_parse_args( $args ); |
|
| 120 | + $args = wp_parse_args( $args ); |
|
| 121 | 121 | |
| 122 | - $uri = add_query_arg( $args, $uri ); |
|
| 123 | - } |
|
| 122 | + $uri = add_query_arg( $args, $uri ); |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | - $scheme = defined( 'FORCE_SSL_ADMIN' ) && FORCE_SSL_ADMIN ? 'https' : 'admin'; |
|
| 125 | + $scheme = defined( 'FORCE_SSL_ADMIN' ) && FORCE_SSL_ADMIN ? 'https' : 'admin'; |
|
| 126 | 126 | |
| 127 | - $ajax_url = admin_url( 'admin-ajax.php', $scheme ); |
|
| 127 | + $ajax_url = admin_url( 'admin-ajax.php', $scheme ); |
|
| 128 | 128 | |
| 129 | - if ( ( ! preg_match( '/^https/', $uri ) && preg_match( '/^https/', $ajax_url ) ) || wpinv_is_ssl_enforced() ) { |
|
| 130 | - $uri = preg_replace( '/^http:/', 'https:', $uri ); |
|
| 131 | - } |
|
| 129 | + if ( ( ! preg_match( '/^https/', $uri ) && preg_match( '/^https/', $ajax_url ) ) || wpinv_is_ssl_enforced() ) { |
|
| 130 | + $uri = preg_replace( '/^http:/', 'https:', $uri ); |
|
| 131 | + } |
|
| 132 | 132 | |
| 133 | - return apply_filters( 'wpinv_get_checkout_uri', $uri ); |
|
| 133 | + return apply_filters( 'wpinv_get_checkout_uri', $uri ); |
|
| 134 | 134 | } |
| 135 | 135 | |
| 136 | 136 | function wpinv_get_success_page_url( $query_string = null ) { |
| 137 | - $success_page = wpinv_get_option( 'success_page', 0 ); |
|
| 138 | - $success_page = get_permalink( $success_page ); |
|
| 137 | + $success_page = wpinv_get_option( 'success_page', 0 ); |
|
| 138 | + $success_page = get_permalink( $success_page ); |
|
| 139 | 139 | |
| 140 | - if ( $query_string ) |
|
| 141 | - $success_page .= $query_string; |
|
| 140 | + if ( $query_string ) |
|
| 141 | + $success_page .= $query_string; |
|
| 142 | 142 | |
| 143 | - return apply_filters( 'wpinv_success_page_url', $success_page ); |
|
| 143 | + return apply_filters( 'wpinv_success_page_url', $success_page ); |
|
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | function wpinv_get_failed_transaction_uri( $extras = false ) { |
| 147 | - $uri = wpinv_get_option( 'failure_page', '' ); |
|
| 148 | - $uri = ! empty( $uri ) ? trailingslashit( get_permalink( $uri ) ) : home_url(); |
|
| 147 | + $uri = wpinv_get_option( 'failure_page', '' ); |
|
| 148 | + $uri = ! empty( $uri ) ? trailingslashit( get_permalink( $uri ) ) : home_url(); |
|
| 149 | 149 | |
| 150 | - if ( $extras ) |
|
| 151 | - $uri .= $extras; |
|
| 150 | + if ( $extras ) |
|
| 151 | + $uri .= $extras; |
|
| 152 | 152 | |
| 153 | - return apply_filters( 'wpinv_get_failed_transaction_uri', $uri ); |
|
| 153 | + return apply_filters( 'wpinv_get_failed_transaction_uri', $uri ); |
|
| 154 | 154 | } |
| 155 | 155 | |
| 156 | 156 | function wpinv_is_failed_transaction_page() { |
| 157 | - $ret = wpinv_get_option( 'failure_page', false ); |
|
| 158 | - $ret = isset( $ret ) ? is_page( $ret ) : false; |
|
| 157 | + $ret = wpinv_get_option( 'failure_page', false ); |
|
| 158 | + $ret = isset( $ret ) ? is_page( $ret ) : false; |
|
| 159 | 159 | |
| 160 | - return apply_filters( 'wpinv_is_failure_page', $ret ); |
|
| 160 | + return apply_filters( 'wpinv_is_failure_page', $ret ); |
|
| 161 | 161 | } |
| 162 | 162 | |
| 163 | 163 | function wpinv_transaction_query( $type = 'start' ) { |
@@ -232,36 +232,36 @@ discard block |
||
| 232 | 232 | $require_billing_details = apply_filters( 'wpinv_checkout_required_billing_details', wpinv_use_taxes() ); |
| 233 | 233 | |
| 234 | 234 | if ( $require_billing_details ) { |
| 235 | - if ( (bool)wpinv_get_option( 'fname_mandatory' ) ) { |
|
| 236 | - $required_fields['first_name'] = array( |
|
| 237 | - 'error_id' => 'invalid_first_name', |
|
| 238 | - 'error_message' => __( 'Please enter your first name', 'invoicing' ) |
|
| 239 | - ); |
|
| 240 | - } |
|
| 241 | - if ( (bool)wpinv_get_option( 'address_mandatory' ) ) { |
|
| 242 | - $required_fields['address'] = array( |
|
| 243 | - 'error_id' => 'invalid_address', |
|
| 244 | - 'error_message' => __( 'Please enter your address', 'invoicing' ) |
|
| 245 | - ); |
|
| 246 | - } |
|
| 247 | - if ( (bool)wpinv_get_option( 'city_mandatory' ) ) { |
|
| 248 | - $required_fields['city'] = array( |
|
| 249 | - 'error_id' => 'invalid_city', |
|
| 250 | - 'error_message' => __( 'Please enter your billing city', 'invoicing' ) |
|
| 251 | - ); |
|
| 252 | - } |
|
| 253 | - if ( (bool)wpinv_get_option( 'state_mandatory' ) ) { |
|
| 254 | - $required_fields['state'] = array( |
|
| 255 | - 'error_id' => 'invalid_state', |
|
| 256 | - 'error_message' => __( 'Please enter billing state / province', 'invoicing' ) |
|
| 257 | - ); |
|
| 258 | - } |
|
| 259 | - if ( (bool)wpinv_get_option( 'country_mandatory' ) ) { |
|
| 260 | - $required_fields['country'] = array( |
|
| 261 | - 'error_id' => 'invalid_country', |
|
| 262 | - 'error_message' => __( 'Please select your billing country', 'invoicing' ) |
|
| 263 | - ); |
|
| 264 | - } |
|
| 235 | + if ( (bool)wpinv_get_option( 'fname_mandatory' ) ) { |
|
| 236 | + $required_fields['first_name'] = array( |
|
| 237 | + 'error_id' => 'invalid_first_name', |
|
| 238 | + 'error_message' => __( 'Please enter your first name', 'invoicing' ) |
|
| 239 | + ); |
|
| 240 | + } |
|
| 241 | + if ( (bool)wpinv_get_option( 'address_mandatory' ) ) { |
|
| 242 | + $required_fields['address'] = array( |
|
| 243 | + 'error_id' => 'invalid_address', |
|
| 244 | + 'error_message' => __( 'Please enter your address', 'invoicing' ) |
|
| 245 | + ); |
|
| 246 | + } |
|
| 247 | + if ( (bool)wpinv_get_option( 'city_mandatory' ) ) { |
|
| 248 | + $required_fields['city'] = array( |
|
| 249 | + 'error_id' => 'invalid_city', |
|
| 250 | + 'error_message' => __( 'Please enter your billing city', 'invoicing' ) |
|
| 251 | + ); |
|
| 252 | + } |
|
| 253 | + if ( (bool)wpinv_get_option( 'state_mandatory' ) ) { |
|
| 254 | + $required_fields['state'] = array( |
|
| 255 | + 'error_id' => 'invalid_state', |
|
| 256 | + 'error_message' => __( 'Please enter billing state / province', 'invoicing' ) |
|
| 257 | + ); |
|
| 258 | + } |
|
| 259 | + if ( (bool)wpinv_get_option( 'country_mandatory' ) ) { |
|
| 260 | + $required_fields['country'] = array( |
|
| 261 | + 'error_id' => 'invalid_country', |
|
| 262 | + 'error_message' => __( 'Please select your billing country', 'invoicing' ) |
|
| 263 | + ); |
|
| 264 | + } |
|
| 265 | 265 | } |
| 266 | 266 | |
| 267 | 267 | return apply_filters( 'wpinv_checkout_required_fields', $required_fields ); |
@@ -15,136 +15,136 @@ |
||
| 15 | 15 | class WPInv_REST_Invoice_Controller extends GetPaid_REST_Posts_Controller { |
| 16 | 16 | |
| 17 | 17 | /** |
| 18 | - * Post type. |
|
| 19 | - * |
|
| 20 | - * @var string |
|
| 21 | - */ |
|
| 22 | - protected $post_type = 'wpi_invoice'; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * The base of this controller's route. |
|
| 26 | - * |
|
| 27 | - * @since 1.0.13 |
|
| 28 | - * @var string |
|
| 29 | - */ |
|
| 30 | - protected $rest_base = 'invoices'; |
|
| 31 | - |
|
| 32 | - /** Contains this controller's class name. |
|
| 33 | - * |
|
| 34 | - * @var string |
|
| 35 | - */ |
|
| 36 | - public $crud_class = 'WPInv_Invoice'; |
|
| 18 | + * Post type. |
|
| 19 | + * |
|
| 20 | + * @var string |
|
| 21 | + */ |
|
| 22 | + protected $post_type = 'wpi_invoice'; |
|
| 37 | 23 | |
| 38 | 24 | /** |
| 39 | - * Retrieves the query params for the invoices collection. |
|
| 40 | - * |
|
| 41 | - * @since 1.0.13 |
|
| 42 | - * |
|
| 43 | - * @return array Collection parameters. |
|
| 44 | - */ |
|
| 45 | - public function get_collection_params() { |
|
| 46 | - |
|
| 47 | - $params = array_merge( |
|
| 48 | - |
|
| 49 | - parent::get_collection_params(), |
|
| 50 | - |
|
| 51 | - array( |
|
| 52 | - |
|
| 53 | - |
|
| 54 | - 'customers' => array( |
|
| 55 | - 'description' => __( 'Limit result set to invoices for specific user ids.', 'invoicing' ), |
|
| 56 | - 'type' => 'array', |
|
| 57 | - 'items' => array( |
|
| 58 | - 'type' => 'integer', |
|
| 59 | - ), |
|
| 60 | - 'default' => array(), |
|
| 61 | - 'sanitize_callback' => 'wp_parse_id_list', |
|
| 62 | - ), |
|
| 63 | - |
|
| 64 | - 'exclude_customers' => array( |
|
| 65 | - 'description' => __( 'Exclude invoices to specific users.', 'invoicing' ), |
|
| 66 | - 'type' => 'array', |
|
| 67 | - 'items' => array( |
|
| 68 | - 'type' => 'integer', |
|
| 69 | - ), |
|
| 70 | - 'default' => array(), |
|
| 71 | - 'sanitize_callback' => 'wp_parse_id_list', |
|
| 72 | - ), |
|
| 73 | - |
|
| 74 | - 'parent' => array( |
|
| 75 | - 'description' => __( 'Limit result set to those of particular parent IDs.', 'invoicing' ), |
|
| 76 | - 'type' => 'array', |
|
| 77 | - 'items' => array( |
|
| 78 | - 'type' => 'integer', |
|
| 79 | - ), |
|
| 80 | - 'sanitize_callback' => 'wp_parse_id_list', |
|
| 81 | - 'default' => array(), |
|
| 82 | - ), |
|
| 83 | - |
|
| 84 | - 'parent_exclude' => array( |
|
| 85 | - 'description' => __( 'Limit result set to all items except those of a particular parent ID.', 'invoicing' ), |
|
| 86 | - 'type' => 'array', |
|
| 87 | - 'items' => array( |
|
| 88 | - 'type' => 'integer', |
|
| 89 | - ), |
|
| 90 | - 'sanitize_callback' => 'wp_parse_id_list', |
|
| 91 | - 'default' => array(), |
|
| 92 | - ), |
|
| 93 | - |
|
| 94 | - ) |
|
| 95 | - |
|
| 96 | - ); |
|
| 97 | - |
|
| 98 | - // Filter collection parameters for the invoices controller. |
|
| 99 | - return apply_filters( 'getpaid_rest_invoices_collection_params', $params, $this ); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Determine the allowed query_vars for a get_items() response and |
|
| 104 | - * prepare for WP_Query. |
|
| 105 | - * |
|
| 106 | - * @param array $prepared_args Prepared arguments. |
|
| 107 | - * @param WP_REST_Request $request Request object. |
|
| 108 | - * @return array $query_args |
|
| 109 | - */ |
|
| 110 | - protected function prepare_items_query( $prepared_args = array(), $request = null ) { |
|
| 111 | - |
|
| 112 | - $query_args = parent::prepare_items_query( $prepared_args ); |
|
| 113 | - |
|
| 114 | - // Retrieve invoices for specific customers. |
|
| 115 | - if ( ! empty( $request['customers'] ) ) { |
|
| 116 | - $query_args['author__in'] = $request['customers']; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - // Skip invoices for specific customers. |
|
| 120 | - if ( ! empty( $request['exclude_customers'] ) ) { |
|
| 121 | - $query_args['author__not_in'] = $request['exclude_customers']; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - return apply_filters( 'getpaid_rest_invoices_prepare_items_query', $query_args, $request, $this ); |
|
| 125 | - |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Retrieves a valid list of post statuses. |
|
| 130 | - * |
|
| 131 | - * @since 1.0.15 |
|
| 132 | - * |
|
| 133 | - * @return array A list of registered item statuses. |
|
| 134 | - */ |
|
| 135 | - public function get_post_statuses() { |
|
| 136 | - return array_keys( wpinv_get_invoice_statuses( true, false, $this->post_type ) ); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - /** |
|
| 140 | - * Saves a single invoice. |
|
| 141 | - * |
|
| 142 | - * @param WPInv_Invoice $invoice Invoice to save. |
|
| 143 | - * @return WP_Error|WPInv_Invoice |
|
| 144 | - */ |
|
| 145 | - protected function save_object( $invoice ) { |
|
| 146 | - $invoice->recalculate_total(); |
|
| 147 | - return parent::save_object( $invoice ); |
|
| 148 | - } |
|
| 25 | + * The base of this controller's route. |
|
| 26 | + * |
|
| 27 | + * @since 1.0.13 |
|
| 28 | + * @var string |
|
| 29 | + */ |
|
| 30 | + protected $rest_base = 'invoices'; |
|
| 31 | + |
|
| 32 | + /** Contains this controller's class name. |
|
| 33 | + * |
|
| 34 | + * @var string |
|
| 35 | + */ |
|
| 36 | + public $crud_class = 'WPInv_Invoice'; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Retrieves the query params for the invoices collection. |
|
| 40 | + * |
|
| 41 | + * @since 1.0.13 |
|
| 42 | + * |
|
| 43 | + * @return array Collection parameters. |
|
| 44 | + */ |
|
| 45 | + public function get_collection_params() { |
|
| 46 | + |
|
| 47 | + $params = array_merge( |
|
| 48 | + |
|
| 49 | + parent::get_collection_params(), |
|
| 50 | + |
|
| 51 | + array( |
|
| 52 | + |
|
| 53 | + |
|
| 54 | + 'customers' => array( |
|
| 55 | + 'description' => __( 'Limit result set to invoices for specific user ids.', 'invoicing' ), |
|
| 56 | + 'type' => 'array', |
|
| 57 | + 'items' => array( |
|
| 58 | + 'type' => 'integer', |
|
| 59 | + ), |
|
| 60 | + 'default' => array(), |
|
| 61 | + 'sanitize_callback' => 'wp_parse_id_list', |
|
| 62 | + ), |
|
| 63 | + |
|
| 64 | + 'exclude_customers' => array( |
|
| 65 | + 'description' => __( 'Exclude invoices to specific users.', 'invoicing' ), |
|
| 66 | + 'type' => 'array', |
|
| 67 | + 'items' => array( |
|
| 68 | + 'type' => 'integer', |
|
| 69 | + ), |
|
| 70 | + 'default' => array(), |
|
| 71 | + 'sanitize_callback' => 'wp_parse_id_list', |
|
| 72 | + ), |
|
| 73 | + |
|
| 74 | + 'parent' => array( |
|
| 75 | + 'description' => __( 'Limit result set to those of particular parent IDs.', 'invoicing' ), |
|
| 76 | + 'type' => 'array', |
|
| 77 | + 'items' => array( |
|
| 78 | + 'type' => 'integer', |
|
| 79 | + ), |
|
| 80 | + 'sanitize_callback' => 'wp_parse_id_list', |
|
| 81 | + 'default' => array(), |
|
| 82 | + ), |
|
| 83 | + |
|
| 84 | + 'parent_exclude' => array( |
|
| 85 | + 'description' => __( 'Limit result set to all items except those of a particular parent ID.', 'invoicing' ), |
|
| 86 | + 'type' => 'array', |
|
| 87 | + 'items' => array( |
|
| 88 | + 'type' => 'integer', |
|
| 89 | + ), |
|
| 90 | + 'sanitize_callback' => 'wp_parse_id_list', |
|
| 91 | + 'default' => array(), |
|
| 92 | + ), |
|
| 93 | + |
|
| 94 | + ) |
|
| 95 | + |
|
| 96 | + ); |
|
| 97 | + |
|
| 98 | + // Filter collection parameters for the invoices controller. |
|
| 99 | + return apply_filters( 'getpaid_rest_invoices_collection_params', $params, $this ); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Determine the allowed query_vars for a get_items() response and |
|
| 104 | + * prepare for WP_Query. |
|
| 105 | + * |
|
| 106 | + * @param array $prepared_args Prepared arguments. |
|
| 107 | + * @param WP_REST_Request $request Request object. |
|
| 108 | + * @return array $query_args |
|
| 109 | + */ |
|
| 110 | + protected function prepare_items_query( $prepared_args = array(), $request = null ) { |
|
| 111 | + |
|
| 112 | + $query_args = parent::prepare_items_query( $prepared_args ); |
|
| 113 | + |
|
| 114 | + // Retrieve invoices for specific customers. |
|
| 115 | + if ( ! empty( $request['customers'] ) ) { |
|
| 116 | + $query_args['author__in'] = $request['customers']; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + // Skip invoices for specific customers. |
|
| 120 | + if ( ! empty( $request['exclude_customers'] ) ) { |
|
| 121 | + $query_args['author__not_in'] = $request['exclude_customers']; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + return apply_filters( 'getpaid_rest_invoices_prepare_items_query', $query_args, $request, $this ); |
|
| 125 | + |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Retrieves a valid list of post statuses. |
|
| 130 | + * |
|
| 131 | + * @since 1.0.15 |
|
| 132 | + * |
|
| 133 | + * @return array A list of registered item statuses. |
|
| 134 | + */ |
|
| 135 | + public function get_post_statuses() { |
|
| 136 | + return array_keys( wpinv_get_invoice_statuses( true, false, $this->post_type ) ); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * Saves a single invoice. |
|
| 141 | + * |
|
| 142 | + * @param WPInv_Invoice $invoice Invoice to save. |
|
| 143 | + * @return WP_Error|WPInv_Invoice |
|
| 144 | + */ |
|
| 145 | + protected function save_object( $invoice ) { |
|
| 146 | + $invoice->recalculate_total(); |
|
| 147 | + return parent::save_object( $invoice ); |
|
| 148 | + } |
|
| 149 | 149 | |
| 150 | 150 | } |
@@ -5,7 +5,7 @@ discard block |
||
| 5 | 5 | * |
| 6 | 6 | */ |
| 7 | 7 | if ( ! defined( 'ABSPATH' ) ) { |
| 8 | - exit; |
|
| 8 | + exit; |
|
| 9 | 9 | } |
| 10 | 10 | |
| 11 | 11 | /** |
@@ -15,537 +15,537 @@ discard block |
||
| 15 | 15 | */ |
| 16 | 16 | class GetPaid_Invoice_Data_Store extends GetPaid_Data_Store_WP { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Data stored in meta keys, but not considered "meta" for a discount. |
|
| 20 | - * |
|
| 21 | - * @since 1.0.19 |
|
| 22 | - * @var array |
|
| 23 | - */ |
|
| 24 | - protected $internal_meta_keys = array( |
|
| 25 | - '_wpinv_subscr_profile_id', |
|
| 26 | - '_wpinv_subscription_id', |
|
| 27 | - '_wpinv_taxes', |
|
| 28 | - '_wpinv_fees', |
|
| 29 | - '_wpinv_discounts', |
|
| 30 | - '_wpinv_submission_id', |
|
| 31 | - '_wpinv_payment_form', |
|
| 32 | - '_wpinv_is_viewed', |
|
| 33 | - 'wpinv_email_cc', |
|
| 34 | - 'wpinv_template', |
|
| 35 | - 'wpinv_created_via' |
|
| 36 | - ); |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * A map of meta keys to data props. |
|
| 40 | - * |
|
| 41 | - * @since 1.0.19 |
|
| 42 | - * |
|
| 43 | - * @var array |
|
| 44 | - */ |
|
| 45 | - protected $meta_key_to_props = array( |
|
| 46 | - '_wpinv_subscr_profile_id' => 'remote_subscription_id', |
|
| 47 | - '_wpinv_subscription_id' => 'subscription_id', |
|
| 48 | - '_wpinv_taxes' => 'taxes', |
|
| 49 | - '_wpinv_fees' => 'fees', |
|
| 50 | - '_wpinv_discounts' => 'discounts', |
|
| 51 | - '_wpinv_submission_id' => 'submission_id', |
|
| 52 | - '_wpinv_payment_form' => 'payment_form', |
|
| 53 | - '_wpinv_is_viewed' => 'is_viewed', |
|
| 54 | - 'wpinv_email_cc' => 'email_cc', |
|
| 55 | - 'wpinv_template' => 'template', |
|
| 56 | - 'wpinv_created_via' => 'created_via', |
|
| 57 | - ); |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * A map of database fields to data props. |
|
| 61 | - * |
|
| 62 | - * @since 1.0.19 |
|
| 63 | - * |
|
| 64 | - * @var array |
|
| 65 | - */ |
|
| 66 | - protected $database_fields_to_props = array( |
|
| 67 | - 'post_id' => 'id', |
|
| 68 | - 'number' => 'number', |
|
| 69 | - 'currency' => 'currency', |
|
| 70 | - 'key' => 'key', |
|
| 71 | - 'type' => 'type', |
|
| 72 | - 'mode' => 'mode', |
|
| 73 | - 'user_ip' => 'user_ip', |
|
| 74 | - 'first_name' => 'first_name', |
|
| 75 | - 'last_name' => 'last_name', |
|
| 76 | - 'address' => 'address', |
|
| 77 | - 'city' => 'city', |
|
| 78 | - 'state' => 'state', |
|
| 79 | - 'country' => 'country', |
|
| 80 | - 'zip' => 'zip', |
|
| 81 | - 'zip' => 'zip', |
|
| 82 | - 'adddress_confirmed' => 'address_confirmed', |
|
| 83 | - 'gateway' => 'gateway', |
|
| 84 | - 'transaction_id' => 'transaction_id', |
|
| 85 | - 'currency' => 'currency', |
|
| 86 | - 'subtotal' => 'subtotal', |
|
| 87 | - 'tax' => 'total_tax', |
|
| 88 | - 'fees_total' => 'total_fees', |
|
| 89 | - 'discount' => 'total_discount', |
|
| 90 | - 'total' => 'total', |
|
| 91 | - 'discount_code' => 'discount_code', |
|
| 92 | - 'disable_taxes' => 'disable_taxes', |
|
| 93 | - 'due_date' => 'due_date', |
|
| 94 | - 'completed_date' => 'completed_date', |
|
| 95 | - 'company' => 'company', |
|
| 96 | - 'vat_number' => 'vat_number', |
|
| 97 | - 'vat_rate' => 'vat_rate', |
|
| 98 | - ); |
|
| 99 | - |
|
| 100 | - /* |
|
| 18 | + /** |
|
| 19 | + * Data stored in meta keys, but not considered "meta" for a discount. |
|
| 20 | + * |
|
| 21 | + * @since 1.0.19 |
|
| 22 | + * @var array |
|
| 23 | + */ |
|
| 24 | + protected $internal_meta_keys = array( |
|
| 25 | + '_wpinv_subscr_profile_id', |
|
| 26 | + '_wpinv_subscription_id', |
|
| 27 | + '_wpinv_taxes', |
|
| 28 | + '_wpinv_fees', |
|
| 29 | + '_wpinv_discounts', |
|
| 30 | + '_wpinv_submission_id', |
|
| 31 | + '_wpinv_payment_form', |
|
| 32 | + '_wpinv_is_viewed', |
|
| 33 | + 'wpinv_email_cc', |
|
| 34 | + 'wpinv_template', |
|
| 35 | + 'wpinv_created_via' |
|
| 36 | + ); |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * A map of meta keys to data props. |
|
| 40 | + * |
|
| 41 | + * @since 1.0.19 |
|
| 42 | + * |
|
| 43 | + * @var array |
|
| 44 | + */ |
|
| 45 | + protected $meta_key_to_props = array( |
|
| 46 | + '_wpinv_subscr_profile_id' => 'remote_subscription_id', |
|
| 47 | + '_wpinv_subscription_id' => 'subscription_id', |
|
| 48 | + '_wpinv_taxes' => 'taxes', |
|
| 49 | + '_wpinv_fees' => 'fees', |
|
| 50 | + '_wpinv_discounts' => 'discounts', |
|
| 51 | + '_wpinv_submission_id' => 'submission_id', |
|
| 52 | + '_wpinv_payment_form' => 'payment_form', |
|
| 53 | + '_wpinv_is_viewed' => 'is_viewed', |
|
| 54 | + 'wpinv_email_cc' => 'email_cc', |
|
| 55 | + 'wpinv_template' => 'template', |
|
| 56 | + 'wpinv_created_via' => 'created_via', |
|
| 57 | + ); |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * A map of database fields to data props. |
|
| 61 | + * |
|
| 62 | + * @since 1.0.19 |
|
| 63 | + * |
|
| 64 | + * @var array |
|
| 65 | + */ |
|
| 66 | + protected $database_fields_to_props = array( |
|
| 67 | + 'post_id' => 'id', |
|
| 68 | + 'number' => 'number', |
|
| 69 | + 'currency' => 'currency', |
|
| 70 | + 'key' => 'key', |
|
| 71 | + 'type' => 'type', |
|
| 72 | + 'mode' => 'mode', |
|
| 73 | + 'user_ip' => 'user_ip', |
|
| 74 | + 'first_name' => 'first_name', |
|
| 75 | + 'last_name' => 'last_name', |
|
| 76 | + 'address' => 'address', |
|
| 77 | + 'city' => 'city', |
|
| 78 | + 'state' => 'state', |
|
| 79 | + 'country' => 'country', |
|
| 80 | + 'zip' => 'zip', |
|
| 81 | + 'zip' => 'zip', |
|
| 82 | + 'adddress_confirmed' => 'address_confirmed', |
|
| 83 | + 'gateway' => 'gateway', |
|
| 84 | + 'transaction_id' => 'transaction_id', |
|
| 85 | + 'currency' => 'currency', |
|
| 86 | + 'subtotal' => 'subtotal', |
|
| 87 | + 'tax' => 'total_tax', |
|
| 88 | + 'fees_total' => 'total_fees', |
|
| 89 | + 'discount' => 'total_discount', |
|
| 90 | + 'total' => 'total', |
|
| 91 | + 'discount_code' => 'discount_code', |
|
| 92 | + 'disable_taxes' => 'disable_taxes', |
|
| 93 | + 'due_date' => 'due_date', |
|
| 94 | + 'completed_date' => 'completed_date', |
|
| 95 | + 'company' => 'company', |
|
| 96 | + 'vat_number' => 'vat_number', |
|
| 97 | + 'vat_rate' => 'vat_rate', |
|
| 98 | + ); |
|
| 99 | + |
|
| 100 | + /* |
|
| 101 | 101 | |-------------------------------------------------------------------------- |
| 102 | 102 | | CRUD Methods |
| 103 | 103 | |-------------------------------------------------------------------------- |
| 104 | 104 | */ |
| 105 | 105 | |
| 106 | - /** |
|
| 107 | - * Method to create a new invoice in the database. |
|
| 108 | - * |
|
| 109 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 110 | - */ |
|
| 111 | - public function create( &$invoice ) { |
|
| 112 | - $invoice->set_version( WPINV_VERSION ); |
|
| 113 | - $invoice->set_date_created( current_time('mysql') ); |
|
| 114 | - |
|
| 115 | - // Create a new post. |
|
| 116 | - $id = wp_insert_post( |
|
| 117 | - apply_filters( |
|
| 118 | - 'getpaid_new_invoice_data', |
|
| 119 | - array( |
|
| 120 | - 'post_date' => $invoice->get_date_created( 'edit' ), |
|
| 121 | - 'post_type' => $invoice->get_post_type( 'edit' ), |
|
| 122 | - 'post_status' => $this->get_post_status( $invoice ), |
|
| 123 | - 'ping_status' => 'closed', |
|
| 124 | - 'post_author' => $invoice->get_user_id( 'edit' ), |
|
| 125 | - 'post_title' => $invoice->get_title( 'edit' ), |
|
| 126 | - 'post_excerpt' => $invoice->get_description( 'edit' ), |
|
| 127 | - 'post_parent' => $invoice->get_parent_id( 'edit' ), |
|
| 128 | - ) |
|
| 129 | - ), |
|
| 130 | - true |
|
| 131 | - ); |
|
| 132 | - |
|
| 133 | - if ( $id && ! is_wp_error( $id ) ) { |
|
| 134 | - |
|
| 135 | - // Update the new id and regenerate a title. |
|
| 136 | - $invoice->set_id( $id ); |
|
| 137 | - |
|
| 138 | - $invoice->maybe_set_number(); |
|
| 139 | - |
|
| 140 | - wp_update_post( |
|
| 141 | - array( |
|
| 142 | - 'ID' => $invoice->get_id(), |
|
| 143 | - 'post_title' => $invoice->get_number( 'edit' ), |
|
| 144 | - 'post_name' => $invoice->get_path( 'edit' ) |
|
| 145 | - ) |
|
| 146 | - ); |
|
| 147 | - |
|
| 148 | - // Save special fields and items. |
|
| 149 | - $this->save_special_fields( $invoice ); |
|
| 150 | - $this->save_items( $invoice ); |
|
| 151 | - |
|
| 152 | - // Update meta data. |
|
| 153 | - $this->update_post_meta( $invoice ); |
|
| 154 | - $invoice->save_meta_data(); |
|
| 155 | - |
|
| 156 | - // Apply changes. |
|
| 157 | - $invoice->apply_changes(); |
|
| 158 | - $this->clear_caches( $invoice ); |
|
| 159 | - |
|
| 160 | - // Fires after a new invoice is created. |
|
| 161 | - do_action( 'getpaid_new_invoice', $invoice ); |
|
| 162 | - return true; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - if ( is_wp_error( $id ) ) { |
|
| 166 | - $invoice->last_error = $id->get_error_message(); |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - return false; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * Method to read an invoice from the database. |
|
| 174 | - * |
|
| 175 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 176 | - * |
|
| 177 | - */ |
|
| 178 | - public function read( &$invoice ) { |
|
| 179 | - |
|
| 180 | - $invoice->set_defaults(); |
|
| 181 | - $invoice_object = get_post( $invoice->get_id() ); |
|
| 182 | - |
|
| 183 | - if ( ! $invoice->get_id() || ! $invoice_object || ! getpaid_is_invoice_post_type( $invoice_object->post_type ) ) { |
|
| 184 | - $invoice->last_error = __( 'Invalid invoice.', 'invoicing' ); |
|
| 185 | - $invoice->set_id( 0 ); |
|
| 186 | - return false; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - $invoice->set_props( |
|
| 190 | - array( |
|
| 191 | - 'date_created' => 0 < $invoice_object->post_date ? $invoice_object->post_date : null, |
|
| 192 | - 'date_modified' => 0 < $invoice_object->post_modified ? $invoice_object->post_modified : null, |
|
| 193 | - 'status' => $invoice_object->post_status, |
|
| 194 | - 'author' => $invoice_object->post_author, |
|
| 195 | - 'description' => $invoice_object->post_excerpt, |
|
| 196 | - 'parent_id' => $invoice_object->post_parent, |
|
| 197 | - 'name' => $invoice_object->post_title, |
|
| 198 | - 'path' => $invoice_object->post_name, |
|
| 199 | - 'post_type' => $invoice_object->post_type, |
|
| 200 | - ) |
|
| 201 | - ); |
|
| 202 | - |
|
| 203 | - $invoice->set_type( $invoice_object->post_type ); |
|
| 204 | - |
|
| 205 | - $this->read_object_data( $invoice, $invoice_object ); |
|
| 206 | - $this->add_special_fields( $invoice ); |
|
| 207 | - $this->add_items( $invoice ); |
|
| 208 | - $invoice->read_meta_data(); |
|
| 209 | - $invoice->set_object_read( true ); |
|
| 210 | - do_action( 'getpaid_read_invoice', $invoice ); |
|
| 211 | - |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * Method to update an invoice in the database. |
|
| 216 | - * |
|
| 217 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 218 | - */ |
|
| 219 | - public function update( &$invoice ) { |
|
| 220 | - $invoice->save_meta_data(); |
|
| 221 | - $invoice->set_version( WPINV_VERSION ); |
|
| 222 | - |
|
| 223 | - if ( null === $invoice->get_date_created( 'edit' ) ) { |
|
| 224 | - $invoice->set_date_created( current_time('mysql') ); |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - // Ensure both the key and number are set. |
|
| 228 | - $invoice->get_path(); |
|
| 229 | - |
|
| 230 | - // Grab the current status so we can compare. |
|
| 231 | - $previous_status = get_post_status( $invoice->get_id() ); |
|
| 232 | - |
|
| 233 | - $changes = $invoice->get_changes(); |
|
| 234 | - |
|
| 235 | - // Only update the post when the post data changes. |
|
| 236 | - if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'name', 'author', 'description', 'parent_id', 'post_excerpt', 'path' ), array_keys( $changes ) ) ) { |
|
| 237 | - $post_data = array( |
|
| 238 | - 'post_date' => $invoice->get_date_created( 'edit' ), |
|
| 239 | - 'post_date_gmt' => $invoice->get_date_created_gmt( 'edit' ), |
|
| 240 | - 'post_status' => $invoice->get_status( 'edit' ), |
|
| 241 | - 'post_title' => $invoice->get_name( 'edit' ), |
|
| 242 | - 'post_author' => $invoice->get_user_id( 'edit' ), |
|
| 243 | - 'post_modified' => $invoice->get_date_modified( 'edit' ), |
|
| 244 | - 'post_excerpt' => $invoice->get_description( 'edit' ), |
|
| 245 | - 'post_parent' => $invoice->get_parent_id( 'edit' ), |
|
| 246 | - 'post_name' => $invoice->get_path( 'edit' ), |
|
| 247 | - 'post_type' => $invoice->get_post_type( 'edit' ), |
|
| 248 | - ); |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * When updating this object, to prevent infinite loops, use $wpdb |
|
| 252 | - * to update data, since wp_update_post spawns more calls to the |
|
| 253 | - * save_post action. |
|
| 254 | - * |
|
| 255 | - * This ensures hooks are fired by either WP itself (admin screen save), |
|
| 256 | - * or an update purely from CRUD. |
|
| 257 | - */ |
|
| 258 | - if ( doing_action( 'save_post' ) ) { |
|
| 259 | - $GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $invoice->get_id() ) ); |
|
| 260 | - clean_post_cache( $invoice->get_id() ); |
|
| 261 | - } else { |
|
| 262 | - wp_update_post( array_merge( array( 'ID' => $invoice->get_id() ), $post_data ) ); |
|
| 263 | - } |
|
| 264 | - $invoice->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook. |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - // Update meta data. |
|
| 268 | - $this->update_post_meta( $invoice ); |
|
| 269 | - |
|
| 270 | - // Save special fields and items. |
|
| 271 | - $this->save_special_fields( $invoice ); |
|
| 272 | - $this->save_items( $invoice ); |
|
| 273 | - |
|
| 274 | - // Apply the changes. |
|
| 275 | - $invoice->apply_changes(); |
|
| 276 | - |
|
| 277 | - // Clear caches. |
|
| 278 | - $this->clear_caches( $invoice ); |
|
| 279 | - |
|
| 280 | - // Fire a hook depending on the status - this should be considered a creation if it was previously draft status. |
|
| 281 | - $new_status = $invoice->get_status( 'edit' ); |
|
| 282 | - |
|
| 283 | - if ( $new_status !== $previous_status && in_array( $previous_status, array( 'new', 'auto-draft', 'draft' ), true ) ) { |
|
| 284 | - do_action( 'getpaid_new_invoice', $invoice ); |
|
| 285 | - } else { |
|
| 286 | - do_action( 'getpaid_update_invoice', $invoice ); |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - /* |
|
| 106 | + /** |
|
| 107 | + * Method to create a new invoice in the database. |
|
| 108 | + * |
|
| 109 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 110 | + */ |
|
| 111 | + public function create( &$invoice ) { |
|
| 112 | + $invoice->set_version( WPINV_VERSION ); |
|
| 113 | + $invoice->set_date_created( current_time('mysql') ); |
|
| 114 | + |
|
| 115 | + // Create a new post. |
|
| 116 | + $id = wp_insert_post( |
|
| 117 | + apply_filters( |
|
| 118 | + 'getpaid_new_invoice_data', |
|
| 119 | + array( |
|
| 120 | + 'post_date' => $invoice->get_date_created( 'edit' ), |
|
| 121 | + 'post_type' => $invoice->get_post_type( 'edit' ), |
|
| 122 | + 'post_status' => $this->get_post_status( $invoice ), |
|
| 123 | + 'ping_status' => 'closed', |
|
| 124 | + 'post_author' => $invoice->get_user_id( 'edit' ), |
|
| 125 | + 'post_title' => $invoice->get_title( 'edit' ), |
|
| 126 | + 'post_excerpt' => $invoice->get_description( 'edit' ), |
|
| 127 | + 'post_parent' => $invoice->get_parent_id( 'edit' ), |
|
| 128 | + ) |
|
| 129 | + ), |
|
| 130 | + true |
|
| 131 | + ); |
|
| 132 | + |
|
| 133 | + if ( $id && ! is_wp_error( $id ) ) { |
|
| 134 | + |
|
| 135 | + // Update the new id and regenerate a title. |
|
| 136 | + $invoice->set_id( $id ); |
|
| 137 | + |
|
| 138 | + $invoice->maybe_set_number(); |
|
| 139 | + |
|
| 140 | + wp_update_post( |
|
| 141 | + array( |
|
| 142 | + 'ID' => $invoice->get_id(), |
|
| 143 | + 'post_title' => $invoice->get_number( 'edit' ), |
|
| 144 | + 'post_name' => $invoice->get_path( 'edit' ) |
|
| 145 | + ) |
|
| 146 | + ); |
|
| 147 | + |
|
| 148 | + // Save special fields and items. |
|
| 149 | + $this->save_special_fields( $invoice ); |
|
| 150 | + $this->save_items( $invoice ); |
|
| 151 | + |
|
| 152 | + // Update meta data. |
|
| 153 | + $this->update_post_meta( $invoice ); |
|
| 154 | + $invoice->save_meta_data(); |
|
| 155 | + |
|
| 156 | + // Apply changes. |
|
| 157 | + $invoice->apply_changes(); |
|
| 158 | + $this->clear_caches( $invoice ); |
|
| 159 | + |
|
| 160 | + // Fires after a new invoice is created. |
|
| 161 | + do_action( 'getpaid_new_invoice', $invoice ); |
|
| 162 | + return true; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + if ( is_wp_error( $id ) ) { |
|
| 166 | + $invoice->last_error = $id->get_error_message(); |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + return false; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * Method to read an invoice from the database. |
|
| 174 | + * |
|
| 175 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 176 | + * |
|
| 177 | + */ |
|
| 178 | + public function read( &$invoice ) { |
|
| 179 | + |
|
| 180 | + $invoice->set_defaults(); |
|
| 181 | + $invoice_object = get_post( $invoice->get_id() ); |
|
| 182 | + |
|
| 183 | + if ( ! $invoice->get_id() || ! $invoice_object || ! getpaid_is_invoice_post_type( $invoice_object->post_type ) ) { |
|
| 184 | + $invoice->last_error = __( 'Invalid invoice.', 'invoicing' ); |
|
| 185 | + $invoice->set_id( 0 ); |
|
| 186 | + return false; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + $invoice->set_props( |
|
| 190 | + array( |
|
| 191 | + 'date_created' => 0 < $invoice_object->post_date ? $invoice_object->post_date : null, |
|
| 192 | + 'date_modified' => 0 < $invoice_object->post_modified ? $invoice_object->post_modified : null, |
|
| 193 | + 'status' => $invoice_object->post_status, |
|
| 194 | + 'author' => $invoice_object->post_author, |
|
| 195 | + 'description' => $invoice_object->post_excerpt, |
|
| 196 | + 'parent_id' => $invoice_object->post_parent, |
|
| 197 | + 'name' => $invoice_object->post_title, |
|
| 198 | + 'path' => $invoice_object->post_name, |
|
| 199 | + 'post_type' => $invoice_object->post_type, |
|
| 200 | + ) |
|
| 201 | + ); |
|
| 202 | + |
|
| 203 | + $invoice->set_type( $invoice_object->post_type ); |
|
| 204 | + |
|
| 205 | + $this->read_object_data( $invoice, $invoice_object ); |
|
| 206 | + $this->add_special_fields( $invoice ); |
|
| 207 | + $this->add_items( $invoice ); |
|
| 208 | + $invoice->read_meta_data(); |
|
| 209 | + $invoice->set_object_read( true ); |
|
| 210 | + do_action( 'getpaid_read_invoice', $invoice ); |
|
| 211 | + |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * Method to update an invoice in the database. |
|
| 216 | + * |
|
| 217 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 218 | + */ |
|
| 219 | + public function update( &$invoice ) { |
|
| 220 | + $invoice->save_meta_data(); |
|
| 221 | + $invoice->set_version( WPINV_VERSION ); |
|
| 222 | + |
|
| 223 | + if ( null === $invoice->get_date_created( 'edit' ) ) { |
|
| 224 | + $invoice->set_date_created( current_time('mysql') ); |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + // Ensure both the key and number are set. |
|
| 228 | + $invoice->get_path(); |
|
| 229 | + |
|
| 230 | + // Grab the current status so we can compare. |
|
| 231 | + $previous_status = get_post_status( $invoice->get_id() ); |
|
| 232 | + |
|
| 233 | + $changes = $invoice->get_changes(); |
|
| 234 | + |
|
| 235 | + // Only update the post when the post data changes. |
|
| 236 | + if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'name', 'author', 'description', 'parent_id', 'post_excerpt', 'path' ), array_keys( $changes ) ) ) { |
|
| 237 | + $post_data = array( |
|
| 238 | + 'post_date' => $invoice->get_date_created( 'edit' ), |
|
| 239 | + 'post_date_gmt' => $invoice->get_date_created_gmt( 'edit' ), |
|
| 240 | + 'post_status' => $invoice->get_status( 'edit' ), |
|
| 241 | + 'post_title' => $invoice->get_name( 'edit' ), |
|
| 242 | + 'post_author' => $invoice->get_user_id( 'edit' ), |
|
| 243 | + 'post_modified' => $invoice->get_date_modified( 'edit' ), |
|
| 244 | + 'post_excerpt' => $invoice->get_description( 'edit' ), |
|
| 245 | + 'post_parent' => $invoice->get_parent_id( 'edit' ), |
|
| 246 | + 'post_name' => $invoice->get_path( 'edit' ), |
|
| 247 | + 'post_type' => $invoice->get_post_type( 'edit' ), |
|
| 248 | + ); |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * When updating this object, to prevent infinite loops, use $wpdb |
|
| 252 | + * to update data, since wp_update_post spawns more calls to the |
|
| 253 | + * save_post action. |
|
| 254 | + * |
|
| 255 | + * This ensures hooks are fired by either WP itself (admin screen save), |
|
| 256 | + * or an update purely from CRUD. |
|
| 257 | + */ |
|
| 258 | + if ( doing_action( 'save_post' ) ) { |
|
| 259 | + $GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $invoice->get_id() ) ); |
|
| 260 | + clean_post_cache( $invoice->get_id() ); |
|
| 261 | + } else { |
|
| 262 | + wp_update_post( array_merge( array( 'ID' => $invoice->get_id() ), $post_data ) ); |
|
| 263 | + } |
|
| 264 | + $invoice->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook. |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + // Update meta data. |
|
| 268 | + $this->update_post_meta( $invoice ); |
|
| 269 | + |
|
| 270 | + // Save special fields and items. |
|
| 271 | + $this->save_special_fields( $invoice ); |
|
| 272 | + $this->save_items( $invoice ); |
|
| 273 | + |
|
| 274 | + // Apply the changes. |
|
| 275 | + $invoice->apply_changes(); |
|
| 276 | + |
|
| 277 | + // Clear caches. |
|
| 278 | + $this->clear_caches( $invoice ); |
|
| 279 | + |
|
| 280 | + // Fire a hook depending on the status - this should be considered a creation if it was previously draft status. |
|
| 281 | + $new_status = $invoice->get_status( 'edit' ); |
|
| 282 | + |
|
| 283 | + if ( $new_status !== $previous_status && in_array( $previous_status, array( 'new', 'auto-draft', 'draft' ), true ) ) { |
|
| 284 | + do_action( 'getpaid_new_invoice', $invoice ); |
|
| 285 | + } else { |
|
| 286 | + do_action( 'getpaid_update_invoice', $invoice ); |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + /* |
|
| 292 | 292 | |-------------------------------------------------------------------------- |
| 293 | 293 | | Additional Methods |
| 294 | 294 | |-------------------------------------------------------------------------- |
| 295 | 295 | */ |
| 296 | 296 | |
| 297 | - /** |
|
| 297 | + /** |
|
| 298 | 298 | * Retrieves special fields and adds to the invoice. |
| 299 | - * |
|
| 300 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 299 | + * |
|
| 300 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 301 | 301 | */ |
| 302 | 302 | public function add_special_fields( &$invoice ) { |
| 303 | - global $wpdb; |
|
| 303 | + global $wpdb; |
|
| 304 | 304 | |
| 305 | - // Maybe retrieve from the cache. |
|
| 306 | - $data = wp_cache_get( $invoice->get_id(), 'getpaid_invoice_special_fields' ); |
|
| 305 | + // Maybe retrieve from the cache. |
|
| 306 | + $data = wp_cache_get( $invoice->get_id(), 'getpaid_invoice_special_fields' ); |
|
| 307 | 307 | |
| 308 | - // If not found, retrieve from the db. |
|
| 309 | - if ( false === $data ) { |
|
| 310 | - $table = $wpdb->prefix . 'getpaid_invoices'; |
|
| 308 | + // If not found, retrieve from the db. |
|
| 309 | + if ( false === $data ) { |
|
| 310 | + $table = $wpdb->prefix . 'getpaid_invoices'; |
|
| 311 | 311 | |
| 312 | - $data = $wpdb->get_row( |
|
| 313 | - $wpdb->prepare( "SELECT * FROM $table WHERE `post_id`=%d LIMIT 1", $invoice->get_id() ), |
|
| 314 | - ARRAY_A |
|
| 315 | - ); |
|
| 312 | + $data = $wpdb->get_row( |
|
| 313 | + $wpdb->prepare( "SELECT * FROM $table WHERE `post_id`=%d LIMIT 1", $invoice->get_id() ), |
|
| 314 | + ARRAY_A |
|
| 315 | + ); |
|
| 316 | 316 | |
| 317 | - // Update the cache with our data |
|
| 318 | - wp_cache_set( $invoice->get_id(), $data, 'getpaid_invoice_special_fields' ); |
|
| 317 | + // Update the cache with our data |
|
| 318 | + wp_cache_set( $invoice->get_id(), $data, 'getpaid_invoice_special_fields' ); |
|
| 319 | 319 | |
| 320 | - } |
|
| 320 | + } |
|
| 321 | 321 | |
| 322 | - // Abort if the data does not exist. |
|
| 323 | - if ( empty( $data ) ) { |
|
| 324 | - $invoice->set_object_read( true ); |
|
| 325 | - $invoice->set_props( wpinv_get_user_address( $invoice->get_user_id() ) ); |
|
| 326 | - return; |
|
| 327 | - } |
|
| 322 | + // Abort if the data does not exist. |
|
| 323 | + if ( empty( $data ) ) { |
|
| 324 | + $invoice->set_object_read( true ); |
|
| 325 | + $invoice->set_props( wpinv_get_user_address( $invoice->get_user_id() ) ); |
|
| 326 | + return; |
|
| 327 | + } |
|
| 328 | 328 | |
| 329 | - $props = array(); |
|
| 329 | + $props = array(); |
|
| 330 | 330 | |
| 331 | - foreach ( $this->database_fields_to_props as $db_field => $prop ) { |
|
| 331 | + foreach ( $this->database_fields_to_props as $db_field => $prop ) { |
|
| 332 | 332 | |
| 333 | - if ( $db_field == 'post_id' ) { |
|
| 334 | - continue; |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - $props[ $prop ] = $data[ $db_field ]; |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - $invoice->set_props( $props ); |
|
| 341 | - |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - /** |
|
| 345 | - * Gets a list of special fields that need updated based on change state |
|
| 346 | - * or if they are present in the database or not. |
|
| 347 | - * |
|
| 348 | - * @param WPInv_Invoice $invoice The Invoice object. |
|
| 349 | - * @return array A mapping of field keys => prop names, filtered by ones that should be updated. |
|
| 350 | - */ |
|
| 351 | - protected function get_special_fields_to_update( $invoice ) { |
|
| 352 | - $fields_to_update = array(); |
|
| 353 | - $changed_props = $invoice->get_changes(); |
|
| 354 | - |
|
| 355 | - // Props should be updated if they are a part of the $changed array or don't exist yet. |
|
| 356 | - foreach ( $this->database_fields_to_props as $database_field => $prop ) { |
|
| 357 | - if ( array_key_exists( $prop, $changed_props ) ) { |
|
| 358 | - $fields_to_update[ $database_field ] = $prop; |
|
| 359 | - } |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - return $fields_to_update; |
|
| 363 | - } |
|
| 364 | - |
|
| 365 | - /** |
|
| 366 | - * Helper method that updates all the database fields for an invoice based on it's settings in the WPInv_Invoice class. |
|
| 367 | - * |
|
| 368 | - * @param WPInv_Invoice $invoice WPInv_Invoice object. |
|
| 369 | - * @since 1.0.19 |
|
| 370 | - */ |
|
| 371 | - protected function update_special_fields( &$invoice ) { |
|
| 372 | - global $wpdb; |
|
| 373 | - |
|
| 374 | - $updated_props = array(); |
|
| 375 | - $fields_to_update = $this->get_special_fields_to_update( $invoice ); |
|
| 376 | - |
|
| 377 | - foreach ( $fields_to_update as $database_field => $prop ) { |
|
| 378 | - $value = $invoice->{"get_$prop"}( 'edit' ); |
|
| 379 | - $value = is_string( $value ) ? wp_slash( $value ) : $value; |
|
| 380 | - $value = is_bool( $value ) ? ( int ) $value : $value; |
|
| 381 | - $updated_props[ $database_field ] = maybe_serialize( $value ); |
|
| 382 | - } |
|
| 383 | - |
|
| 384 | - if ( ! empty( $updated_props ) ) { |
|
| 385 | - |
|
| 386 | - $table = $wpdb->prefix . 'getpaid_invoices'; |
|
| 387 | - $wpdb->update( $table, $updated_props, array( 'post_id' => $invoice->get_id() ) ); |
|
| 388 | - wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_special_fields' ); |
|
| 389 | - do_action( "getpaid_invoice_update_database_fields", $invoice, $updated_props ); |
|
| 390 | - |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - /** |
|
| 396 | - * Helper method that inserts special fields to the database. |
|
| 397 | - * |
|
| 398 | - * @param WPInv_Invoice $invoice WPInv_Invoice object. |
|
| 399 | - * @since 1.0.19 |
|
| 400 | - */ |
|
| 401 | - protected function insert_special_fields( &$invoice ) { |
|
| 402 | - global $wpdb; |
|
| 403 | - |
|
| 404 | - $updated_props = array(); |
|
| 405 | - |
|
| 406 | - foreach ( $this->database_fields_to_props as $database_field => $prop ) { |
|
| 407 | - $value = $invoice->{"get_$prop"}( 'edit' ); |
|
| 408 | - $value = is_string( $value ) ? wp_slash( $value ) : $value; |
|
| 409 | - $value = is_bool( $value ) ? ( int ) $value : $value; |
|
| 410 | - $updated_props[ $database_field ] = maybe_serialize( $value ); |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - $table = $wpdb->prefix . 'getpaid_invoices'; |
|
| 414 | - $wpdb->insert( $table, $updated_props ); |
|
| 415 | - wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_special_fields' ); |
|
| 416 | - do_action( "getpaid_invoice_insert_database_fields", $invoice, $updated_props ); |
|
| 417 | - |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - /** |
|
| 333 | + if ( $db_field == 'post_id' ) { |
|
| 334 | + continue; |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + $props[ $prop ] = $data[ $db_field ]; |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + $invoice->set_props( $props ); |
|
| 341 | + |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + /** |
|
| 345 | + * Gets a list of special fields that need updated based on change state |
|
| 346 | + * or if they are present in the database or not. |
|
| 347 | + * |
|
| 348 | + * @param WPInv_Invoice $invoice The Invoice object. |
|
| 349 | + * @return array A mapping of field keys => prop names, filtered by ones that should be updated. |
|
| 350 | + */ |
|
| 351 | + protected function get_special_fields_to_update( $invoice ) { |
|
| 352 | + $fields_to_update = array(); |
|
| 353 | + $changed_props = $invoice->get_changes(); |
|
| 354 | + |
|
| 355 | + // Props should be updated if they are a part of the $changed array or don't exist yet. |
|
| 356 | + foreach ( $this->database_fields_to_props as $database_field => $prop ) { |
|
| 357 | + if ( array_key_exists( $prop, $changed_props ) ) { |
|
| 358 | + $fields_to_update[ $database_field ] = $prop; |
|
| 359 | + } |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + return $fields_to_update; |
|
| 363 | + } |
|
| 364 | + |
|
| 365 | + /** |
|
| 366 | + * Helper method that updates all the database fields for an invoice based on it's settings in the WPInv_Invoice class. |
|
| 367 | + * |
|
| 368 | + * @param WPInv_Invoice $invoice WPInv_Invoice object. |
|
| 369 | + * @since 1.0.19 |
|
| 370 | + */ |
|
| 371 | + protected function update_special_fields( &$invoice ) { |
|
| 372 | + global $wpdb; |
|
| 373 | + |
|
| 374 | + $updated_props = array(); |
|
| 375 | + $fields_to_update = $this->get_special_fields_to_update( $invoice ); |
|
| 376 | + |
|
| 377 | + foreach ( $fields_to_update as $database_field => $prop ) { |
|
| 378 | + $value = $invoice->{"get_$prop"}( 'edit' ); |
|
| 379 | + $value = is_string( $value ) ? wp_slash( $value ) : $value; |
|
| 380 | + $value = is_bool( $value ) ? ( int ) $value : $value; |
|
| 381 | + $updated_props[ $database_field ] = maybe_serialize( $value ); |
|
| 382 | + } |
|
| 383 | + |
|
| 384 | + if ( ! empty( $updated_props ) ) { |
|
| 385 | + |
|
| 386 | + $table = $wpdb->prefix . 'getpaid_invoices'; |
|
| 387 | + $wpdb->update( $table, $updated_props, array( 'post_id' => $invoice->get_id() ) ); |
|
| 388 | + wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_special_fields' ); |
|
| 389 | + do_action( "getpaid_invoice_update_database_fields", $invoice, $updated_props ); |
|
| 390 | + |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + /** |
|
| 396 | + * Helper method that inserts special fields to the database. |
|
| 397 | + * |
|
| 398 | + * @param WPInv_Invoice $invoice WPInv_Invoice object. |
|
| 399 | + * @since 1.0.19 |
|
| 400 | + */ |
|
| 401 | + protected function insert_special_fields( &$invoice ) { |
|
| 402 | + global $wpdb; |
|
| 403 | + |
|
| 404 | + $updated_props = array(); |
|
| 405 | + |
|
| 406 | + foreach ( $this->database_fields_to_props as $database_field => $prop ) { |
|
| 407 | + $value = $invoice->{"get_$prop"}( 'edit' ); |
|
| 408 | + $value = is_string( $value ) ? wp_slash( $value ) : $value; |
|
| 409 | + $value = is_bool( $value ) ? ( int ) $value : $value; |
|
| 410 | + $updated_props[ $database_field ] = maybe_serialize( $value ); |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + $table = $wpdb->prefix . 'getpaid_invoices'; |
|
| 414 | + $wpdb->insert( $table, $updated_props ); |
|
| 415 | + wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_special_fields' ); |
|
| 416 | + do_action( "getpaid_invoice_insert_database_fields", $invoice, $updated_props ); |
|
| 417 | + |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + /** |
|
| 421 | 421 | * Saves all special fields. |
| 422 | - * |
|
| 423 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 422 | + * |
|
| 423 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 424 | 424 | */ |
| 425 | 425 | public function save_special_fields( & $invoice ) { |
| 426 | - global $wpdb; |
|
| 426 | + global $wpdb; |
|
| 427 | 427 | |
| 428 | - // The invoices table. |
|
| 429 | - $table = $wpdb->prefix . 'getpaid_invoices'; |
|
| 430 | - $id = (int) $invoice->get_id(); |
|
| 431 | - $invoice->maybe_set_key(); |
|
| 428 | + // The invoices table. |
|
| 429 | + $table = $wpdb->prefix . 'getpaid_invoices'; |
|
| 430 | + $id = (int) $invoice->get_id(); |
|
| 431 | + $invoice->maybe_set_key(); |
|
| 432 | 432 | |
| 433 | - if ( $wpdb->get_var( "SELECT `post_id` FROM $table WHERE `post_id`= $id" ) ) { |
|
| 433 | + if ( $wpdb->get_var( "SELECT `post_id` FROM $table WHERE `post_id`= $id" ) ) { |
|
| 434 | 434 | |
| 435 | - $this->update_special_fields( $invoice ); |
|
| 435 | + $this->update_special_fields( $invoice ); |
|
| 436 | 436 | |
| 437 | - } else { |
|
| 437 | + } else { |
|
| 438 | 438 | |
| 439 | - $this->insert_special_fields( $invoice ); |
|
| 439 | + $this->insert_special_fields( $invoice ); |
|
| 440 | 440 | |
| 441 | - } |
|
| 441 | + } |
|
| 442 | 442 | |
| 443 | - } |
|
| 443 | + } |
|
| 444 | 444 | |
| 445 | - /** |
|
| 445 | + /** |
|
| 446 | 446 | * Set's up cart details. |
| 447 | - * |
|
| 448 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 447 | + * |
|
| 448 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 449 | 449 | */ |
| 450 | 450 | public function add_items( &$invoice ) { |
| 451 | - global $wpdb; |
|
| 451 | + global $wpdb; |
|
| 452 | 452 | |
| 453 | - // Maybe retrieve from the cache. |
|
| 454 | - $items = wp_cache_get( $invoice->get_id(), 'getpaid_invoice_cart_details' ); |
|
| 453 | + // Maybe retrieve from the cache. |
|
| 454 | + $items = wp_cache_get( $invoice->get_id(), 'getpaid_invoice_cart_details' ); |
|
| 455 | 455 | |
| 456 | - // If not found, retrieve from the db. |
|
| 457 | - if ( false === $items ) { |
|
| 458 | - $table = $wpdb->prefix . 'getpaid_invoice_items'; |
|
| 456 | + // If not found, retrieve from the db. |
|
| 457 | + if ( false === $items ) { |
|
| 458 | + $table = $wpdb->prefix . 'getpaid_invoice_items'; |
|
| 459 | 459 | |
| 460 | - $items = $wpdb->get_results( |
|
| 461 | - $wpdb->prepare( "SELECT * FROM $table WHERE `post_id`=%d", $invoice->get_id() ) |
|
| 462 | - ); |
|
| 460 | + $items = $wpdb->get_results( |
|
| 461 | + $wpdb->prepare( "SELECT * FROM $table WHERE `post_id`=%d", $invoice->get_id() ) |
|
| 462 | + ); |
|
| 463 | 463 | |
| 464 | - // Update the cache with our data |
|
| 465 | - wp_cache_set( $invoice->get_id(), $items, 'getpaid_invoice_cart_details' ); |
|
| 464 | + // Update the cache with our data |
|
| 465 | + wp_cache_set( $invoice->get_id(), $items, 'getpaid_invoice_cart_details' ); |
|
| 466 | 466 | |
| 467 | - } |
|
| 467 | + } |
|
| 468 | 468 | |
| 469 | - // Abort if no items found. |
|
| 469 | + // Abort if no items found. |
|
| 470 | 470 | if ( empty( $items ) ) { |
| 471 | 471 | return; |
| 472 | - } |
|
| 472 | + } |
|
| 473 | 473 | |
| 474 | - foreach ( $items as $item_data ) { |
|
| 475 | - $item = new GetPaid_Form_Item( $item_data->item_id ); |
|
| 474 | + foreach ( $items as $item_data ) { |
|
| 475 | + $item = new GetPaid_Form_Item( $item_data->item_id ); |
|
| 476 | 476 | |
| 477 | - // Set item data. |
|
| 478 | - $item->item_tax = wpinv_sanitize_amount( $item_data->tax ); |
|
| 479 | - $item->item_discount = wpinv_sanitize_amount( $item_data->discount ); |
|
| 480 | - $item->set_name( $item_data->item_name ); |
|
| 481 | - $item->set_description( $item_data->item_description ); |
|
| 482 | - $item->set_price( $item_data->item_price ); |
|
| 483 | - $item->set_quantity( $item_data->quantity ); |
|
| 484 | - $item->set_item_meta( $item_data->meta ); |
|
| 477 | + // Set item data. |
|
| 478 | + $item->item_tax = wpinv_sanitize_amount( $item_data->tax ); |
|
| 479 | + $item->item_discount = wpinv_sanitize_amount( $item_data->discount ); |
|
| 480 | + $item->set_name( $item_data->item_name ); |
|
| 481 | + $item->set_description( $item_data->item_description ); |
|
| 482 | + $item->set_price( $item_data->item_price ); |
|
| 483 | + $item->set_quantity( $item_data->quantity ); |
|
| 484 | + $item->set_item_meta( $item_data->meta ); |
|
| 485 | 485 | |
| 486 | - $invoice->add_item( $item ); |
|
| 487 | - } |
|
| 486 | + $invoice->add_item( $item ); |
|
| 487 | + } |
|
| 488 | 488 | |
| 489 | - } |
|
| 489 | + } |
|
| 490 | 490 | |
| 491 | - /** |
|
| 491 | + /** |
|
| 492 | 492 | * Saves cart details. |
| 493 | - * |
|
| 494 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 493 | + * |
|
| 494 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 495 | 495 | */ |
| 496 | 496 | public function save_items( $invoice ) { |
| 497 | 497 | |
| 498 | - // Delete previously existing items. |
|
| 499 | - $this->delete_items( $invoice ); |
|
| 498 | + // Delete previously existing items. |
|
| 499 | + $this->delete_items( $invoice ); |
|
| 500 | 500 | |
| 501 | - $table = $GLOBALS['wpdb']->prefix . 'getpaid_invoice_items'; |
|
| 501 | + $table = $GLOBALS['wpdb']->prefix . 'getpaid_invoice_items'; |
|
| 502 | 502 | |
| 503 | - foreach ( $invoice->get_cart_details() as $item_data ) { |
|
| 504 | - $item_data = array_map( 'maybe_serialize', $item_data ); |
|
| 505 | - $GLOBALS['wpdb']->insert( $table, $item_data ); |
|
| 506 | - } |
|
| 503 | + foreach ( $invoice->get_cart_details() as $item_data ) { |
|
| 504 | + $item_data = array_map( 'maybe_serialize', $item_data ); |
|
| 505 | + $GLOBALS['wpdb']->insert( $table, $item_data ); |
|
| 506 | + } |
|
| 507 | 507 | |
| 508 | - wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_cart_details' ); |
|
| 509 | - do_action( "getpaid_invoice_save_items", $invoice ); |
|
| 508 | + wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_cart_details' ); |
|
| 509 | + do_action( "getpaid_invoice_save_items", $invoice ); |
|
| 510 | 510 | |
| 511 | - } |
|
| 511 | + } |
|
| 512 | 512 | |
| 513 | - /** |
|
| 513 | + /** |
|
| 514 | 514 | * Deletes an invoice's cart details from the database. |
| 515 | - * |
|
| 516 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 515 | + * |
|
| 516 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 517 | 517 | */ |
| 518 | 518 | public function delete_items( $invoice ) { |
| 519 | - $table = $GLOBALS['wpdb']->prefix . 'getpaid_invoice_items'; |
|
| 520 | - return $GLOBALS['wpdb']->delete( $table, array( 'post_id' => $invoice->get_id() ) ); |
|
| 521 | - } |
|
| 519 | + $table = $GLOBALS['wpdb']->prefix . 'getpaid_invoice_items'; |
|
| 520 | + return $GLOBALS['wpdb']->delete( $table, array( 'post_id' => $invoice->get_id() ) ); |
|
| 521 | + } |
|
| 522 | 522 | |
| 523 | - /** |
|
| 523 | + /** |
|
| 524 | 524 | * Deletes an invoice's special fields from the database. |
| 525 | - * |
|
| 526 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 525 | + * |
|
| 526 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 527 | 527 | */ |
| 528 | 528 | public function delete_special_fields( $invoice ) { |
| 529 | - $table = $GLOBALS['wpdb']->prefix . 'getpaid_invoices'; |
|
| 530 | - return $GLOBALS['wpdb']->delete( $table, array( 'post_id' => $invoice->get_id() ) ); |
|
| 531 | - } |
|
| 529 | + $table = $GLOBALS['wpdb']->prefix . 'getpaid_invoices'; |
|
| 530 | + return $GLOBALS['wpdb']->delete( $table, array( 'post_id' => $invoice->get_id() ) ); |
|
| 531 | + } |
|
| 532 | 532 | |
| 533 | - /** |
|
| 534 | - * Get the status to save to the post object. |
|
| 535 | - * |
|
| 536 | - * |
|
| 537 | - * @since 1.0.19 |
|
| 538 | - * @param WPInv_Invoice $object GetPaid_Data object. |
|
| 539 | - * @return string |
|
| 540 | - */ |
|
| 541 | - protected function get_post_status( $object ) { |
|
| 542 | - $object_status = $object->get_status( 'edit' ); |
|
| 543 | - |
|
| 544 | - if ( ! $object_status ) { |
|
| 545 | - $object_status = $object->get_default_status(); |
|
| 546 | - } |
|
| 547 | - |
|
| 548 | - return $object_status; |
|
| 549 | - } |
|
| 533 | + /** |
|
| 534 | + * Get the status to save to the post object. |
|
| 535 | + * |
|
| 536 | + * |
|
| 537 | + * @since 1.0.19 |
|
| 538 | + * @param WPInv_Invoice $object GetPaid_Data object. |
|
| 539 | + * @return string |
|
| 540 | + */ |
|
| 541 | + protected function get_post_status( $object ) { |
|
| 542 | + $object_status = $object->get_status( 'edit' ); |
|
| 543 | + |
|
| 544 | + if ( ! $object_status ) { |
|
| 545 | + $object_status = $object->get_default_status(); |
|
| 546 | + } |
|
| 547 | + |
|
| 548 | + return $object_status; |
|
| 549 | + } |
|
| 550 | 550 | |
| 551 | 551 | } |
@@ -13,282 +13,282 @@ |
||
| 13 | 13 | class GetPaid_Subscription_Notification_Emails { |
| 14 | 14 | |
| 15 | 15 | /** |
| 16 | - * The array of subscription email actions. |
|
| 17 | - * |
|
| 18 | - * @param array |
|
| 19 | - */ |
|
| 20 | - public $subscription_actions; |
|
| 16 | + * The array of subscription email actions. |
|
| 17 | + * |
|
| 18 | + * @param array |
|
| 19 | + */ |
|
| 20 | + public $subscription_actions; |
|
| 21 | 21 | |
| 22 | 22 | /** |
| 23 | - * Class constructor |
|
| 23 | + * Class constructor |
|
| 24 | 24 | * |
| 25 | - */ |
|
| 26 | - public function __construct() { |
|
| 27 | - |
|
| 28 | - $this->subscription_actions = apply_filters( |
|
| 29 | - 'getpaid_notification_email_subscription_triggers', |
|
| 30 | - array( |
|
| 31 | - 'getpaid_subscription_trialling' => 'subscription_trial', |
|
| 32 | - 'getpaid_subscription_cancelled' => 'subscription_cancelled', |
|
| 33 | - 'getpaid_subscription_expired' => 'subscription_expired', |
|
| 34 | - 'getpaid_subscription_completed' => 'subscription_complete', |
|
| 35 | - 'getpaid_daily_maintenance' => 'renewal_reminder', |
|
| 36 | - ) |
|
| 37 | - ); |
|
| 38 | - |
|
| 39 | - $this->init_hooks(); |
|
| 25 | + */ |
|
| 26 | + public function __construct() { |
|
| 27 | + |
|
| 28 | + $this->subscription_actions = apply_filters( |
|
| 29 | + 'getpaid_notification_email_subscription_triggers', |
|
| 30 | + array( |
|
| 31 | + 'getpaid_subscription_trialling' => 'subscription_trial', |
|
| 32 | + 'getpaid_subscription_cancelled' => 'subscription_cancelled', |
|
| 33 | + 'getpaid_subscription_expired' => 'subscription_expired', |
|
| 34 | + 'getpaid_subscription_completed' => 'subscription_complete', |
|
| 35 | + 'getpaid_daily_maintenance' => 'renewal_reminder', |
|
| 36 | + ) |
|
| 37 | + ); |
|
| 38 | + |
|
| 39 | + $this->init_hooks(); |
|
| 40 | 40 | |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | /** |
| 44 | - * Registers email hooks. |
|
| 45 | - */ |
|
| 46 | - public function init_hooks() { |
|
| 47 | - |
|
| 48 | - add_filter( 'getpaid_get_email_merge_tags', array( $this, 'subscription_merge_tags' ), 10, 2 ); |
|
| 49 | - foreach ( $this->subscription_actions as $hook => $email_type ) { |
|
| 50 | - |
|
| 51 | - $email = new GetPaid_Notification_Email( $email_type ); |
|
| 52 | - |
|
| 53 | - if ( ! $email->is_active() ) { |
|
| 54 | - continue; |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - if ( method_exists( $this, $email_type ) ) { |
|
| 58 | - add_action( $hook, array( $this, $email_type ), 100, 2 ); |
|
| 59 | - continue; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - do_action( 'getpaid_subscription_notification_email_register_hook', $email_type, $hook ); |
|
| 63 | - |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Filters subscription merge tags. |
|
| 70 | - * |
|
| 71 | - * @param array $merge_tags |
|
| 72 | - * @param mixed|WPInv_Invoice|WPInv_Subscription $object |
|
| 73 | - */ |
|
| 74 | - public function subscription_merge_tags( $merge_tags, $object ) { |
|
| 75 | - |
|
| 76 | - if ( is_a( $object, 'WPInv_Subscription' ) ) { |
|
| 77 | - $merge_tags = array_merge( |
|
| 78 | - $merge_tags, |
|
| 79 | - $this->get_subscription_merge_tags( $object ) |
|
| 80 | - ); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - return $merge_tags; |
|
| 84 | - |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Generates subscription merge tags. |
|
| 89 | - * |
|
| 90 | - * @param WPInv_Subscription $subscription |
|
| 91 | - * @return array |
|
| 92 | - */ |
|
| 93 | - public function get_subscription_merge_tags( $subscription ) { |
|
| 94 | - |
|
| 95 | - // Abort if it does not exist. |
|
| 96 | - if ( ! $subscription->get_id() ) { |
|
| 97 | - return array(); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - $invoice = $subscription->get_parent_invoice(); |
|
| 101 | - return array( |
|
| 102 | - '{subscription_renewal_date}' => getpaid_format_date_value( $subscription->get_next_renewal_date(), __( 'Never', 'invoicing' ) ), |
|
| 103 | - '{subscription_created}' => getpaid_format_date_value( $subscription->get_date_created() ), |
|
| 104 | - '{subscription_status}' => sanitize_text_field( $subscription->get_status_label() ), |
|
| 105 | - '{subscription_profile_id}' => sanitize_text_field( $subscription->get_profile_id() ), |
|
| 106 | - '{subscription_id}' => absint( $subscription->get_id() ), |
|
| 107 | - '{subscription_recurring_amount}' => wpinv_price( wpinv_format_amount( $subscription->get_recurring_amount() ), $invoice->get_currency() ), |
|
| 108 | - '{subscription_initial_amount}' => wpinv_price( wpinv_format_amount( $subscription->get_initial_amount() ), $invoice->get_currency() ), |
|
| 109 | - '{subscription_recurring_period}' => getpaid_get_subscription_period_label( $subscription->get_period(), $subscription->get_frequency(), '' ), |
|
| 110 | - '{subscription_bill_times}' => $subscription->get_bill_times(), |
|
| 111 | - '{subscription_url}' => esc_url( $subscription->get_view_url() ), |
|
| 112 | - ); |
|
| 113 | - |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * Checks if we should send a notification for a subscription. |
|
| 118 | - * |
|
| 119 | - * @param WPInv_Invoice $invoice |
|
| 120 | - * @return bool |
|
| 121 | - */ |
|
| 122 | - public function should_send_notification( $invoice ) { |
|
| 123 | - return 0 != $invoice->get_id(); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Returns notification recipients. |
|
| 128 | - * |
|
| 129 | - * @param WPInv_Invoice $invoice |
|
| 130 | - * @return array |
|
| 131 | - */ |
|
| 132 | - public function get_recipients( $invoice ) { |
|
| 133 | - $recipients = array( $invoice->get_email() ); |
|
| 134 | - |
|
| 135 | - $cc = $invoice->get_email_cc(); |
|
| 136 | - |
|
| 137 | - if ( ! empty( $cc ) ) { |
|
| 138 | - $cc = array_map( 'sanitize_email', wpinv_parse_list( $cc ) ); |
|
| 139 | - $recipients = array_filter( array_unique( array_merge( $recipients, $cc ) ) ); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - return $recipients; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Helper function to send an email. |
|
| 147 | - * |
|
| 148 | - * @param WPInv_Subscription $subscription |
|
| 149 | - * @param GetPaid_Notification_Email $email |
|
| 150 | - * @param string $type |
|
| 151 | - * @param array $extra_args Extra template args. |
|
| 152 | - */ |
|
| 153 | - public function send_email( $subscription, $email, $type, $extra_args = array() ) { |
|
| 154 | - |
|
| 155 | - // Abort in case the parent invoice does not exist. |
|
| 156 | - $invoice = $subscription->get_parent_invoice(); |
|
| 157 | - if ( ! $this->should_send_notification( $invoice ) ) { |
|
| 158 | - return; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - do_action( 'getpaid_before_send_subscription_notification', $type, $subscription, $email ); |
|
| 162 | - |
|
| 163 | - $recipients = $this->get_recipients( $invoice ); |
|
| 164 | - $mailer = new GetPaid_Notification_Email_Sender(); |
|
| 165 | - $merge_tags = $email->get_merge_tags(); |
|
| 166 | - $content = $email->get_content( $merge_tags, $extra_args ); |
|
| 167 | - $subject = $email->add_merge_tags( $email->get_subject(), $merge_tags ); |
|
| 168 | - $attachments = $email->get_attachments(); |
|
| 169 | - |
|
| 170 | - $result = $mailer->send( |
|
| 171 | - apply_filters( 'getpaid_subscription_email_recipients', wpinv_parse_list( $recipients ), $email ), |
|
| 172 | - $subject, |
|
| 173 | - $content, |
|
| 174 | - $attachments |
|
| 175 | - ); |
|
| 176 | - |
|
| 177 | - // Maybe send a copy to the admin. |
|
| 178 | - if ( $email->include_admin_bcc() ) { |
|
| 179 | - $mailer->send( |
|
| 180 | - wpinv_get_admin_email(), |
|
| 181 | - $subject . __( ' - ADMIN BCC COPY', 'invoicing' ), |
|
| 182 | - $content, |
|
| 183 | - $attachments |
|
| 184 | - ); |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - if ( ! $result ) { |
|
| 188 | - $subscription->get_parent_invoice()->add_note( sprintf( __( 'Failed sending %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - do_action( 'getpaid_after_send_subscription_notification', $type, $subscription, $email ); |
|
| 192 | - |
|
| 193 | - } |
|
| 44 | + * Registers email hooks. |
|
| 45 | + */ |
|
| 46 | + public function init_hooks() { |
|
| 47 | + |
|
| 48 | + add_filter( 'getpaid_get_email_merge_tags', array( $this, 'subscription_merge_tags' ), 10, 2 ); |
|
| 49 | + foreach ( $this->subscription_actions as $hook => $email_type ) { |
|
| 50 | + |
|
| 51 | + $email = new GetPaid_Notification_Email( $email_type ); |
|
| 52 | + |
|
| 53 | + if ( ! $email->is_active() ) { |
|
| 54 | + continue; |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + if ( method_exists( $this, $email_type ) ) { |
|
| 58 | + add_action( $hook, array( $this, $email_type ), 100, 2 ); |
|
| 59 | + continue; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + do_action( 'getpaid_subscription_notification_email_register_hook', $email_type, $hook ); |
|
| 63 | + |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + } |
|
| 194 | 67 | |
| 195 | 68 | /** |
| 196 | - * Sends a new trial notification. |
|
| 197 | - * |
|
| 198 | - * @param WPInv_Subscription $subscription |
|
| 199 | - */ |
|
| 200 | - public function subscription_trial( $subscription ) { |
|
| 69 | + * Filters subscription merge tags. |
|
| 70 | + * |
|
| 71 | + * @param array $merge_tags |
|
| 72 | + * @param mixed|WPInv_Invoice|WPInv_Subscription $object |
|
| 73 | + */ |
|
| 74 | + public function subscription_merge_tags( $merge_tags, $object ) { |
|
| 201 | 75 | |
| 202 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 203 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 76 | + if ( is_a( $object, 'WPInv_Subscription' ) ) { |
|
| 77 | + $merge_tags = array_merge( |
|
| 78 | + $merge_tags, |
|
| 79 | + $this->get_subscription_merge_tags( $object ) |
|
| 80 | + ); |
|
| 81 | + } |
|
| 204 | 82 | |
| 205 | - } |
|
| 83 | + return $merge_tags; |
|
| 206 | 84 | |
| 207 | - /** |
|
| 208 | - * Sends a cancelled subscription notification. |
|
| 209 | - * |
|
| 210 | - * @param WPInv_Subscription $subscription |
|
| 211 | - */ |
|
| 212 | - public function subscription_cancelled( $subscription ) { |
|
| 85 | + } |
|
| 213 | 86 | |
| 214 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 215 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 87 | + /** |
|
| 88 | + * Generates subscription merge tags. |
|
| 89 | + * |
|
| 90 | + * @param WPInv_Subscription $subscription |
|
| 91 | + * @return array |
|
| 92 | + */ |
|
| 93 | + public function get_subscription_merge_tags( $subscription ) { |
|
| 94 | + |
|
| 95 | + // Abort if it does not exist. |
|
| 96 | + if ( ! $subscription->get_id() ) { |
|
| 97 | + return array(); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + $invoice = $subscription->get_parent_invoice(); |
|
| 101 | + return array( |
|
| 102 | + '{subscription_renewal_date}' => getpaid_format_date_value( $subscription->get_next_renewal_date(), __( 'Never', 'invoicing' ) ), |
|
| 103 | + '{subscription_created}' => getpaid_format_date_value( $subscription->get_date_created() ), |
|
| 104 | + '{subscription_status}' => sanitize_text_field( $subscription->get_status_label() ), |
|
| 105 | + '{subscription_profile_id}' => sanitize_text_field( $subscription->get_profile_id() ), |
|
| 106 | + '{subscription_id}' => absint( $subscription->get_id() ), |
|
| 107 | + '{subscription_recurring_amount}' => wpinv_price( wpinv_format_amount( $subscription->get_recurring_amount() ), $invoice->get_currency() ), |
|
| 108 | + '{subscription_initial_amount}' => wpinv_price( wpinv_format_amount( $subscription->get_initial_amount() ), $invoice->get_currency() ), |
|
| 109 | + '{subscription_recurring_period}' => getpaid_get_subscription_period_label( $subscription->get_period(), $subscription->get_frequency(), '' ), |
|
| 110 | + '{subscription_bill_times}' => $subscription->get_bill_times(), |
|
| 111 | + '{subscription_url}' => esc_url( $subscription->get_view_url() ), |
|
| 112 | + ); |
|
| 216 | 113 | |
| 217 | - } |
|
| 114 | + } |
|
| 218 | 115 | |
| 219 | - /** |
|
| 220 | - * Sends a subscription expired notification. |
|
| 221 | - * |
|
| 222 | - * @param WPInv_Subscription $subscription |
|
| 223 | - */ |
|
| 224 | - public function subscription_expired( $subscription ) { |
|
| 116 | + /** |
|
| 117 | + * Checks if we should send a notification for a subscription. |
|
| 118 | + * |
|
| 119 | + * @param WPInv_Invoice $invoice |
|
| 120 | + * @return bool |
|
| 121 | + */ |
|
| 122 | + public function should_send_notification( $invoice ) { |
|
| 123 | + return 0 != $invoice->get_id(); |
|
| 124 | + } |
|
| 225 | 125 | |
| 226 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 227 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 126 | + /** |
|
| 127 | + * Returns notification recipients. |
|
| 128 | + * |
|
| 129 | + * @param WPInv_Invoice $invoice |
|
| 130 | + * @return array |
|
| 131 | + */ |
|
| 132 | + public function get_recipients( $invoice ) { |
|
| 133 | + $recipients = array( $invoice->get_email() ); |
|
| 228 | 134 | |
| 229 | - } |
|
| 135 | + $cc = $invoice->get_email_cc(); |
|
| 230 | 136 | |
| 231 | - /** |
|
| 232 | - * Sends a completed subscription notification. |
|
| 233 | - * |
|
| 234 | - * @param WPInv_Subscription $subscription |
|
| 235 | - */ |
|
| 236 | - public function subscription_complete( $subscription ) { |
|
| 137 | + if ( ! empty( $cc ) ) { |
|
| 138 | + $cc = array_map( 'sanitize_email', wpinv_parse_list( $cc ) ); |
|
| 139 | + $recipients = array_filter( array_unique( array_merge( $recipients, $cc ) ) ); |
|
| 140 | + } |
|
| 237 | 141 | |
| 238 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 239 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 142 | + return $recipients; |
|
| 143 | + } |
|
| 240 | 144 | |
| 241 | - } |
|
| 145 | + /** |
|
| 146 | + * Helper function to send an email. |
|
| 147 | + * |
|
| 148 | + * @param WPInv_Subscription $subscription |
|
| 149 | + * @param GetPaid_Notification_Email $email |
|
| 150 | + * @param string $type |
|
| 151 | + * @param array $extra_args Extra template args. |
|
| 152 | + */ |
|
| 153 | + public function send_email( $subscription, $email, $type, $extra_args = array() ) { |
|
| 154 | + |
|
| 155 | + // Abort in case the parent invoice does not exist. |
|
| 156 | + $invoice = $subscription->get_parent_invoice(); |
|
| 157 | + if ( ! $this->should_send_notification( $invoice ) ) { |
|
| 158 | + return; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + do_action( 'getpaid_before_send_subscription_notification', $type, $subscription, $email ); |
|
| 162 | + |
|
| 163 | + $recipients = $this->get_recipients( $invoice ); |
|
| 164 | + $mailer = new GetPaid_Notification_Email_Sender(); |
|
| 165 | + $merge_tags = $email->get_merge_tags(); |
|
| 166 | + $content = $email->get_content( $merge_tags, $extra_args ); |
|
| 167 | + $subject = $email->add_merge_tags( $email->get_subject(), $merge_tags ); |
|
| 168 | + $attachments = $email->get_attachments(); |
|
| 169 | + |
|
| 170 | + $result = $mailer->send( |
|
| 171 | + apply_filters( 'getpaid_subscription_email_recipients', wpinv_parse_list( $recipients ), $email ), |
|
| 172 | + $subject, |
|
| 173 | + $content, |
|
| 174 | + $attachments |
|
| 175 | + ); |
|
| 176 | + |
|
| 177 | + // Maybe send a copy to the admin. |
|
| 178 | + if ( $email->include_admin_bcc() ) { |
|
| 179 | + $mailer->send( |
|
| 180 | + wpinv_get_admin_email(), |
|
| 181 | + $subject . __( ' - ADMIN BCC COPY', 'invoicing' ), |
|
| 182 | + $content, |
|
| 183 | + $attachments |
|
| 184 | + ); |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + if ( ! $result ) { |
|
| 188 | + $subscription->get_parent_invoice()->add_note( sprintf( __( 'Failed sending %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + do_action( 'getpaid_after_send_subscription_notification', $type, $subscription, $email ); |
|
| 242 | 192 | |
| 243 | - /** |
|
| 244 | - * Sends a subscription renewal reminder notification. |
|
| 245 | - * |
|
| 246 | - */ |
|
| 247 | - public function renewal_reminder() { |
|
| 193 | + } |
|
| 248 | 194 | |
| 249 | - $email = new GetPaid_Notification_Email( __FUNCTION__ ); |
|
| 195 | + /** |
|
| 196 | + * Sends a new trial notification. |
|
| 197 | + * |
|
| 198 | + * @param WPInv_Subscription $subscription |
|
| 199 | + */ |
|
| 200 | + public function subscription_trial( $subscription ) { |
|
| 250 | 201 | |
| 251 | - // Fetch reminder days. |
|
| 252 | - $reminder_days = array_unique( wp_parse_id_list( $email->get_option( 'days' ) ) ); |
|
| 202 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 203 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 253 | 204 | |
| 254 | - // Abort if non is set. |
|
| 255 | - if ( empty( $reminder_days ) ) { |
|
| 256 | - return; |
|
| 257 | - } |
|
| 205 | + } |
|
| 258 | 206 | |
| 259 | - // Fetch matching subscriptions. |
|
| 207 | + /** |
|
| 208 | + * Sends a cancelled subscription notification. |
|
| 209 | + * |
|
| 210 | + * @param WPInv_Subscription $subscription |
|
| 211 | + */ |
|
| 212 | + public function subscription_cancelled( $subscription ) { |
|
| 213 | + |
|
| 214 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 215 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 216 | + |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * Sends a subscription expired notification. |
|
| 221 | + * |
|
| 222 | + * @param WPInv_Subscription $subscription |
|
| 223 | + */ |
|
| 224 | + public function subscription_expired( $subscription ) { |
|
| 225 | + |
|
| 226 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 227 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 228 | + |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + /** |
|
| 232 | + * Sends a completed subscription notification. |
|
| 233 | + * |
|
| 234 | + * @param WPInv_Subscription $subscription |
|
| 235 | + */ |
|
| 236 | + public function subscription_complete( $subscription ) { |
|
| 237 | + |
|
| 238 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 239 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 240 | + |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * Sends a subscription renewal reminder notification. |
|
| 245 | + * |
|
| 246 | + */ |
|
| 247 | + public function renewal_reminder() { |
|
| 248 | + |
|
| 249 | + $email = new GetPaid_Notification_Email( __FUNCTION__ ); |
|
| 250 | + |
|
| 251 | + // Fetch reminder days. |
|
| 252 | + $reminder_days = array_unique( wp_parse_id_list( $email->get_option( 'days' ) ) ); |
|
| 253 | + |
|
| 254 | + // Abort if non is set. |
|
| 255 | + if ( empty( $reminder_days ) ) { |
|
| 256 | + return; |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + // Fetch matching subscriptions. |
|
| 260 | 260 | $args = array( |
| 261 | 261 | 'number' => -1, |
| 262 | - 'count_total' => false, |
|
| 263 | - 'status' => 'trialling active', |
|
| 262 | + 'count_total' => false, |
|
| 263 | + 'status' => 'trialling active', |
|
| 264 | 264 | 'date_expires_query' => array( |
| 265 | - 'relation' => 'OR' |
|
| 265 | + 'relation' => 'OR' |
|
| 266 | 266 | ), |
| 267 | - ); |
|
| 267 | + ); |
|
| 268 | 268 | |
| 269 | - foreach ( $reminder_days as $days ) { |
|
| 270 | - $date = date_parse( date( 'Y-m-d', strtotime( "+$days days", current_time( 'timestamp' ) ) ) ); |
|
| 269 | + foreach ( $reminder_days as $days ) { |
|
| 270 | + $date = date_parse( date( 'Y-m-d', strtotime( "+$days days", current_time( 'timestamp' ) ) ) ); |
|
| 271 | 271 | |
| 272 | - $args['date_expires_query'][] = array( |
|
| 273 | - 'year' => $date['year'], |
|
| 274 | - 'month' => $date['month'], |
|
| 275 | - 'day' => $date['day'], |
|
| 276 | - ); |
|
| 272 | + $args['date_expires_query'][] = array( |
|
| 273 | + 'year' => $date['year'], |
|
| 274 | + 'month' => $date['month'], |
|
| 275 | + 'day' => $date['day'], |
|
| 276 | + ); |
|
| 277 | 277 | |
| 278 | - } |
|
| 278 | + } |
|
| 279 | 279 | |
| 280 | - $subscriptions = new GetPaid_Subscriptions_Query( $args ); |
|
| 280 | + $subscriptions = new GetPaid_Subscriptions_Query( $args ); |
|
| 281 | 281 | |
| 282 | 282 | foreach ( $subscriptions as $subscription ) { |
| 283 | 283 | |
| 284 | - // Skip packages. |
|
| 285 | - if ( get_post_meta( $subscription->get_product_id(), '_wpinv_type', true ) != 'package' ) { |
|
| 286 | - $email->object = $subscription; |
|
| 287 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 288 | - } |
|
| 284 | + // Skip packages. |
|
| 285 | + if ( get_post_meta( $subscription->get_product_id(), '_wpinv_type', true ) != 'package' ) { |
|
| 286 | + $email->object = $subscription; |
|
| 287 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 288 | + } |
|
| 289 | 289 | |
| 290 | - } |
|
| 290 | + } |
|
| 291 | 291 | |
| 292 | - } |
|
| 292 | + } |
|
| 293 | 293 | |
| 294 | 294 | } |
@@ -81,13 +81,13 @@ discard block |
||
| 81 | 81 | */ |
| 82 | 82 | function wpinv_get_invoice_statuses( $draft = false, $trashed = false, $invoice = false ) { |
| 83 | 83 | |
| 84 | - $invoice_statuses = array( |
|
| 85 | - 'wpi-pending' => _x( 'Pending payment', 'Invoice status', 'invoicing' ), |
|
| 84 | + $invoice_statuses = array( |
|
| 85 | + 'wpi-pending' => _x( 'Pending payment', 'Invoice status', 'invoicing' ), |
|
| 86 | 86 | 'publish' => _x( 'Paid', 'Invoice status', 'invoicing' ), |
| 87 | 87 | 'wpi-processing' => _x( 'Processing', 'Invoice status', 'invoicing' ), |
| 88 | - 'wpi-onhold' => _x( 'On hold', 'Invoice status', 'invoicing' ), |
|
| 89 | - 'wpi-cancelled' => _x( 'Cancelled', 'Invoice status', 'invoicing' ), |
|
| 90 | - 'wpi-refunded' => _x( 'Refunded', 'Invoice status', 'invoicing' ), |
|
| 88 | + 'wpi-onhold' => _x( 'On hold', 'Invoice status', 'invoicing' ), |
|
| 89 | + 'wpi-cancelled' => _x( 'Cancelled', 'Invoice status', 'invoicing' ), |
|
| 90 | + 'wpi-refunded' => _x( 'Refunded', 'Invoice status', 'invoicing' ), |
|
| 91 | 91 | 'wpi-failed' => _x( 'Failed', 'Invoice status', 'invoicing' ), |
| 92 | 92 | 'wpi-renewal' => _x( 'Renewal Payment', 'Invoice status', 'invoicing' ), |
| 93 | 93 | ); |
@@ -104,7 +104,7 @@ discard block |
||
| 104 | 104 | $invoice = $invoice->get_post_type(); |
| 105 | 105 | } |
| 106 | 106 | |
| 107 | - return apply_filters( 'wpinv_statuses', $invoice_statuses, $invoice ); |
|
| 107 | + return apply_filters( 'wpinv_statuses', $invoice_statuses, $invoice ); |
|
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | /** |
@@ -222,25 +222,25 @@ discard block |
||
| 222 | 222 | * @return string |
| 223 | 223 | */ |
| 224 | 224 | function getpaid_get_price_format() { |
| 225 | - $currency_pos = wpinv_currency_position(); |
|
| 226 | - $format = '%1$s%2$s'; |
|
| 225 | + $currency_pos = wpinv_currency_position(); |
|
| 226 | + $format = '%1$s%2$s'; |
|
| 227 | 227 | |
| 228 | - switch ( $currency_pos ) { |
|
| 229 | - case 'left': |
|
| 230 | - $format = '%1$s%2$s'; |
|
| 231 | - break; |
|
| 232 | - case 'right': |
|
| 233 | - $format = '%2$s%1$s'; |
|
| 234 | - break; |
|
| 235 | - case 'left_space': |
|
| 236 | - $format = '%1$s %2$s'; |
|
| 237 | - break; |
|
| 238 | - case 'right_space': |
|
| 239 | - $format = '%2$s %1$s'; |
|
| 240 | - break; |
|
| 241 | - } |
|
| 228 | + switch ( $currency_pos ) { |
|
| 229 | + case 'left': |
|
| 230 | + $format = '%1$s%2$s'; |
|
| 231 | + break; |
|
| 232 | + case 'right': |
|
| 233 | + $format = '%2$s%1$s'; |
|
| 234 | + break; |
|
| 235 | + case 'left_space': |
|
| 236 | + $format = '%1$s %2$s'; |
|
| 237 | + break; |
|
| 238 | + case 'right_space': |
|
| 239 | + $format = '%2$s %1$s'; |
|
| 240 | + break; |
|
| 241 | + } |
|
| 242 | 242 | |
| 243 | - return apply_filters( 'getpaid_price_format', $format, $currency_pos ); |
|
| 243 | + return apply_filters( 'getpaid_price_format', $format, $currency_pos ); |
|
| 244 | 244 | } |
| 245 | 245 | |
| 246 | 246 | /** |
@@ -343,13 +343,13 @@ discard block |
||
| 343 | 343 | * @param mixed $value Value. |
| 344 | 344 | */ |
| 345 | 345 | function getpaid_maybe_define_constant( $name, $value ) { |
| 346 | - if ( ! defined( $name ) ) { |
|
| 347 | - define( $name, $value ); |
|
| 348 | - } |
|
| 346 | + if ( ! defined( $name ) ) { |
|
| 347 | + define( $name, $value ); |
|
| 348 | + } |
|
| 349 | 349 | } |
| 350 | 350 | |
| 351 | 351 | function wpinv_get_php_arg_separator_output() { |
| 352 | - return ini_get( 'arg_separator.output' ); |
|
| 352 | + return ini_get( 'arg_separator.output' ); |
|
| 353 | 353 | } |
| 354 | 354 | |
| 355 | 355 | function wpinv_rgb_from_hex( $color ) { |
@@ -700,11 +700,11 @@ discard block |
||
| 700 | 700 | $list = array(); |
| 701 | 701 | } |
| 702 | 702 | |
| 703 | - if ( ! is_array( $list ) ) { |
|
| 704 | - return preg_split( '/[\s,]+/', $list, -1, PREG_SPLIT_NO_EMPTY ); |
|
| 705 | - } |
|
| 703 | + if ( ! is_array( $list ) ) { |
|
| 704 | + return preg_split( '/[\s,]+/', $list, -1, PREG_SPLIT_NO_EMPTY ); |
|
| 705 | + } |
|
| 706 | 706 | |
| 707 | - return $list; |
|
| 707 | + return $list; |
|
| 708 | 708 | } |
| 709 | 709 | |
| 710 | 710 | /** |
@@ -724,9 +724,9 @@ discard block |
||
| 724 | 724 | } |
| 725 | 725 | |
| 726 | 726 | $data = apply_filters( "wpinv_get_$key", include WPINV_PLUGIN_DIR . "includes/data/$key.php" ); |
| 727 | - wp_cache_set( "wpinv-data-$key", $data, 'wpinv' ); |
|
| 727 | + wp_cache_set( "wpinv-data-$key", $data, 'wpinv' ); |
|
| 728 | 728 | |
| 729 | - return $data; |
|
| 729 | + return $data; |
|
| 730 | 730 | } |
| 731 | 731 | |
| 732 | 732 | /** |
@@ -755,17 +755,17 @@ discard block |
||
| 755 | 755 | */ |
| 756 | 756 | function wpinv_clean( $var ) { |
| 757 | 757 | |
| 758 | - if ( is_array( $var ) ) { |
|
| 759 | - return array_map( 'wpinv_clean', $var ); |
|
| 758 | + if ( is_array( $var ) ) { |
|
| 759 | + return array_map( 'wpinv_clean', $var ); |
|
| 760 | 760 | } |
| 761 | 761 | |
| 762 | 762 | if ( is_object( $var ) ) { |
| 763 | - $object_vars = get_object_vars( $var ); |
|
| 764 | - foreach ( $object_vars as $property_name => $property_value ) { |
|
| 765 | - $var->$property_name = wpinv_clean( $property_value ); |
|
| 763 | + $object_vars = get_object_vars( $var ); |
|
| 764 | + foreach ( $object_vars as $property_name => $property_value ) { |
|
| 765 | + $var->$property_name = wpinv_clean( $property_value ); |
|
| 766 | 766 | } |
| 767 | 767 | return $var; |
| 768 | - } |
|
| 768 | + } |
|
| 769 | 769 | |
| 770 | 770 | return is_string( $var ) ? sanitize_text_field( $var ) : $var; |
| 771 | 771 | } |
@@ -778,7 +778,7 @@ discard block |
||
| 778 | 778 | */ |
| 779 | 779 | function getpaid_convert_price_string_to_options( $str ) { |
| 780 | 780 | |
| 781 | - $raw_options = array_map( 'trim', explode( ',', $str ) ); |
|
| 781 | + $raw_options = array_map( 'trim', explode( ',', $str ) ); |
|
| 782 | 782 | $options = array(); |
| 783 | 783 | |
| 784 | 784 | foreach ( $raw_options as $option ) { |
@@ -856,7 +856,7 @@ discard block |
||
| 856 | 856 | * @return string |
| 857 | 857 | */ |
| 858 | 858 | function getpaid_date_format() { |
| 859 | - return apply_filters( 'getpaid_date_format', get_option( 'date_format' ) ); |
|
| 859 | + return apply_filters( 'getpaid_date_format', get_option( 'date_format' ) ); |
|
| 860 | 860 | } |
| 861 | 861 | |
| 862 | 862 | /** |
@@ -865,7 +865,7 @@ discard block |
||
| 865 | 865 | * @return string |
| 866 | 866 | */ |
| 867 | 867 | function getpaid_time_format() { |
| 868 | - return apply_filters( 'getpaid_time_format', get_option( 'time_format' ) ); |
|
| 868 | + return apply_filters( 'getpaid_time_format', get_option( 'time_format' ) ); |
|
| 869 | 869 | } |
| 870 | 870 | |
| 871 | 871 | /** |
@@ -878,15 +878,15 @@ discard block |
||
| 878 | 878 | function getpaid_limit_length( $string, $limit ) { |
| 879 | 879 | $str_limit = $limit - 3; |
| 880 | 880 | |
| 881 | - if ( function_exists( 'mb_strimwidth' ) ) { |
|
| 882 | - if ( mb_strlen( $string ) > $limit ) { |
|
| 883 | - $string = mb_strimwidth( $string, 0, $str_limit ) . '...'; |
|
| 884 | - } |
|
| 885 | - } else { |
|
| 886 | - if ( strlen( $string ) > $limit ) { |
|
| 887 | - $string = substr( $string, 0, $str_limit ) . '...'; |
|
| 888 | - } |
|
| 889 | - } |
|
| 881 | + if ( function_exists( 'mb_strimwidth' ) ) { |
|
| 882 | + if ( mb_strlen( $string ) > $limit ) { |
|
| 883 | + $string = mb_strimwidth( $string, 0, $str_limit ) . '...'; |
|
| 884 | + } |
|
| 885 | + } else { |
|
| 886 | + if ( strlen( $string ) > $limit ) { |
|
| 887 | + $string = substr( $string, 0, $str_limit ) . '...'; |
|
| 888 | + } |
|
| 889 | + } |
|
| 890 | 890 | return $string; |
| 891 | 891 | |
| 892 | 892 | } |