@@ -1,7 +1,7 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | - exit; // Exit if accessed directly |
|
| 4 | + exit; // Exit if accessed directly |
|
| 5 | 5 | } |
| 6 | 6 | |
| 7 | 7 | /** |
@@ -11,1183 +11,1183 @@ discard block |
||
| 11 | 11 | */ |
| 12 | 12 | class AUI_Component_Input { |
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * Build the component. |
|
| 16 | - * |
|
| 17 | - * @param array $args |
|
| 18 | - * |
|
| 19 | - * @return string The rendered component. |
|
| 20 | - */ |
|
| 21 | - public static function input( $args = array() ) { |
|
| 22 | - $defaults = array( |
|
| 23 | - 'type' => 'text', |
|
| 24 | - 'name' => '', |
|
| 25 | - 'class' => '', |
|
| 26 | - 'wrap_class' => '', |
|
| 27 | - 'id' => '', |
|
| 28 | - 'placeholder' => '', |
|
| 29 | - 'title' => '', |
|
| 30 | - 'value' => '', |
|
| 31 | - 'required' => false, |
|
| 32 | - 'label' => '', |
|
| 33 | - 'label_after' => false, |
|
| 34 | - 'label_class' => '', |
|
| 35 | - 'label_col' => '2', |
|
| 36 | - 'label_type' => '', |
|
| 37 | - 'label_force_left' => false, // used to force checkbox label left when using horizontal |
|
| 38 | - // sets the label type, default: hidden. Options: hidden, top, horizontal, floating |
|
| 39 | - 'help_text' => '', |
|
| 40 | - 'validation_text' => '', |
|
| 41 | - 'validation_pattern' => '', |
|
| 42 | - 'no_wrap' => false, |
|
| 43 | - 'input_group_right' => '', |
|
| 44 | - 'input_group_left' => '', |
|
| 45 | - 'input_group_right_inside' => false, |
|
| 46 | - // forces the input group inside the input |
|
| 47 | - 'input_group_left_inside' => false, |
|
| 48 | - // forces the input group inside the input |
|
| 49 | - 'step' => '', |
|
| 50 | - 'switch' => false, |
|
| 51 | - // to show checkbox as a switch |
|
| 52 | - 'checked' => false, |
|
| 53 | - // set a checkbox or radio as selected |
|
| 54 | - 'password_toggle' => true, |
|
| 55 | - // toggle view/hide password |
|
| 56 | - 'element_require' => '', |
|
| 57 | - // [%element_id%] == "1" |
|
| 58 | - 'extra_attributes' => array(), |
|
| 59 | - // an array of extra attributes |
|
| 60 | - 'wrap_attributes' => array() |
|
| 61 | - ); |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Parse incoming $args into an array and merge it with $defaults |
|
| 65 | - */ |
|
| 66 | - $args = wp_parse_args( $args, $defaults ); |
|
| 67 | - $output = ''; |
|
| 68 | - if ( ! empty( $args['type'] ) ) { |
|
| 69 | - // hidden label option needs to be empty |
|
| 70 | - $args['label_type'] = $args['label_type'] == 'hidden' ? '' : $args['label_type']; |
|
| 71 | - |
|
| 72 | - $type = sanitize_html_class( $args['type'] ); |
|
| 73 | - |
|
| 74 | - $help_text = ''; |
|
| 75 | - $label = ''; |
|
| 76 | - $label_after = $args['label_after']; |
|
| 77 | - $label_args = array( |
|
| 78 | - 'title' => $args['label'], |
|
| 79 | - 'for' => $args['id'], |
|
| 80 | - 'class' => $args['label_class'] . " ", |
|
| 81 | - 'label_type' => $args['label_type'], |
|
| 82 | - 'label_col' => $args['label_col'] |
|
| 83 | - ); |
|
| 84 | - |
|
| 85 | - // floating labels need label after |
|
| 86 | - if ( $args['label_type'] == 'floating' && $type != 'checkbox' ) { |
|
| 87 | - $label_after = true; |
|
| 88 | - $args['placeholder'] = ' '; // set the placeholder not empty so the floating label works. |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - // Some special sauce for files |
|
| 92 | - if ( $type == 'file' ) { |
|
| 93 | - $label_after = true; // if type file we need the label after |
|
| 94 | - $args['class'] .= ' custom-file-input '; |
|
| 95 | - } elseif ( $type == 'checkbox' ) { |
|
| 96 | - $label_after = true; // if type file we need the label after |
|
| 97 | - $args['class'] .= ' custom-control-input '; |
|
| 98 | - } elseif ( $type == 'datepicker' || $type == 'timepicker' ) { |
|
| 99 | - $type = 'text'; |
|
| 100 | - //$args['class'] .= ' aui-flatpickr bg-initial '; |
|
| 101 | - $args['class'] .= ' bg-initial '; |
|
| 102 | - |
|
| 103 | - $args['extra_attributes']['data-aui-init'] = 'flatpickr'; |
|
| 104 | - // enqueue the script |
|
| 105 | - $aui_settings = AyeCode_UI_Settings::instance(); |
|
| 106 | - $aui_settings->enqueue_flatpickr(); |
|
| 107 | - } elseif ( $type == 'iconpicker' ) { |
|
| 108 | - $type = 'text'; |
|
| 109 | - //$args['class'] .= ' aui-flatpickr bg-initial '; |
|
| 14 | + /** |
|
| 15 | + * Build the component. |
|
| 16 | + * |
|
| 17 | + * @param array $args |
|
| 18 | + * |
|
| 19 | + * @return string The rendered component. |
|
| 20 | + */ |
|
| 21 | + public static function input( $args = array() ) { |
|
| 22 | + $defaults = array( |
|
| 23 | + 'type' => 'text', |
|
| 24 | + 'name' => '', |
|
| 25 | + 'class' => '', |
|
| 26 | + 'wrap_class' => '', |
|
| 27 | + 'id' => '', |
|
| 28 | + 'placeholder' => '', |
|
| 29 | + 'title' => '', |
|
| 30 | + 'value' => '', |
|
| 31 | + 'required' => false, |
|
| 32 | + 'label' => '', |
|
| 33 | + 'label_after' => false, |
|
| 34 | + 'label_class' => '', |
|
| 35 | + 'label_col' => '2', |
|
| 36 | + 'label_type' => '', |
|
| 37 | + 'label_force_left' => false, // used to force checkbox label left when using horizontal |
|
| 38 | + // sets the label type, default: hidden. Options: hidden, top, horizontal, floating |
|
| 39 | + 'help_text' => '', |
|
| 40 | + 'validation_text' => '', |
|
| 41 | + 'validation_pattern' => '', |
|
| 42 | + 'no_wrap' => false, |
|
| 43 | + 'input_group_right' => '', |
|
| 44 | + 'input_group_left' => '', |
|
| 45 | + 'input_group_right_inside' => false, |
|
| 46 | + // forces the input group inside the input |
|
| 47 | + 'input_group_left_inside' => false, |
|
| 48 | + // forces the input group inside the input |
|
| 49 | + 'step' => '', |
|
| 50 | + 'switch' => false, |
|
| 51 | + // to show checkbox as a switch |
|
| 52 | + 'checked' => false, |
|
| 53 | + // set a checkbox or radio as selected |
|
| 54 | + 'password_toggle' => true, |
|
| 55 | + // toggle view/hide password |
|
| 56 | + 'element_require' => '', |
|
| 57 | + // [%element_id%] == "1" |
|
| 58 | + 'extra_attributes' => array(), |
|
| 59 | + // an array of extra attributes |
|
| 60 | + 'wrap_attributes' => array() |
|
| 61 | + ); |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Parse incoming $args into an array and merge it with $defaults |
|
| 65 | + */ |
|
| 66 | + $args = wp_parse_args( $args, $defaults ); |
|
| 67 | + $output = ''; |
|
| 68 | + if ( ! empty( $args['type'] ) ) { |
|
| 69 | + // hidden label option needs to be empty |
|
| 70 | + $args['label_type'] = $args['label_type'] == 'hidden' ? '' : $args['label_type']; |
|
| 71 | + |
|
| 72 | + $type = sanitize_html_class( $args['type'] ); |
|
| 73 | + |
|
| 74 | + $help_text = ''; |
|
| 75 | + $label = ''; |
|
| 76 | + $label_after = $args['label_after']; |
|
| 77 | + $label_args = array( |
|
| 78 | + 'title' => $args['label'], |
|
| 79 | + 'for' => $args['id'], |
|
| 80 | + 'class' => $args['label_class'] . " ", |
|
| 81 | + 'label_type' => $args['label_type'], |
|
| 82 | + 'label_col' => $args['label_col'] |
|
| 83 | + ); |
|
| 84 | + |
|
| 85 | + // floating labels need label after |
|
| 86 | + if ( $args['label_type'] == 'floating' && $type != 'checkbox' ) { |
|
| 87 | + $label_after = true; |
|
| 88 | + $args['placeholder'] = ' '; // set the placeholder not empty so the floating label works. |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + // Some special sauce for files |
|
| 92 | + if ( $type == 'file' ) { |
|
| 93 | + $label_after = true; // if type file we need the label after |
|
| 94 | + $args['class'] .= ' custom-file-input '; |
|
| 95 | + } elseif ( $type == 'checkbox' ) { |
|
| 96 | + $label_after = true; // if type file we need the label after |
|
| 97 | + $args['class'] .= ' custom-control-input '; |
|
| 98 | + } elseif ( $type == 'datepicker' || $type == 'timepicker' ) { |
|
| 99 | + $type = 'text'; |
|
| 100 | + //$args['class'] .= ' aui-flatpickr bg-initial '; |
|
| 101 | + $args['class'] .= ' bg-initial '; |
|
| 102 | + |
|
| 103 | + $args['extra_attributes']['data-aui-init'] = 'flatpickr'; |
|
| 104 | + // enqueue the script |
|
| 105 | + $aui_settings = AyeCode_UI_Settings::instance(); |
|
| 106 | + $aui_settings->enqueue_flatpickr(); |
|
| 107 | + } elseif ( $type == 'iconpicker' ) { |
|
| 108 | + $type = 'text'; |
|
| 109 | + //$args['class'] .= ' aui-flatpickr bg-initial '; |
|
| 110 | 110 | // $args['class'] .= ' bg-initial '; |
| 111 | 111 | |
| 112 | - $args['extra_attributes']['data-aui-init'] = 'iconpicker'; |
|
| 113 | - $args['extra_attributes']['data-placement'] = 'bottomRight'; |
|
| 112 | + $args['extra_attributes']['data-aui-init'] = 'iconpicker'; |
|
| 113 | + $args['extra_attributes']['data-placement'] = 'bottomRight'; |
|
| 114 | 114 | |
| 115 | - $args['input_group_right'] = '<span class="input-group-addon input-group-text c-pointer"></span>'; |
|
| 115 | + $args['input_group_right'] = '<span class="input-group-addon input-group-text c-pointer"></span>'; |
|
| 116 | 116 | // $args['input_group_right_inside'] = true; |
| 117 | - // enqueue the script |
|
| 118 | - $aui_settings = AyeCode_UI_Settings::instance(); |
|
| 119 | - $aui_settings->enqueue_iconpicker(); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - if ( $type == 'checkbox' && !empty($args['name'] ) && strpos($args['name'], '[') === false ) { |
|
| 123 | - $output .= '<input type="hidden" name="' . esc_attr( $args['name'] ) . '" value="0" />'; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - |
|
| 127 | - // open/type |
|
| 128 | - $output .= '<input type="' . $type . '" '; |
|
| 129 | - |
|
| 130 | - // name |
|
| 131 | - if ( ! empty( $args['name'] ) ) { |
|
| 132 | - $output .= ' name="' . esc_attr( $args['name'] ) . '" '; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - // id |
|
| 136 | - if ( ! empty( $args['id'] ) ) { |
|
| 137 | - $output .= ' id="' . sanitize_html_class( $args['id'] ) . '" '; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - // placeholder |
|
| 141 | - if ( isset( $args['placeholder'] ) && '' != $args['placeholder'] ) { |
|
| 142 | - $output .= ' placeholder="' . esc_attr( $args['placeholder'] ) . '" '; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - // title |
|
| 146 | - if ( ! empty( $args['title'] ) ) { |
|
| 147 | - $output .= ' title="' . esc_attr( $args['title'] ) . '" '; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - // value |
|
| 151 | - if ( ! empty( $args['value'] ) ) { |
|
| 152 | - $output .= AUI_Component_Helper::value( $args['value'] ); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - // checked, for radio and checkboxes |
|
| 156 | - if ( ( $type == 'checkbox' || $type == 'radio' ) && $args['checked'] ) { |
|
| 157 | - $output .= ' checked '; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - // validation text |
|
| 161 | - if ( ! empty( $args['validation_text'] ) ) { |
|
| 162 | - $output .= ' oninvalid="setCustomValidity(\'' . esc_attr( $args['validation_text'] ) . '\')" '; |
|
| 163 | - $output .= ' onchange="try{setCustomValidity(\'\')}catch(e){}" '; |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - // validation_pattern |
|
| 167 | - if ( ! empty( $args['validation_pattern'] ) ) { |
|
| 168 | - $output .= ' pattern="' . esc_attr( $args['validation_pattern'] ) . '" '; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - // step (for numbers) |
|
| 172 | - if ( ! empty( $args['step'] ) ) { |
|
| 173 | - $output .= ' step="' . $args['step'] . '" '; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - // required |
|
| 177 | - if ( ! empty( $args['required'] ) ) { |
|
| 178 | - $output .= ' required '; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - // class |
|
| 182 | - $class = ! empty( $args['class'] ) ? AUI_Component_Helper::esc_classes( $args['class'] ) : ''; |
|
| 183 | - $output .= ' class="form-control ' . $class . '" '; |
|
| 184 | - |
|
| 185 | - // data-attributes |
|
| 186 | - $output .= AUI_Component_Helper::data_attributes( $args ); |
|
| 187 | - |
|
| 188 | - // extra attributes |
|
| 189 | - if ( ! empty( $args['extra_attributes'] ) ) { |
|
| 190 | - $output .= AUI_Component_Helper::extra_attributes( $args['extra_attributes'] ); |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - // close |
|
| 194 | - $output .= ' >'; |
|
| 195 | - |
|
| 196 | - // help text |
|
| 197 | - if ( ! empty( $args['help_text'] ) ) { |
|
| 198 | - $help_text = AUI_Component_Helper::help_text( $args['help_text'] ); |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - // label |
|
| 202 | - if ( ! empty( $args['label'] ) ) { |
|
| 203 | - $label_base_class = ''; |
|
| 204 | - if ( $type == 'file' ) { |
|
| 205 | - $label_base_class = ' custom-file-label'; |
|
| 206 | - } elseif ( $type == 'checkbox' ) { |
|
| 207 | - if ( ! empty( $args['label_force_left'] ) ) { |
|
| 208 | - $label_args['title'] = wp_kses_post( $args['help_text'] ); |
|
| 209 | - $help_text = ''; |
|
| 210 | - //$label_args['class'] .= ' d-inline '; |
|
| 211 | - $args['wrap_class'] .= ' align-items-center '; |
|
| 212 | - }else{ |
|
| 213 | - |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - $label_base_class = ' custom-control-label'; |
|
| 217 | - } |
|
| 218 | - $label_args['class'] .= $label_base_class; |
|
| 219 | - $temp_label_args = $label_args; |
|
| 220 | - if(! empty( $args['label_force_left'] )){$temp_label_args['class'] = $label_base_class." text-muted";} |
|
| 221 | - $label = self::label( $temp_label_args, $type ); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - |
|
| 225 | - |
|
| 226 | - |
|
| 227 | - // set help text in the correct position |
|
| 228 | - if ( $label_after ) { |
|
| 229 | - $output .= $label . $help_text; |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - // some input types need a separate wrap |
|
| 233 | - if ( $type == 'file' ) { |
|
| 234 | - $output = self::wrap( array( |
|
| 235 | - 'content' => $output, |
|
| 236 | - 'class' => 'form-group custom-file' |
|
| 237 | - ) ); |
|
| 238 | - } elseif ( $type == 'checkbox' ) { |
|
| 239 | - |
|
| 240 | - $label_args['title'] = $args['label']; |
|
| 241 | - $label_col = AUI_Component_Helper::get_column_class( $args['label_col'], 'label' ); |
|
| 242 | - $label = !empty( $args['label_force_left'] ) ? self::label( $label_args, 'cb' ) : '<div class="' . $label_col . ' col-form-label"></div>'; |
|
| 243 | - $switch_size_class = $args['switch'] && !is_bool( $args['switch'] ) ? ' custom-switch-'.esc_attr( $args['switch'] ) : ''; |
|
| 244 | - $wrap_class = $args['switch'] ? 'custom-switch'.$switch_size_class : 'custom-checkbox'; |
|
| 245 | - if ( ! empty( $args['label_force_left'] ) ) { |
|
| 246 | - $wrap_class .= ' d-flex align-content-center'; |
|
| 247 | - $label = str_replace("custom-control-label","", self::label( $label_args, 'cb' ) ); |
|
| 248 | - } |
|
| 249 | - $output = self::wrap( array( |
|
| 250 | - 'content' => $output, |
|
| 251 | - 'class' => 'custom-control ' . $wrap_class |
|
| 252 | - ) ); |
|
| 253 | - |
|
| 254 | - if ( $args['label_type'] == 'horizontal' ) { |
|
| 255 | - $input_col = AUI_Component_Helper::get_column_class( $args['label_col'], 'input' ); |
|
| 256 | - $output = $label . '<div class="' . $input_col . '">' . $output . '</div>'; |
|
| 257 | - } |
|
| 258 | - } elseif ( $type == 'password' && $args['password_toggle'] && ! $args['input_group_right'] ) { |
|
| 259 | - |
|
| 260 | - |
|
| 261 | - // allow password field to toggle view |
|
| 262 | - $args['input_group_right'] = '<span class="input-group-text c-pointer px-3" |
|
| 117 | + // enqueue the script |
|
| 118 | + $aui_settings = AyeCode_UI_Settings::instance(); |
|
| 119 | + $aui_settings->enqueue_iconpicker(); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + if ( $type == 'checkbox' && !empty($args['name'] ) && strpos($args['name'], '[') === false ) { |
|
| 123 | + $output .= '<input type="hidden" name="' . esc_attr( $args['name'] ) . '" value="0" />'; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + |
|
| 127 | + // open/type |
|
| 128 | + $output .= '<input type="' . $type . '" '; |
|
| 129 | + |
|
| 130 | + // name |
|
| 131 | + if ( ! empty( $args['name'] ) ) { |
|
| 132 | + $output .= ' name="' . esc_attr( $args['name'] ) . '" '; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + // id |
|
| 136 | + if ( ! empty( $args['id'] ) ) { |
|
| 137 | + $output .= ' id="' . sanitize_html_class( $args['id'] ) . '" '; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + // placeholder |
|
| 141 | + if ( isset( $args['placeholder'] ) && '' != $args['placeholder'] ) { |
|
| 142 | + $output .= ' placeholder="' . esc_attr( $args['placeholder'] ) . '" '; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + // title |
|
| 146 | + if ( ! empty( $args['title'] ) ) { |
|
| 147 | + $output .= ' title="' . esc_attr( $args['title'] ) . '" '; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + // value |
|
| 151 | + if ( ! empty( $args['value'] ) ) { |
|
| 152 | + $output .= AUI_Component_Helper::value( $args['value'] ); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + // checked, for radio and checkboxes |
|
| 156 | + if ( ( $type == 'checkbox' || $type == 'radio' ) && $args['checked'] ) { |
|
| 157 | + $output .= ' checked '; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + // validation text |
|
| 161 | + if ( ! empty( $args['validation_text'] ) ) { |
|
| 162 | + $output .= ' oninvalid="setCustomValidity(\'' . esc_attr( $args['validation_text'] ) . '\')" '; |
|
| 163 | + $output .= ' onchange="try{setCustomValidity(\'\')}catch(e){}" '; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + // validation_pattern |
|
| 167 | + if ( ! empty( $args['validation_pattern'] ) ) { |
|
| 168 | + $output .= ' pattern="' . esc_attr( $args['validation_pattern'] ) . '" '; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + // step (for numbers) |
|
| 172 | + if ( ! empty( $args['step'] ) ) { |
|
| 173 | + $output .= ' step="' . $args['step'] . '" '; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + // required |
|
| 177 | + if ( ! empty( $args['required'] ) ) { |
|
| 178 | + $output .= ' required '; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + // class |
|
| 182 | + $class = ! empty( $args['class'] ) ? AUI_Component_Helper::esc_classes( $args['class'] ) : ''; |
|
| 183 | + $output .= ' class="form-control ' . $class . '" '; |
|
| 184 | + |
|
| 185 | + // data-attributes |
|
| 186 | + $output .= AUI_Component_Helper::data_attributes( $args ); |
|
| 187 | + |
|
| 188 | + // extra attributes |
|
| 189 | + if ( ! empty( $args['extra_attributes'] ) ) { |
|
| 190 | + $output .= AUI_Component_Helper::extra_attributes( $args['extra_attributes'] ); |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + // close |
|
| 194 | + $output .= ' >'; |
|
| 195 | + |
|
| 196 | + // help text |
|
| 197 | + if ( ! empty( $args['help_text'] ) ) { |
|
| 198 | + $help_text = AUI_Component_Helper::help_text( $args['help_text'] ); |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + // label |
|
| 202 | + if ( ! empty( $args['label'] ) ) { |
|
| 203 | + $label_base_class = ''; |
|
| 204 | + if ( $type == 'file' ) { |
|
| 205 | + $label_base_class = ' custom-file-label'; |
|
| 206 | + } elseif ( $type == 'checkbox' ) { |
|
| 207 | + if ( ! empty( $args['label_force_left'] ) ) { |
|
| 208 | + $label_args['title'] = wp_kses_post( $args['help_text'] ); |
|
| 209 | + $help_text = ''; |
|
| 210 | + //$label_args['class'] .= ' d-inline '; |
|
| 211 | + $args['wrap_class'] .= ' align-items-center '; |
|
| 212 | + }else{ |
|
| 213 | + |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + $label_base_class = ' custom-control-label'; |
|
| 217 | + } |
|
| 218 | + $label_args['class'] .= $label_base_class; |
|
| 219 | + $temp_label_args = $label_args; |
|
| 220 | + if(! empty( $args['label_force_left'] )){$temp_label_args['class'] = $label_base_class." text-muted";} |
|
| 221 | + $label = self::label( $temp_label_args, $type ); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + |
|
| 225 | + |
|
| 226 | + |
|
| 227 | + // set help text in the correct position |
|
| 228 | + if ( $label_after ) { |
|
| 229 | + $output .= $label . $help_text; |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + // some input types need a separate wrap |
|
| 233 | + if ( $type == 'file' ) { |
|
| 234 | + $output = self::wrap( array( |
|
| 235 | + 'content' => $output, |
|
| 236 | + 'class' => 'form-group custom-file' |
|
| 237 | + ) ); |
|
| 238 | + } elseif ( $type == 'checkbox' ) { |
|
| 239 | + |
|
| 240 | + $label_args['title'] = $args['label']; |
|
| 241 | + $label_col = AUI_Component_Helper::get_column_class( $args['label_col'], 'label' ); |
|
| 242 | + $label = !empty( $args['label_force_left'] ) ? self::label( $label_args, 'cb' ) : '<div class="' . $label_col . ' col-form-label"></div>'; |
|
| 243 | + $switch_size_class = $args['switch'] && !is_bool( $args['switch'] ) ? ' custom-switch-'.esc_attr( $args['switch'] ) : ''; |
|
| 244 | + $wrap_class = $args['switch'] ? 'custom-switch'.$switch_size_class : 'custom-checkbox'; |
|
| 245 | + if ( ! empty( $args['label_force_left'] ) ) { |
|
| 246 | + $wrap_class .= ' d-flex align-content-center'; |
|
| 247 | + $label = str_replace("custom-control-label","", self::label( $label_args, 'cb' ) ); |
|
| 248 | + } |
|
| 249 | + $output = self::wrap( array( |
|
| 250 | + 'content' => $output, |
|
| 251 | + 'class' => 'custom-control ' . $wrap_class |
|
| 252 | + ) ); |
|
| 253 | + |
|
| 254 | + if ( $args['label_type'] == 'horizontal' ) { |
|
| 255 | + $input_col = AUI_Component_Helper::get_column_class( $args['label_col'], 'input' ); |
|
| 256 | + $output = $label . '<div class="' . $input_col . '">' . $output . '</div>'; |
|
| 257 | + } |
|
| 258 | + } elseif ( $type == 'password' && $args['password_toggle'] && ! $args['input_group_right'] ) { |
|
| 259 | + |
|
| 260 | + |
|
| 261 | + // allow password field to toggle view |
|
| 262 | + $args['input_group_right'] = '<span class="input-group-text c-pointer px-3" |
|
| 263 | 263 | onclick="var $el = jQuery(this).find(\'i\');$el.toggleClass(\'fa-eye fa-eye-slash\'); |
| 264 | 264 | var $eli = jQuery(this).parent().parent().find(\'input\'); |
| 265 | 265 | if($el.hasClass(\'fa-eye\')) |
| 266 | 266 | {$eli.attr(\'type\',\'text\');} |
| 267 | 267 | else{$eli.attr(\'type\',\'password\');}" |
| 268 | 268 | ><i class="far fa-fw fa-eye-slash"></i></span>'; |
| 269 | - } |
|
| 270 | - |
|
| 271 | - // input group wraps |
|
| 272 | - if ( $args['input_group_left'] || $args['input_group_right'] ) { |
|
| 273 | - $w100 = strpos( $args['class'], 'w-100' ) !== false ? ' w-100' : ''; |
|
| 274 | - if ( $args['input_group_left'] ) { |
|
| 275 | - $output = self::wrap( array( |
|
| 276 | - 'content' => $output, |
|
| 277 | - 'class' => $args['input_group_left_inside'] ? 'input-group-inside position-relative' . $w100 : 'input-group', |
|
| 278 | - 'input_group_left' => $args['input_group_left'], |
|
| 279 | - 'input_group_left_inside' => $args['input_group_left_inside'] |
|
| 280 | - ) ); |
|
| 281 | - } |
|
| 282 | - if ( $args['input_group_right'] ) { |
|
| 283 | - $output = self::wrap( array( |
|
| 284 | - 'content' => $output, |
|
| 285 | - 'class' => $args['input_group_right_inside'] ? 'input-group-inside position-relative' . $w100 : 'input-group', |
|
| 286 | - 'input_group_right' => $args['input_group_right'], |
|
| 287 | - 'input_group_right_inside' => $args['input_group_right_inside'] |
|
| 288 | - ) ); |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - if ( ! $label_after ) { |
|
| 294 | - $output .= $help_text; |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - |
|
| 298 | - if ( $args['label_type'] == 'horizontal' && $type != 'checkbox' ) { |
|
| 299 | - $output = self::wrap( array( |
|
| 300 | - 'content' => $output, |
|
| 301 | - 'class' => AUI_Component_Helper::get_column_class( $args['label_col'], 'input' ) |
|
| 302 | - ) ); |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - if ( ! $label_after ) { |
|
| 306 | - $output = $label . $output; |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - // wrap |
|
| 310 | - if ( ! $args['no_wrap'] ) { |
|
| 311 | - $form_group_class = $args['label_type'] == 'floating' && $type != 'checkbox' ? 'form-label-group' : 'form-group'; |
|
| 312 | - $wrap_class = $args['label_type'] == 'horizontal' ? $form_group_class . ' row' : $form_group_class; |
|
| 313 | - $wrap_class = ! empty( $args['wrap_class'] ) ? $wrap_class . " " . $args['wrap_class'] : $wrap_class; |
|
| 314 | - $output = self::wrap( array( |
|
| 315 | - 'content' => $output, |
|
| 316 | - 'class' => $wrap_class, |
|
| 317 | - 'element_require' => $args['element_require'], |
|
| 318 | - 'argument_id' => $args['id'], |
|
| 319 | - 'wrap_attributes' => $args['wrap_attributes'], |
|
| 320 | - ) ); |
|
| 321 | - } |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - return $output; |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - public static function label( $args = array(), $type = '' ) { |
|
| 328 | - //<label for="exampleInputEmail1">Email address</label> |
|
| 329 | - $defaults = array( |
|
| 330 | - 'title' => 'div', |
|
| 331 | - 'for' => '', |
|
| 332 | - 'class' => '', |
|
| 333 | - 'label_type' => '', // empty = hidden, top, horizontal |
|
| 334 | - 'label_col' => '', |
|
| 335 | - ); |
|
| 336 | - |
|
| 337 | - /** |
|
| 338 | - * Parse incoming $args into an array and merge it with $defaults |
|
| 339 | - */ |
|
| 340 | - $args = wp_parse_args( $args, $defaults ); |
|
| 341 | - $output = ''; |
|
| 342 | - |
|
| 343 | - if ( $args['title'] ) { |
|
| 344 | - |
|
| 345 | - // maybe hide labels //@todo set a global option for visibility class |
|
| 346 | - if ( $type == 'file' || $type == 'checkbox' || $type == 'radio' || ! empty( $args['label_type'] ) ) { |
|
| 347 | - $class = $args['class']; |
|
| 348 | - } else { |
|
| 349 | - $class = 'sr-only ' . $args['class']; |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - // maybe horizontal |
|
| 353 | - if ( $args['label_type'] == 'horizontal' && $type != 'checkbox' ) { |
|
| 354 | - $class .= ' ' . AUI_Component_Helper::get_column_class( $args['label_col'], 'label' ) . ' col-form-label'; |
|
| 355 | - } |
|
| 356 | - |
|
| 357 | - // open |
|
| 358 | - $output .= '<label '; |
|
| 359 | - |
|
| 360 | - // for |
|
| 361 | - if ( ! empty( $args['for'] ) ) { |
|
| 362 | - $output .= ' for="' . esc_attr( $args['for'] ) . '" '; |
|
| 363 | - } |
|
| 364 | - |
|
| 365 | - // class |
|
| 366 | - $class = $class ? AUI_Component_Helper::esc_classes( $class ) : ''; |
|
| 367 | - $output .= ' class="' . $class . '" '; |
|
| 368 | - |
|
| 369 | - // close |
|
| 370 | - $output .= '>'; |
|
| 371 | - |
|
| 372 | - |
|
| 373 | - // title, don't escape fully as can contain html |
|
| 374 | - if ( ! empty( $args['title'] ) ) { |
|
| 375 | - $output .= wp_kses_post( $args['title'] ); |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - // close wrap |
|
| 379 | - $output .= '</label>'; |
|
| 380 | - |
|
| 381 | - |
|
| 382 | - } |
|
| 383 | - |
|
| 384 | - |
|
| 385 | - return $output; |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - /** |
|
| 389 | - * Wrap some content in a HTML wrapper. |
|
| 390 | - * |
|
| 391 | - * @param array $args |
|
| 392 | - * |
|
| 393 | - * @return string |
|
| 394 | - */ |
|
| 395 | - public static function wrap( $args = array() ) { |
|
| 396 | - $defaults = array( |
|
| 397 | - 'type' => 'div', |
|
| 398 | - 'class' => 'form-group', |
|
| 399 | - 'content' => '', |
|
| 400 | - 'input_group_left' => '', |
|
| 401 | - 'input_group_right' => '', |
|
| 402 | - 'input_group_left_inside' => false, |
|
| 403 | - 'input_group_right_inside' => false, |
|
| 404 | - 'element_require' => '', |
|
| 405 | - 'argument_id' => '', |
|
| 406 | - 'wrap_attributes' => array() |
|
| 407 | - ); |
|
| 408 | - |
|
| 409 | - /** |
|
| 410 | - * Parse incoming $args into an array and merge it with $defaults |
|
| 411 | - */ |
|
| 412 | - $args = wp_parse_args( $args, $defaults ); |
|
| 413 | - $output = ''; |
|
| 414 | - if ( $args['type'] ) { |
|
| 415 | - |
|
| 416 | - // open |
|
| 417 | - $output .= '<' . sanitize_html_class( $args['type'] ); |
|
| 418 | - |
|
| 419 | - // element require |
|
| 420 | - if ( ! empty( $args['element_require'] ) ) { |
|
| 421 | - $output .= AUI_Component_Helper::element_require( $args['element_require'] ); |
|
| 422 | - $args['class'] .= " aui-conditional-field"; |
|
| 423 | - } |
|
| 424 | - |
|
| 425 | - // argument_id |
|
| 426 | - if ( ! empty( $args['argument_id'] ) ) { |
|
| 427 | - $output .= ' data-argument="' . esc_attr( $args['argument_id'] ) . '"'; |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - // class |
|
| 431 | - $class = ! empty( $args['class'] ) ? AUI_Component_Helper::esc_classes( $args['class'] ) : ''; |
|
| 432 | - $output .= ' class="' . $class . '" '; |
|
| 433 | - |
|
| 434 | - // Attributes |
|
| 435 | - if ( ! empty( $args['wrap_attributes'] ) ) { |
|
| 436 | - $output .= AUI_Component_Helper::extra_attributes( $args['wrap_attributes'] ); |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - // close wrap |
|
| 440 | - $output .= ' >'; |
|
| 441 | - |
|
| 442 | - |
|
| 443 | - // Input group left |
|
| 444 | - if ( ! empty( $args['input_group_left'] ) ) { |
|
| 445 | - $position_class = ! empty( $args['input_group_left_inside'] ) ? 'position-absolute h-100' : ''; |
|
| 446 | - $input_group_left = strpos( $args['input_group_left'], '<' ) !== false ? $args['input_group_left'] : '<span class="input-group-text">' . $args['input_group_left'] . '</span>'; |
|
| 447 | - $output .= '<div class="input-group-prepend ' . $position_class . '">' . $input_group_left . '</div>'; |
|
| 448 | - } |
|
| 449 | - |
|
| 450 | - // content |
|
| 451 | - $output .= $args['content']; |
|
| 452 | - |
|
| 453 | - // Input group right |
|
| 454 | - if ( ! empty( $args['input_group_right'] ) ) { |
|
| 455 | - $position_class = ! empty( $args['input_group_left_inside'] ) ? 'position-absolute h-100' : ''; |
|
| 456 | - $input_group_right = strpos( $args['input_group_right'], '<' ) !== false ? $args['input_group_right'] : '<span class="input-group-text">' . $args['input_group_right'] . '</span>'; |
|
| 457 | - $output .= '<div class="input-group-append ' . $position_class . '">' . $input_group_right . '</div>'; |
|
| 458 | - } |
|
| 459 | - |
|
| 460 | - |
|
| 461 | - // close wrap |
|
| 462 | - $output .= '</' . sanitize_html_class( $args['type'] ) . '>'; |
|
| 463 | - |
|
| 464 | - |
|
| 465 | - } else { |
|
| 466 | - $output = $args['content']; |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - return $output; |
|
| 470 | - } |
|
| 471 | - |
|
| 472 | - /** |
|
| 473 | - * Build the component. |
|
| 474 | - * |
|
| 475 | - * @param array $args |
|
| 476 | - * |
|
| 477 | - * @return string The rendered component. |
|
| 478 | - */ |
|
| 479 | - public static function textarea( $args = array() ) { |
|
| 480 | - $defaults = array( |
|
| 481 | - 'name' => '', |
|
| 482 | - 'class' => '', |
|
| 483 | - 'wrap_class' => '', |
|
| 484 | - 'id' => '', |
|
| 485 | - 'placeholder' => '', |
|
| 486 | - 'title' => '', |
|
| 487 | - 'value' => '', |
|
| 488 | - 'required' => false, |
|
| 489 | - 'label' => '', |
|
| 490 | - 'label_after' => false, |
|
| 491 | - 'label_class' => '', |
|
| 492 | - 'label_type' => '', |
|
| 493 | - 'label_col' => '', |
|
| 494 | - // sets the label type, default: hidden. Options: hidden, top, horizontal, floating |
|
| 495 | - 'help_text' => '', |
|
| 496 | - 'validation_text' => '', |
|
| 497 | - 'validation_pattern' => '', |
|
| 498 | - 'no_wrap' => false, |
|
| 499 | - 'rows' => '', |
|
| 500 | - 'wysiwyg' => false, |
|
| 501 | - 'allow_tags' => false, |
|
| 502 | - // Allow HTML tags |
|
| 503 | - 'element_require' => '', |
|
| 504 | - // [%element_id%] == "1" |
|
| 505 | - 'extra_attributes' => array(), |
|
| 506 | - // an array of extra attributes |
|
| 507 | - 'wrap_attributes' => array(), |
|
| 508 | - ); |
|
| 509 | - |
|
| 510 | - /** |
|
| 511 | - * Parse incoming $args into an array and merge it with $defaults |
|
| 512 | - */ |
|
| 513 | - $args = wp_parse_args( $args, $defaults ); |
|
| 514 | - $output = ''; |
|
| 515 | - |
|
| 516 | - // hidden label option needs to be empty |
|
| 517 | - $args['label_type'] = $args['label_type'] == 'hidden' ? '' : $args['label_type']; |
|
| 518 | - |
|
| 519 | - // floating labels don't work with wysiwyg so set it as top |
|
| 520 | - if ( $args['label_type'] == 'floating' && ! empty( $args['wysiwyg'] ) ) { |
|
| 521 | - $args['label_type'] = 'top'; |
|
| 522 | - } |
|
| 523 | - |
|
| 524 | - $label_after = $args['label_after']; |
|
| 525 | - |
|
| 526 | - // floating labels need label after |
|
| 527 | - if ( $args['label_type'] == 'floating' && empty( $args['wysiwyg'] ) ) { |
|
| 528 | - $label_after = true; |
|
| 529 | - $args['placeholder'] = ' '; // set the placeholder not empty so the floating label works. |
|
| 530 | - } |
|
| 531 | - |
|
| 532 | - // label |
|
| 533 | - if ( ! empty( $args['label'] ) && is_array( $args['label'] ) ) { |
|
| 534 | - } elseif ( ! empty( $args['label'] ) && ! $label_after ) { |
|
| 535 | - $label_args = array( |
|
| 536 | - 'title' => $args['label'], |
|
| 537 | - 'for' => $args['id'], |
|
| 538 | - 'class' => $args['label_class'] . " ", |
|
| 539 | - 'label_type' => $args['label_type'], |
|
| 540 | - 'label_col' => $args['label_col'] |
|
| 541 | - ); |
|
| 542 | - $output .= self::label( $label_args ); |
|
| 543 | - } |
|
| 544 | - |
|
| 545 | - // maybe horizontal label |
|
| 546 | - if ( $args['label_type'] == 'horizontal' ) { |
|
| 547 | - $input_col = AUI_Component_Helper::get_column_class( $args['label_col'], 'input' ); |
|
| 548 | - $output .= '<div class="' . $input_col . '">'; |
|
| 549 | - } |
|
| 550 | - |
|
| 551 | - if ( ! empty( $args['wysiwyg'] ) ) { |
|
| 552 | - ob_start(); |
|
| 553 | - $content = $args['value']; |
|
| 554 | - $editor_id = ! empty( $args['id'] ) ? sanitize_html_class( $args['id'] ) : 'wp_editor'; |
|
| 555 | - $settings = array( |
|
| 556 | - 'textarea_rows' => ! empty( absint( $args['rows'] ) ) ? absint( $args['rows'] ) : 4, |
|
| 557 | - 'quicktags' => false, |
|
| 558 | - 'media_buttons' => false, |
|
| 559 | - 'editor_class' => 'form-control', |
|
| 560 | - 'textarea_name' => ! empty( $args['name'] ) ? sanitize_html_class( $args['name'] ) : sanitize_html_class( $args['id'] ), |
|
| 561 | - 'teeny' => true, |
|
| 562 | - ); |
|
| 563 | - |
|
| 564 | - // maybe set settings if array |
|
| 565 | - if ( is_array( $args['wysiwyg'] ) ) { |
|
| 566 | - $settings = wp_parse_args( $args['wysiwyg'], $settings ); |
|
| 567 | - } |
|
| 568 | - |
|
| 569 | - wp_editor( $content, $editor_id, $settings ); |
|
| 570 | - $output .= ob_get_clean(); |
|
| 571 | - } else { |
|
| 572 | - |
|
| 573 | - // open |
|
| 574 | - $output .= '<textarea '; |
|
| 575 | - |
|
| 576 | - // name |
|
| 577 | - if ( ! empty( $args['name'] ) ) { |
|
| 578 | - $output .= ' name="' . esc_attr( $args['name'] ) . '" '; |
|
| 579 | - } |
|
| 580 | - |
|
| 581 | - // id |
|
| 582 | - if ( ! empty( $args['id'] ) ) { |
|
| 583 | - $output .= ' id="' . sanitize_html_class( $args['id'] ) . '" '; |
|
| 584 | - } |
|
| 585 | - |
|
| 586 | - // placeholder |
|
| 587 | - if ( isset( $args['placeholder'] ) && '' != $args['placeholder'] ) { |
|
| 588 | - $output .= ' placeholder="' . esc_attr( $args['placeholder'] ) . '" '; |
|
| 589 | - } |
|
| 590 | - |
|
| 591 | - // title |
|
| 592 | - if ( ! empty( $args['title'] ) ) { |
|
| 593 | - $output .= ' title="' . esc_attr( $args['title'] ) . '" '; |
|
| 594 | - } |
|
| 595 | - |
|
| 596 | - // validation text |
|
| 597 | - if ( ! empty( $args['validation_text'] ) ) { |
|
| 598 | - $output .= ' oninvalid="setCustomValidity(\'' . esc_attr( $args['validation_text'] ) . '\')" '; |
|
| 599 | - $output .= ' onchange="try{setCustomValidity(\'\')}catch(e){}" '; |
|
| 600 | - } |
|
| 601 | - |
|
| 602 | - // validation_pattern |
|
| 603 | - if ( ! empty( $args['validation_pattern'] ) ) { |
|
| 604 | - $output .= ' pattern="' . esc_attr( $args['validation_pattern'] ) . '" '; |
|
| 605 | - } |
|
| 606 | - |
|
| 607 | - // required |
|
| 608 | - if ( ! empty( $args['required'] ) ) { |
|
| 609 | - $output .= ' required '; |
|
| 610 | - } |
|
| 611 | - |
|
| 612 | - // rows |
|
| 613 | - if ( ! empty( $args['rows'] ) ) { |
|
| 614 | - $output .= ' rows="' . absint( $args['rows'] ) . '" '; |
|
| 615 | - } |
|
| 616 | - |
|
| 617 | - |
|
| 618 | - // class |
|
| 619 | - $class = ! empty( $args['class'] ) ? $args['class'] : ''; |
|
| 620 | - $output .= ' class="form-control ' . $class . '" '; |
|
| 621 | - |
|
| 622 | - // extra attributes |
|
| 623 | - if ( ! empty( $args['extra_attributes'] ) ) { |
|
| 624 | - $output .= AUI_Component_Helper::extra_attributes( $args['extra_attributes'] ); |
|
| 625 | - } |
|
| 626 | - |
|
| 627 | - // close tag |
|
| 628 | - $output .= ' >'; |
|
| 629 | - |
|
| 630 | - // value |
|
| 631 | - if ( ! empty( $args['value'] ) ) { |
|
| 632 | - if ( ! empty( $args['allow_tags'] ) ) { |
|
| 633 | - $output .= AUI_Component_Helper::sanitize_html_field( $args['value'], $args ); // Sanitize HTML. |
|
| 634 | - } else { |
|
| 635 | - $output .= sanitize_textarea_field( $args['value'] ); |
|
| 636 | - } |
|
| 637 | - } |
|
| 638 | - |
|
| 639 | - // closing tag |
|
| 640 | - $output .= '</textarea>'; |
|
| 641 | - |
|
| 642 | - } |
|
| 643 | - |
|
| 644 | - if ( ! empty( $args['label'] ) && $label_after ) { |
|
| 645 | - $label_args = array( |
|
| 646 | - 'title' => $args['label'], |
|
| 647 | - 'for' => $args['id'], |
|
| 648 | - 'class' => $args['label_class'] . " ", |
|
| 649 | - 'label_type' => $args['label_type'], |
|
| 650 | - 'label_col' => $args['label_col'] |
|
| 651 | - ); |
|
| 652 | - $output .= self::label( $label_args ); |
|
| 653 | - } |
|
| 654 | - |
|
| 655 | - // help text |
|
| 656 | - if ( ! empty( $args['help_text'] ) ) { |
|
| 657 | - $output .= AUI_Component_Helper::help_text( $args['help_text'] ); |
|
| 658 | - } |
|
| 659 | - |
|
| 660 | - // maybe horizontal label |
|
| 661 | - if ( $args['label_type'] == 'horizontal' ) { |
|
| 662 | - $output .= '</div>'; |
|
| 663 | - } |
|
| 664 | - |
|
| 665 | - |
|
| 666 | - // wrap |
|
| 667 | - if ( ! $args['no_wrap'] ) { |
|
| 668 | - $form_group_class = $args['label_type'] == 'floating' ? 'form-label-group' : 'form-group'; |
|
| 669 | - $wrap_class = $args['label_type'] == 'horizontal' ? $form_group_class . ' row' : $form_group_class; |
|
| 670 | - $wrap_class = ! empty( $args['wrap_class'] ) ? $wrap_class . " " . $args['wrap_class'] : $wrap_class; |
|
| 671 | - $output = self::wrap( array( |
|
| 672 | - 'content' => $output, |
|
| 673 | - 'class' => $wrap_class, |
|
| 674 | - 'element_require' => $args['element_require'], |
|
| 675 | - 'argument_id' => $args['id'], |
|
| 676 | - 'wrap_attributes' => $args['wrap_attributes'], |
|
| 677 | - ) ); |
|
| 678 | - } |
|
| 679 | - |
|
| 680 | - |
|
| 681 | - return $output; |
|
| 682 | - } |
|
| 683 | - |
|
| 684 | - /** |
|
| 685 | - * Build the component. |
|
| 686 | - * |
|
| 687 | - * @param array $args |
|
| 688 | - * |
|
| 689 | - * @return string The rendered component. |
|
| 690 | - */ |
|
| 691 | - public static function select( $args = array() ) { |
|
| 692 | - $defaults = array( |
|
| 693 | - 'class' => '', |
|
| 694 | - 'wrap_class' => '', |
|
| 695 | - 'id' => '', |
|
| 696 | - 'title' => '', |
|
| 697 | - 'value' => '', |
|
| 698 | - // can be an array or a string |
|
| 699 | - 'required' => false, |
|
| 700 | - 'label' => '', |
|
| 701 | - 'label_after' => false, |
|
| 702 | - 'label_type' => '', |
|
| 703 | - 'label_col' => '', |
|
| 704 | - // sets the label type, default: hidden. Options: hidden, top, horizontal, floating |
|
| 705 | - 'label_class' => '', |
|
| 706 | - 'help_text' => '', |
|
| 707 | - 'placeholder' => '', |
|
| 708 | - 'options' => array(), |
|
| 709 | - // array or string |
|
| 710 | - 'icon' => '', |
|
| 711 | - 'multiple' => false, |
|
| 712 | - 'select2' => false, |
|
| 713 | - 'no_wrap' => false, |
|
| 714 | - 'input_group_right' => '', |
|
| 715 | - 'input_group_left' => '', |
|
| 716 | - 'input_group_right_inside' => false, // forces the input group inside the input |
|
| 717 | - 'input_group_left_inside' => false, // forces the input group inside the input |
|
| 718 | - 'element_require' => '', |
|
| 719 | - // [%element_id%] == "1" |
|
| 720 | - 'extra_attributes' => array(), |
|
| 721 | - // an array of extra attributes |
|
| 722 | - 'wrap_attributes' => array(), |
|
| 723 | - ); |
|
| 724 | - |
|
| 725 | - /** |
|
| 726 | - * Parse incoming $args into an array and merge it with $defaults |
|
| 727 | - */ |
|
| 728 | - $args = wp_parse_args( $args, $defaults ); |
|
| 729 | - $output = ''; |
|
| 730 | - |
|
| 731 | - // for now lets hide floating labels |
|
| 732 | - if ( $args['label_type'] == 'floating' ) { |
|
| 733 | - $args['label_type'] = 'hidden'; |
|
| 734 | - } |
|
| 735 | - |
|
| 736 | - // hidden label option needs to be empty |
|
| 737 | - $args['label_type'] = $args['label_type'] == 'hidden' ? '' : $args['label_type']; |
|
| 738 | - |
|
| 739 | - |
|
| 740 | - $label_after = $args['label_after']; |
|
| 741 | - |
|
| 742 | - // floating labels need label after |
|
| 743 | - if ( $args['label_type'] == 'floating' ) { |
|
| 744 | - $label_after = true; |
|
| 745 | - $args['placeholder'] = ' '; // set the placeholder not empty so the floating label works. |
|
| 746 | - } |
|
| 747 | - |
|
| 748 | - // Maybe setup select2 |
|
| 749 | - $is_select2 = false; |
|
| 750 | - if ( ! empty( $args['select2'] ) ) { |
|
| 751 | - $args['class'] .= ' aui-select2'; |
|
| 752 | - $is_select2 = true; |
|
| 753 | - } elseif ( strpos( $args['class'], 'aui-select2' ) !== false ) { |
|
| 754 | - $is_select2 = true; |
|
| 755 | - } |
|
| 756 | - |
|
| 757 | - // select2 tags |
|
| 758 | - if ( ! empty( $args['select2'] ) && $args['select2'] === 'tags' ) { // triple equals needed here for some reason |
|
| 759 | - $args['data-tags'] = 'true'; |
|
| 760 | - $args['data-token-separators'] = "[',']"; |
|
| 761 | - $args['multiple'] = true; |
|
| 762 | - } |
|
| 763 | - |
|
| 764 | - // select2 placeholder |
|
| 765 | - if ( $is_select2 && isset( $args['placeholder'] ) && '' != $args['placeholder'] && empty( $args['data-placeholder'] ) ) { |
|
| 766 | - $args['data-placeholder'] = esc_attr( $args['placeholder'] ); |
|
| 767 | - $args['data-allow-clear'] = isset( $args['data-allow-clear'] ) ? (bool) $args['data-allow-clear'] : true; |
|
| 768 | - } |
|
| 769 | - |
|
| 770 | - |
|
| 771 | - |
|
| 772 | - // maybe horizontal label |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + // input group wraps |
|
| 272 | + if ( $args['input_group_left'] || $args['input_group_right'] ) { |
|
| 273 | + $w100 = strpos( $args['class'], 'w-100' ) !== false ? ' w-100' : ''; |
|
| 274 | + if ( $args['input_group_left'] ) { |
|
| 275 | + $output = self::wrap( array( |
|
| 276 | + 'content' => $output, |
|
| 277 | + 'class' => $args['input_group_left_inside'] ? 'input-group-inside position-relative' . $w100 : 'input-group', |
|
| 278 | + 'input_group_left' => $args['input_group_left'], |
|
| 279 | + 'input_group_left_inside' => $args['input_group_left_inside'] |
|
| 280 | + ) ); |
|
| 281 | + } |
|
| 282 | + if ( $args['input_group_right'] ) { |
|
| 283 | + $output = self::wrap( array( |
|
| 284 | + 'content' => $output, |
|
| 285 | + 'class' => $args['input_group_right_inside'] ? 'input-group-inside position-relative' . $w100 : 'input-group', |
|
| 286 | + 'input_group_right' => $args['input_group_right'], |
|
| 287 | + 'input_group_right_inside' => $args['input_group_right_inside'] |
|
| 288 | + ) ); |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + if ( ! $label_after ) { |
|
| 294 | + $output .= $help_text; |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + |
|
| 298 | + if ( $args['label_type'] == 'horizontal' && $type != 'checkbox' ) { |
|
| 299 | + $output = self::wrap( array( |
|
| 300 | + 'content' => $output, |
|
| 301 | + 'class' => AUI_Component_Helper::get_column_class( $args['label_col'], 'input' ) |
|
| 302 | + ) ); |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + if ( ! $label_after ) { |
|
| 306 | + $output = $label . $output; |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + // wrap |
|
| 310 | + if ( ! $args['no_wrap'] ) { |
|
| 311 | + $form_group_class = $args['label_type'] == 'floating' && $type != 'checkbox' ? 'form-label-group' : 'form-group'; |
|
| 312 | + $wrap_class = $args['label_type'] == 'horizontal' ? $form_group_class . ' row' : $form_group_class; |
|
| 313 | + $wrap_class = ! empty( $args['wrap_class'] ) ? $wrap_class . " " . $args['wrap_class'] : $wrap_class; |
|
| 314 | + $output = self::wrap( array( |
|
| 315 | + 'content' => $output, |
|
| 316 | + 'class' => $wrap_class, |
|
| 317 | + 'element_require' => $args['element_require'], |
|
| 318 | + 'argument_id' => $args['id'], |
|
| 319 | + 'wrap_attributes' => $args['wrap_attributes'], |
|
| 320 | + ) ); |
|
| 321 | + } |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + return $output; |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + public static function label( $args = array(), $type = '' ) { |
|
| 328 | + //<label for="exampleInputEmail1">Email address</label> |
|
| 329 | + $defaults = array( |
|
| 330 | + 'title' => 'div', |
|
| 331 | + 'for' => '', |
|
| 332 | + 'class' => '', |
|
| 333 | + 'label_type' => '', // empty = hidden, top, horizontal |
|
| 334 | + 'label_col' => '', |
|
| 335 | + ); |
|
| 336 | + |
|
| 337 | + /** |
|
| 338 | + * Parse incoming $args into an array and merge it with $defaults |
|
| 339 | + */ |
|
| 340 | + $args = wp_parse_args( $args, $defaults ); |
|
| 341 | + $output = ''; |
|
| 342 | + |
|
| 343 | + if ( $args['title'] ) { |
|
| 344 | + |
|
| 345 | + // maybe hide labels //@todo set a global option for visibility class |
|
| 346 | + if ( $type == 'file' || $type == 'checkbox' || $type == 'radio' || ! empty( $args['label_type'] ) ) { |
|
| 347 | + $class = $args['class']; |
|
| 348 | + } else { |
|
| 349 | + $class = 'sr-only ' . $args['class']; |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + // maybe horizontal |
|
| 353 | + if ( $args['label_type'] == 'horizontal' && $type != 'checkbox' ) { |
|
| 354 | + $class .= ' ' . AUI_Component_Helper::get_column_class( $args['label_col'], 'label' ) . ' col-form-label'; |
|
| 355 | + } |
|
| 356 | + |
|
| 357 | + // open |
|
| 358 | + $output .= '<label '; |
|
| 359 | + |
|
| 360 | + // for |
|
| 361 | + if ( ! empty( $args['for'] ) ) { |
|
| 362 | + $output .= ' for="' . esc_attr( $args['for'] ) . '" '; |
|
| 363 | + } |
|
| 364 | + |
|
| 365 | + // class |
|
| 366 | + $class = $class ? AUI_Component_Helper::esc_classes( $class ) : ''; |
|
| 367 | + $output .= ' class="' . $class . '" '; |
|
| 368 | + |
|
| 369 | + // close |
|
| 370 | + $output .= '>'; |
|
| 371 | + |
|
| 372 | + |
|
| 373 | + // title, don't escape fully as can contain html |
|
| 374 | + if ( ! empty( $args['title'] ) ) { |
|
| 375 | + $output .= wp_kses_post( $args['title'] ); |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + // close wrap |
|
| 379 | + $output .= '</label>'; |
|
| 380 | + |
|
| 381 | + |
|
| 382 | + } |
|
| 383 | + |
|
| 384 | + |
|
| 385 | + return $output; |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + /** |
|
| 389 | + * Wrap some content in a HTML wrapper. |
|
| 390 | + * |
|
| 391 | + * @param array $args |
|
| 392 | + * |
|
| 393 | + * @return string |
|
| 394 | + */ |
|
| 395 | + public static function wrap( $args = array() ) { |
|
| 396 | + $defaults = array( |
|
| 397 | + 'type' => 'div', |
|
| 398 | + 'class' => 'form-group', |
|
| 399 | + 'content' => '', |
|
| 400 | + 'input_group_left' => '', |
|
| 401 | + 'input_group_right' => '', |
|
| 402 | + 'input_group_left_inside' => false, |
|
| 403 | + 'input_group_right_inside' => false, |
|
| 404 | + 'element_require' => '', |
|
| 405 | + 'argument_id' => '', |
|
| 406 | + 'wrap_attributes' => array() |
|
| 407 | + ); |
|
| 408 | + |
|
| 409 | + /** |
|
| 410 | + * Parse incoming $args into an array and merge it with $defaults |
|
| 411 | + */ |
|
| 412 | + $args = wp_parse_args( $args, $defaults ); |
|
| 413 | + $output = ''; |
|
| 414 | + if ( $args['type'] ) { |
|
| 415 | + |
|
| 416 | + // open |
|
| 417 | + $output .= '<' . sanitize_html_class( $args['type'] ); |
|
| 418 | + |
|
| 419 | + // element require |
|
| 420 | + if ( ! empty( $args['element_require'] ) ) { |
|
| 421 | + $output .= AUI_Component_Helper::element_require( $args['element_require'] ); |
|
| 422 | + $args['class'] .= " aui-conditional-field"; |
|
| 423 | + } |
|
| 424 | + |
|
| 425 | + // argument_id |
|
| 426 | + if ( ! empty( $args['argument_id'] ) ) { |
|
| 427 | + $output .= ' data-argument="' . esc_attr( $args['argument_id'] ) . '"'; |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + // class |
|
| 431 | + $class = ! empty( $args['class'] ) ? AUI_Component_Helper::esc_classes( $args['class'] ) : ''; |
|
| 432 | + $output .= ' class="' . $class . '" '; |
|
| 433 | + |
|
| 434 | + // Attributes |
|
| 435 | + if ( ! empty( $args['wrap_attributes'] ) ) { |
|
| 436 | + $output .= AUI_Component_Helper::extra_attributes( $args['wrap_attributes'] ); |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + // close wrap |
|
| 440 | + $output .= ' >'; |
|
| 441 | + |
|
| 442 | + |
|
| 443 | + // Input group left |
|
| 444 | + if ( ! empty( $args['input_group_left'] ) ) { |
|
| 445 | + $position_class = ! empty( $args['input_group_left_inside'] ) ? 'position-absolute h-100' : ''; |
|
| 446 | + $input_group_left = strpos( $args['input_group_left'], '<' ) !== false ? $args['input_group_left'] : '<span class="input-group-text">' . $args['input_group_left'] . '</span>'; |
|
| 447 | + $output .= '<div class="input-group-prepend ' . $position_class . '">' . $input_group_left . '</div>'; |
|
| 448 | + } |
|
| 449 | + |
|
| 450 | + // content |
|
| 451 | + $output .= $args['content']; |
|
| 452 | + |
|
| 453 | + // Input group right |
|
| 454 | + if ( ! empty( $args['input_group_right'] ) ) { |
|
| 455 | + $position_class = ! empty( $args['input_group_left_inside'] ) ? 'position-absolute h-100' : ''; |
|
| 456 | + $input_group_right = strpos( $args['input_group_right'], '<' ) !== false ? $args['input_group_right'] : '<span class="input-group-text">' . $args['input_group_right'] . '</span>'; |
|
| 457 | + $output .= '<div class="input-group-append ' . $position_class . '">' . $input_group_right . '</div>'; |
|
| 458 | + } |
|
| 459 | + |
|
| 460 | + |
|
| 461 | + // close wrap |
|
| 462 | + $output .= '</' . sanitize_html_class( $args['type'] ) . '>'; |
|
| 463 | + |
|
| 464 | + |
|
| 465 | + } else { |
|
| 466 | + $output = $args['content']; |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + return $output; |
|
| 470 | + } |
|
| 471 | + |
|
| 472 | + /** |
|
| 473 | + * Build the component. |
|
| 474 | + * |
|
| 475 | + * @param array $args |
|
| 476 | + * |
|
| 477 | + * @return string The rendered component. |
|
| 478 | + */ |
|
| 479 | + public static function textarea( $args = array() ) { |
|
| 480 | + $defaults = array( |
|
| 481 | + 'name' => '', |
|
| 482 | + 'class' => '', |
|
| 483 | + 'wrap_class' => '', |
|
| 484 | + 'id' => '', |
|
| 485 | + 'placeholder' => '', |
|
| 486 | + 'title' => '', |
|
| 487 | + 'value' => '', |
|
| 488 | + 'required' => false, |
|
| 489 | + 'label' => '', |
|
| 490 | + 'label_after' => false, |
|
| 491 | + 'label_class' => '', |
|
| 492 | + 'label_type' => '', |
|
| 493 | + 'label_col' => '', |
|
| 494 | + // sets the label type, default: hidden. Options: hidden, top, horizontal, floating |
|
| 495 | + 'help_text' => '', |
|
| 496 | + 'validation_text' => '', |
|
| 497 | + 'validation_pattern' => '', |
|
| 498 | + 'no_wrap' => false, |
|
| 499 | + 'rows' => '', |
|
| 500 | + 'wysiwyg' => false, |
|
| 501 | + 'allow_tags' => false, |
|
| 502 | + // Allow HTML tags |
|
| 503 | + 'element_require' => '', |
|
| 504 | + // [%element_id%] == "1" |
|
| 505 | + 'extra_attributes' => array(), |
|
| 506 | + // an array of extra attributes |
|
| 507 | + 'wrap_attributes' => array(), |
|
| 508 | + ); |
|
| 509 | + |
|
| 510 | + /** |
|
| 511 | + * Parse incoming $args into an array and merge it with $defaults |
|
| 512 | + */ |
|
| 513 | + $args = wp_parse_args( $args, $defaults ); |
|
| 514 | + $output = ''; |
|
| 515 | + |
|
| 516 | + // hidden label option needs to be empty |
|
| 517 | + $args['label_type'] = $args['label_type'] == 'hidden' ? '' : $args['label_type']; |
|
| 518 | + |
|
| 519 | + // floating labels don't work with wysiwyg so set it as top |
|
| 520 | + if ( $args['label_type'] == 'floating' && ! empty( $args['wysiwyg'] ) ) { |
|
| 521 | + $args['label_type'] = 'top'; |
|
| 522 | + } |
|
| 523 | + |
|
| 524 | + $label_after = $args['label_after']; |
|
| 525 | + |
|
| 526 | + // floating labels need label after |
|
| 527 | + if ( $args['label_type'] == 'floating' && empty( $args['wysiwyg'] ) ) { |
|
| 528 | + $label_after = true; |
|
| 529 | + $args['placeholder'] = ' '; // set the placeholder not empty so the floating label works. |
|
| 530 | + } |
|
| 531 | + |
|
| 532 | + // label |
|
| 533 | + if ( ! empty( $args['label'] ) && is_array( $args['label'] ) ) { |
|
| 534 | + } elseif ( ! empty( $args['label'] ) && ! $label_after ) { |
|
| 535 | + $label_args = array( |
|
| 536 | + 'title' => $args['label'], |
|
| 537 | + 'for' => $args['id'], |
|
| 538 | + 'class' => $args['label_class'] . " ", |
|
| 539 | + 'label_type' => $args['label_type'], |
|
| 540 | + 'label_col' => $args['label_col'] |
|
| 541 | + ); |
|
| 542 | + $output .= self::label( $label_args ); |
|
| 543 | + } |
|
| 544 | + |
|
| 545 | + // maybe horizontal label |
|
| 546 | + if ( $args['label_type'] == 'horizontal' ) { |
|
| 547 | + $input_col = AUI_Component_Helper::get_column_class( $args['label_col'], 'input' ); |
|
| 548 | + $output .= '<div class="' . $input_col . '">'; |
|
| 549 | + } |
|
| 550 | + |
|
| 551 | + if ( ! empty( $args['wysiwyg'] ) ) { |
|
| 552 | + ob_start(); |
|
| 553 | + $content = $args['value']; |
|
| 554 | + $editor_id = ! empty( $args['id'] ) ? sanitize_html_class( $args['id'] ) : 'wp_editor'; |
|
| 555 | + $settings = array( |
|
| 556 | + 'textarea_rows' => ! empty( absint( $args['rows'] ) ) ? absint( $args['rows'] ) : 4, |
|
| 557 | + 'quicktags' => false, |
|
| 558 | + 'media_buttons' => false, |
|
| 559 | + 'editor_class' => 'form-control', |
|
| 560 | + 'textarea_name' => ! empty( $args['name'] ) ? sanitize_html_class( $args['name'] ) : sanitize_html_class( $args['id'] ), |
|
| 561 | + 'teeny' => true, |
|
| 562 | + ); |
|
| 563 | + |
|
| 564 | + // maybe set settings if array |
|
| 565 | + if ( is_array( $args['wysiwyg'] ) ) { |
|
| 566 | + $settings = wp_parse_args( $args['wysiwyg'], $settings ); |
|
| 567 | + } |
|
| 568 | + |
|
| 569 | + wp_editor( $content, $editor_id, $settings ); |
|
| 570 | + $output .= ob_get_clean(); |
|
| 571 | + } else { |
|
| 572 | + |
|
| 573 | + // open |
|
| 574 | + $output .= '<textarea '; |
|
| 575 | + |
|
| 576 | + // name |
|
| 577 | + if ( ! empty( $args['name'] ) ) { |
|
| 578 | + $output .= ' name="' . esc_attr( $args['name'] ) . '" '; |
|
| 579 | + } |
|
| 580 | + |
|
| 581 | + // id |
|
| 582 | + if ( ! empty( $args['id'] ) ) { |
|
| 583 | + $output .= ' id="' . sanitize_html_class( $args['id'] ) . '" '; |
|
| 584 | + } |
|
| 585 | + |
|
| 586 | + // placeholder |
|
| 587 | + if ( isset( $args['placeholder'] ) && '' != $args['placeholder'] ) { |
|
| 588 | + $output .= ' placeholder="' . esc_attr( $args['placeholder'] ) . '" '; |
|
| 589 | + } |
|
| 590 | + |
|
| 591 | + // title |
|
| 592 | + if ( ! empty( $args['title'] ) ) { |
|
| 593 | + $output .= ' title="' . esc_attr( $args['title'] ) . '" '; |
|
| 594 | + } |
|
| 595 | + |
|
| 596 | + // validation text |
|
| 597 | + if ( ! empty( $args['validation_text'] ) ) { |
|
| 598 | + $output .= ' oninvalid="setCustomValidity(\'' . esc_attr( $args['validation_text'] ) . '\')" '; |
|
| 599 | + $output .= ' onchange="try{setCustomValidity(\'\')}catch(e){}" '; |
|
| 600 | + } |
|
| 601 | + |
|
| 602 | + // validation_pattern |
|
| 603 | + if ( ! empty( $args['validation_pattern'] ) ) { |
|
| 604 | + $output .= ' pattern="' . esc_attr( $args['validation_pattern'] ) . '" '; |
|
| 605 | + } |
|
| 606 | + |
|
| 607 | + // required |
|
| 608 | + if ( ! empty( $args['required'] ) ) { |
|
| 609 | + $output .= ' required '; |
|
| 610 | + } |
|
| 611 | + |
|
| 612 | + // rows |
|
| 613 | + if ( ! empty( $args['rows'] ) ) { |
|
| 614 | + $output .= ' rows="' . absint( $args['rows'] ) . '" '; |
|
| 615 | + } |
|
| 616 | + |
|
| 617 | + |
|
| 618 | + // class |
|
| 619 | + $class = ! empty( $args['class'] ) ? $args['class'] : ''; |
|
| 620 | + $output .= ' class="form-control ' . $class . '" '; |
|
| 621 | + |
|
| 622 | + // extra attributes |
|
| 623 | + if ( ! empty( $args['extra_attributes'] ) ) { |
|
| 624 | + $output .= AUI_Component_Helper::extra_attributes( $args['extra_attributes'] ); |
|
| 625 | + } |
|
| 626 | + |
|
| 627 | + // close tag |
|
| 628 | + $output .= ' >'; |
|
| 629 | + |
|
| 630 | + // value |
|
| 631 | + if ( ! empty( $args['value'] ) ) { |
|
| 632 | + if ( ! empty( $args['allow_tags'] ) ) { |
|
| 633 | + $output .= AUI_Component_Helper::sanitize_html_field( $args['value'], $args ); // Sanitize HTML. |
|
| 634 | + } else { |
|
| 635 | + $output .= sanitize_textarea_field( $args['value'] ); |
|
| 636 | + } |
|
| 637 | + } |
|
| 638 | + |
|
| 639 | + // closing tag |
|
| 640 | + $output .= '</textarea>'; |
|
| 641 | + |
|
| 642 | + } |
|
| 643 | + |
|
| 644 | + if ( ! empty( $args['label'] ) && $label_after ) { |
|
| 645 | + $label_args = array( |
|
| 646 | + 'title' => $args['label'], |
|
| 647 | + 'for' => $args['id'], |
|
| 648 | + 'class' => $args['label_class'] . " ", |
|
| 649 | + 'label_type' => $args['label_type'], |
|
| 650 | + 'label_col' => $args['label_col'] |
|
| 651 | + ); |
|
| 652 | + $output .= self::label( $label_args ); |
|
| 653 | + } |
|
| 654 | + |
|
| 655 | + // help text |
|
| 656 | + if ( ! empty( $args['help_text'] ) ) { |
|
| 657 | + $output .= AUI_Component_Helper::help_text( $args['help_text'] ); |
|
| 658 | + } |
|
| 659 | + |
|
| 660 | + // maybe horizontal label |
|
| 661 | + if ( $args['label_type'] == 'horizontal' ) { |
|
| 662 | + $output .= '</div>'; |
|
| 663 | + } |
|
| 664 | + |
|
| 665 | + |
|
| 666 | + // wrap |
|
| 667 | + if ( ! $args['no_wrap'] ) { |
|
| 668 | + $form_group_class = $args['label_type'] == 'floating' ? 'form-label-group' : 'form-group'; |
|
| 669 | + $wrap_class = $args['label_type'] == 'horizontal' ? $form_group_class . ' row' : $form_group_class; |
|
| 670 | + $wrap_class = ! empty( $args['wrap_class'] ) ? $wrap_class . " " . $args['wrap_class'] : $wrap_class; |
|
| 671 | + $output = self::wrap( array( |
|
| 672 | + 'content' => $output, |
|
| 673 | + 'class' => $wrap_class, |
|
| 674 | + 'element_require' => $args['element_require'], |
|
| 675 | + 'argument_id' => $args['id'], |
|
| 676 | + 'wrap_attributes' => $args['wrap_attributes'], |
|
| 677 | + ) ); |
|
| 678 | + } |
|
| 679 | + |
|
| 680 | + |
|
| 681 | + return $output; |
|
| 682 | + } |
|
| 683 | + |
|
| 684 | + /** |
|
| 685 | + * Build the component. |
|
| 686 | + * |
|
| 687 | + * @param array $args |
|
| 688 | + * |
|
| 689 | + * @return string The rendered component. |
|
| 690 | + */ |
|
| 691 | + public static function select( $args = array() ) { |
|
| 692 | + $defaults = array( |
|
| 693 | + 'class' => '', |
|
| 694 | + 'wrap_class' => '', |
|
| 695 | + 'id' => '', |
|
| 696 | + 'title' => '', |
|
| 697 | + 'value' => '', |
|
| 698 | + // can be an array or a string |
|
| 699 | + 'required' => false, |
|
| 700 | + 'label' => '', |
|
| 701 | + 'label_after' => false, |
|
| 702 | + 'label_type' => '', |
|
| 703 | + 'label_col' => '', |
|
| 704 | + // sets the label type, default: hidden. Options: hidden, top, horizontal, floating |
|
| 705 | + 'label_class' => '', |
|
| 706 | + 'help_text' => '', |
|
| 707 | + 'placeholder' => '', |
|
| 708 | + 'options' => array(), |
|
| 709 | + // array or string |
|
| 710 | + 'icon' => '', |
|
| 711 | + 'multiple' => false, |
|
| 712 | + 'select2' => false, |
|
| 713 | + 'no_wrap' => false, |
|
| 714 | + 'input_group_right' => '', |
|
| 715 | + 'input_group_left' => '', |
|
| 716 | + 'input_group_right_inside' => false, // forces the input group inside the input |
|
| 717 | + 'input_group_left_inside' => false, // forces the input group inside the input |
|
| 718 | + 'element_require' => '', |
|
| 719 | + // [%element_id%] == "1" |
|
| 720 | + 'extra_attributes' => array(), |
|
| 721 | + // an array of extra attributes |
|
| 722 | + 'wrap_attributes' => array(), |
|
| 723 | + ); |
|
| 724 | + |
|
| 725 | + /** |
|
| 726 | + * Parse incoming $args into an array and merge it with $defaults |
|
| 727 | + */ |
|
| 728 | + $args = wp_parse_args( $args, $defaults ); |
|
| 729 | + $output = ''; |
|
| 730 | + |
|
| 731 | + // for now lets hide floating labels |
|
| 732 | + if ( $args['label_type'] == 'floating' ) { |
|
| 733 | + $args['label_type'] = 'hidden'; |
|
| 734 | + } |
|
| 735 | + |
|
| 736 | + // hidden label option needs to be empty |
|
| 737 | + $args['label_type'] = $args['label_type'] == 'hidden' ? '' : $args['label_type']; |
|
| 738 | + |
|
| 739 | + |
|
| 740 | + $label_after = $args['label_after']; |
|
| 741 | + |
|
| 742 | + // floating labels need label after |
|
| 743 | + if ( $args['label_type'] == 'floating' ) { |
|
| 744 | + $label_after = true; |
|
| 745 | + $args['placeholder'] = ' '; // set the placeholder not empty so the floating label works. |
|
| 746 | + } |
|
| 747 | + |
|
| 748 | + // Maybe setup select2 |
|
| 749 | + $is_select2 = false; |
|
| 750 | + if ( ! empty( $args['select2'] ) ) { |
|
| 751 | + $args['class'] .= ' aui-select2'; |
|
| 752 | + $is_select2 = true; |
|
| 753 | + } elseif ( strpos( $args['class'], 'aui-select2' ) !== false ) { |
|
| 754 | + $is_select2 = true; |
|
| 755 | + } |
|
| 756 | + |
|
| 757 | + // select2 tags |
|
| 758 | + if ( ! empty( $args['select2'] ) && $args['select2'] === 'tags' ) { // triple equals needed here for some reason |
|
| 759 | + $args['data-tags'] = 'true'; |
|
| 760 | + $args['data-token-separators'] = "[',']"; |
|
| 761 | + $args['multiple'] = true; |
|
| 762 | + } |
|
| 763 | + |
|
| 764 | + // select2 placeholder |
|
| 765 | + if ( $is_select2 && isset( $args['placeholder'] ) && '' != $args['placeholder'] && empty( $args['data-placeholder'] ) ) { |
|
| 766 | + $args['data-placeholder'] = esc_attr( $args['placeholder'] ); |
|
| 767 | + $args['data-allow-clear'] = isset( $args['data-allow-clear'] ) ? (bool) $args['data-allow-clear'] : true; |
|
| 768 | + } |
|
| 769 | + |
|
| 770 | + |
|
| 771 | + |
|
| 772 | + // maybe horizontal label |
|
| 773 | 773 | // if ( $args['label_type'] == 'horizontal' ) { |
| 774 | 774 | // $input_col = AUI_Component_Helper::get_column_class( $args['label_col'], 'input' ); |
| 775 | 775 | // $output .= '<div class="' . $input_col . '">'; |
| 776 | 776 | // } |
| 777 | 777 | |
| 778 | - // Set hidden input to save empty value for multiselect. |
|
| 779 | - if ( ! empty( $args['multiple'] ) && ! empty( $args['name'] ) ) { |
|
| 780 | - $output .= '<input type="hidden" ' . AUI_Component_Helper::name( $args['name'] ) . ' value=""/>'; |
|
| 781 | - } |
|
| 782 | - |
|
| 783 | - // open/type |
|
| 784 | - $output .= '<select '; |
|
| 785 | - |
|
| 786 | - // style |
|
| 787 | - if ( $is_select2 && !($args['input_group_left'] || $args['input_group_right'])) { |
|
| 788 | - $output .= " style='width:100%;' "; |
|
| 789 | - } |
|
| 790 | - |
|
| 791 | - // element require |
|
| 792 | - if ( ! empty( $args['element_require'] ) ) { |
|
| 793 | - $output .= AUI_Component_Helper::element_require( $args['element_require'] ); |
|
| 794 | - $args['class'] .= " aui-conditional-field"; |
|
| 795 | - } |
|
| 796 | - |
|
| 797 | - // class |
|
| 798 | - $class = ! empty( $args['class'] ) ? $args['class'] : ''; |
|
| 799 | - $output .= AUI_Component_Helper::class_attr( 'custom-select ' . $class ); |
|
| 800 | - |
|
| 801 | - // name |
|
| 802 | - if ( ! empty( $args['name'] ) ) { |
|
| 803 | - $output .= AUI_Component_Helper::name( $args['name'], $args['multiple'] ); |
|
| 804 | - } |
|
| 805 | - |
|
| 806 | - // id |
|
| 807 | - if ( ! empty( $args['id'] ) ) { |
|
| 808 | - $output .= AUI_Component_Helper::id( $args['id'] ); |
|
| 809 | - } |
|
| 810 | - |
|
| 811 | - // title |
|
| 812 | - if ( ! empty( $args['title'] ) ) { |
|
| 813 | - $output .= AUI_Component_Helper::title( $args['title'] ); |
|
| 814 | - } |
|
| 815 | - |
|
| 816 | - // data-attributes |
|
| 817 | - $output .= AUI_Component_Helper::data_attributes( $args ); |
|
| 818 | - |
|
| 819 | - // aria-attributes |
|
| 820 | - $output .= AUI_Component_Helper::aria_attributes( $args ); |
|
| 821 | - |
|
| 822 | - // extra attributes |
|
| 823 | - if ( ! empty( $args['extra_attributes'] ) ) { |
|
| 824 | - $output .= AUI_Component_Helper::extra_attributes( $args['extra_attributes'] ); |
|
| 825 | - } |
|
| 826 | - |
|
| 827 | - // required |
|
| 828 | - if ( ! empty( $args['required'] ) ) { |
|
| 829 | - $output .= ' required '; |
|
| 830 | - } |
|
| 831 | - |
|
| 832 | - // multiple |
|
| 833 | - if ( ! empty( $args['multiple'] ) ) { |
|
| 834 | - $output .= ' multiple '; |
|
| 835 | - } |
|
| 836 | - |
|
| 837 | - // close opening tag |
|
| 838 | - $output .= ' >'; |
|
| 839 | - |
|
| 840 | - // placeholder |
|
| 841 | - if ( isset( $args['placeholder'] ) && '' != $args['placeholder'] && ! $is_select2 ) { |
|
| 842 | - $output .= '<option value="" disabled selected hidden>' . esc_attr( $args['placeholder'] ) . '</option>'; |
|
| 843 | - } elseif ( $is_select2 && ! empty( $args['placeholder'] ) ) { |
|
| 844 | - $output .= "<option></option>"; // select2 needs an empty select to fill the placeholder |
|
| 845 | - } |
|
| 846 | - |
|
| 847 | - // Options |
|
| 848 | - if ( ! empty( $args['options'] ) ) { |
|
| 849 | - |
|
| 850 | - if ( ! is_array( $args['options'] ) ) { |
|
| 851 | - $output .= $args['options']; // not the preferred way but an option |
|
| 852 | - } else { |
|
| 853 | - foreach ( $args['options'] as $val => $name ) { |
|
| 854 | - $selected = ''; |
|
| 855 | - if ( is_array( $name ) ) { |
|
| 856 | - if ( isset( $name['optgroup'] ) && ( $name['optgroup'] == 'start' || $name['optgroup'] == 'end' ) ) { |
|
| 857 | - $option_label = isset( $name['label'] ) ? $name['label'] : ''; |
|
| 858 | - |
|
| 859 | - $output .= $name['optgroup'] == 'start' ? '<optgroup label="' . esc_attr( $option_label ) . '">' : '</optgroup>'; |
|
| 860 | - } else { |
|
| 861 | - $option_label = isset( $name['label'] ) ? $name['label'] : ''; |
|
| 862 | - $option_value = isset( $name['value'] ) ? $name['value'] : ''; |
|
| 863 | - $extra_attributes = !empty($name['extra_attributes']) ? AUI_Component_Helper::extra_attributes( $name['extra_attributes'] ) : ''; |
|
| 864 | - if ( ! empty( $args['multiple'] ) && ! empty( $args['value'] ) && is_array( $args['value'] ) ) { |
|
| 865 | - $selected = in_array( $option_value, stripslashes_deep( $args['value'] ) ) ? "selected" : ""; |
|
| 866 | - } elseif ( ! empty( $args['value'] ) ) { |
|
| 867 | - $selected = selected( $option_value, stripslashes_deep( $args['value'] ), false ); |
|
| 868 | - } elseif ( empty( $args['value'] ) && $args['value'] === $option_value ) { |
|
| 869 | - $selected = selected( $option_value, $args['value'], false ); |
|
| 870 | - } |
|
| 871 | - |
|
| 872 | - $output .= '<option value="' . esc_attr( $option_value ) . '" ' . $selected . ' '.$extra_attributes .'>' . $option_label . '</option>'; |
|
| 873 | - } |
|
| 874 | - } else { |
|
| 875 | - if ( ! empty( $args['value'] ) ) { |
|
| 876 | - if ( is_array( $args['value'] ) ) { |
|
| 877 | - $selected = in_array( $val, $args['value'] ) ? 'selected="selected"' : ''; |
|
| 878 | - } elseif ( ! empty( $args['value'] ) ) { |
|
| 879 | - $selected = selected( $args['value'], $val, false ); |
|
| 880 | - } |
|
| 881 | - } elseif ( $args['value'] === $val ) { |
|
| 882 | - $selected = selected( $args['value'], $val, false ); |
|
| 883 | - } |
|
| 884 | - $output .= '<option value="' . esc_attr( $val ) . '" ' . $selected . '>' . esc_attr( $name ) . '</option>'; |
|
| 885 | - } |
|
| 886 | - } |
|
| 887 | - } |
|
| 888 | - |
|
| 889 | - } |
|
| 890 | - |
|
| 891 | - // closing tag |
|
| 892 | - $output .= '</select>'; |
|
| 893 | - |
|
| 894 | - $label = ''; |
|
| 895 | - $help_text = ''; |
|
| 896 | - // label |
|
| 897 | - if ( ! empty( $args['label'] ) && is_array( $args['label'] ) ) { |
|
| 898 | - } elseif ( ! empty( $args['label'] ) && ! $label_after ) { |
|
| 899 | - $label_args = array( |
|
| 900 | - 'title' => $args['label'], |
|
| 901 | - 'for' => $args['id'], |
|
| 902 | - 'class' => $args['label_class'] . " ", |
|
| 903 | - 'label_type' => $args['label_type'], |
|
| 904 | - 'label_col' => $args['label_col'] |
|
| 905 | - ); |
|
| 906 | - $label = self::label( $label_args ); |
|
| 907 | - } |
|
| 908 | - |
|
| 909 | - // help text |
|
| 910 | - if ( ! empty( $args['help_text'] ) ) { |
|
| 911 | - $help_text = AUI_Component_Helper::help_text( $args['help_text'] ); |
|
| 912 | - } |
|
| 913 | - |
|
| 914 | - // input group wraps |
|
| 915 | - if ( $args['input_group_left'] || $args['input_group_right'] ) { |
|
| 916 | - $w100 = strpos( $args['class'], 'w-100' ) !== false ? ' w-100' : ''; |
|
| 917 | - if ( $args['input_group_left'] ) { |
|
| 918 | - $output = self::wrap( array( |
|
| 919 | - 'content' => $output, |
|
| 920 | - 'class' => $args['input_group_left_inside'] ? 'input-group-inside position-relative' . $w100 : 'input-group', |
|
| 921 | - 'input_group_left' => $args['input_group_left'], |
|
| 922 | - 'input_group_left_inside' => $args['input_group_left_inside'] |
|
| 923 | - ) ); |
|
| 924 | - } |
|
| 925 | - if ( $args['input_group_right'] ) { |
|
| 926 | - $output = self::wrap( array( |
|
| 927 | - 'content' => $output, |
|
| 928 | - 'class' => $args['input_group_right_inside'] ? 'input-group-inside position-relative' . $w100 : 'input-group', |
|
| 929 | - 'input_group_right' => $args['input_group_right'], |
|
| 930 | - 'input_group_right_inside' => $args['input_group_right_inside'] |
|
| 931 | - ) ); |
|
| 932 | - } |
|
| 933 | - |
|
| 934 | - } |
|
| 935 | - |
|
| 936 | - if ( ! $label_after ) { |
|
| 937 | - $output .= $help_text; |
|
| 938 | - } |
|
| 939 | - |
|
| 940 | - |
|
| 941 | - if ( $args['label_type'] == 'horizontal' ) { |
|
| 942 | - $output = self::wrap( array( |
|
| 943 | - 'content' => $output, |
|
| 944 | - 'class' => AUI_Component_Helper::get_column_class( $args['label_col'], 'input' ) |
|
| 945 | - ) ); |
|
| 946 | - } |
|
| 947 | - |
|
| 948 | - if ( ! $label_after ) { |
|
| 949 | - $output = $label . $output; |
|
| 950 | - } |
|
| 951 | - |
|
| 952 | - // maybe horizontal label |
|
| 778 | + // Set hidden input to save empty value for multiselect. |
|
| 779 | + if ( ! empty( $args['multiple'] ) && ! empty( $args['name'] ) ) { |
|
| 780 | + $output .= '<input type="hidden" ' . AUI_Component_Helper::name( $args['name'] ) . ' value=""/>'; |
|
| 781 | + } |
|
| 782 | + |
|
| 783 | + // open/type |
|
| 784 | + $output .= '<select '; |
|
| 785 | + |
|
| 786 | + // style |
|
| 787 | + if ( $is_select2 && !($args['input_group_left'] || $args['input_group_right'])) { |
|
| 788 | + $output .= " style='width:100%;' "; |
|
| 789 | + } |
|
| 790 | + |
|
| 791 | + // element require |
|
| 792 | + if ( ! empty( $args['element_require'] ) ) { |
|
| 793 | + $output .= AUI_Component_Helper::element_require( $args['element_require'] ); |
|
| 794 | + $args['class'] .= " aui-conditional-field"; |
|
| 795 | + } |
|
| 796 | + |
|
| 797 | + // class |
|
| 798 | + $class = ! empty( $args['class'] ) ? $args['class'] : ''; |
|
| 799 | + $output .= AUI_Component_Helper::class_attr( 'custom-select ' . $class ); |
|
| 800 | + |
|
| 801 | + // name |
|
| 802 | + if ( ! empty( $args['name'] ) ) { |
|
| 803 | + $output .= AUI_Component_Helper::name( $args['name'], $args['multiple'] ); |
|
| 804 | + } |
|
| 805 | + |
|
| 806 | + // id |
|
| 807 | + if ( ! empty( $args['id'] ) ) { |
|
| 808 | + $output .= AUI_Component_Helper::id( $args['id'] ); |
|
| 809 | + } |
|
| 810 | + |
|
| 811 | + // title |
|
| 812 | + if ( ! empty( $args['title'] ) ) { |
|
| 813 | + $output .= AUI_Component_Helper::title( $args['title'] ); |
|
| 814 | + } |
|
| 815 | + |
|
| 816 | + // data-attributes |
|
| 817 | + $output .= AUI_Component_Helper::data_attributes( $args ); |
|
| 818 | + |
|
| 819 | + // aria-attributes |
|
| 820 | + $output .= AUI_Component_Helper::aria_attributes( $args ); |
|
| 821 | + |
|
| 822 | + // extra attributes |
|
| 823 | + if ( ! empty( $args['extra_attributes'] ) ) { |
|
| 824 | + $output .= AUI_Component_Helper::extra_attributes( $args['extra_attributes'] ); |
|
| 825 | + } |
|
| 826 | + |
|
| 827 | + // required |
|
| 828 | + if ( ! empty( $args['required'] ) ) { |
|
| 829 | + $output .= ' required '; |
|
| 830 | + } |
|
| 831 | + |
|
| 832 | + // multiple |
|
| 833 | + if ( ! empty( $args['multiple'] ) ) { |
|
| 834 | + $output .= ' multiple '; |
|
| 835 | + } |
|
| 836 | + |
|
| 837 | + // close opening tag |
|
| 838 | + $output .= ' >'; |
|
| 839 | + |
|
| 840 | + // placeholder |
|
| 841 | + if ( isset( $args['placeholder'] ) && '' != $args['placeholder'] && ! $is_select2 ) { |
|
| 842 | + $output .= '<option value="" disabled selected hidden>' . esc_attr( $args['placeholder'] ) . '</option>'; |
|
| 843 | + } elseif ( $is_select2 && ! empty( $args['placeholder'] ) ) { |
|
| 844 | + $output .= "<option></option>"; // select2 needs an empty select to fill the placeholder |
|
| 845 | + } |
|
| 846 | + |
|
| 847 | + // Options |
|
| 848 | + if ( ! empty( $args['options'] ) ) { |
|
| 849 | + |
|
| 850 | + if ( ! is_array( $args['options'] ) ) { |
|
| 851 | + $output .= $args['options']; // not the preferred way but an option |
|
| 852 | + } else { |
|
| 853 | + foreach ( $args['options'] as $val => $name ) { |
|
| 854 | + $selected = ''; |
|
| 855 | + if ( is_array( $name ) ) { |
|
| 856 | + if ( isset( $name['optgroup'] ) && ( $name['optgroup'] == 'start' || $name['optgroup'] == 'end' ) ) { |
|
| 857 | + $option_label = isset( $name['label'] ) ? $name['label'] : ''; |
|
| 858 | + |
|
| 859 | + $output .= $name['optgroup'] == 'start' ? '<optgroup label="' . esc_attr( $option_label ) . '">' : '</optgroup>'; |
|
| 860 | + } else { |
|
| 861 | + $option_label = isset( $name['label'] ) ? $name['label'] : ''; |
|
| 862 | + $option_value = isset( $name['value'] ) ? $name['value'] : ''; |
|
| 863 | + $extra_attributes = !empty($name['extra_attributes']) ? AUI_Component_Helper::extra_attributes( $name['extra_attributes'] ) : ''; |
|
| 864 | + if ( ! empty( $args['multiple'] ) && ! empty( $args['value'] ) && is_array( $args['value'] ) ) { |
|
| 865 | + $selected = in_array( $option_value, stripslashes_deep( $args['value'] ) ) ? "selected" : ""; |
|
| 866 | + } elseif ( ! empty( $args['value'] ) ) { |
|
| 867 | + $selected = selected( $option_value, stripslashes_deep( $args['value'] ), false ); |
|
| 868 | + } elseif ( empty( $args['value'] ) && $args['value'] === $option_value ) { |
|
| 869 | + $selected = selected( $option_value, $args['value'], false ); |
|
| 870 | + } |
|
| 871 | + |
|
| 872 | + $output .= '<option value="' . esc_attr( $option_value ) . '" ' . $selected . ' '.$extra_attributes .'>' . $option_label . '</option>'; |
|
| 873 | + } |
|
| 874 | + } else { |
|
| 875 | + if ( ! empty( $args['value'] ) ) { |
|
| 876 | + if ( is_array( $args['value'] ) ) { |
|
| 877 | + $selected = in_array( $val, $args['value'] ) ? 'selected="selected"' : ''; |
|
| 878 | + } elseif ( ! empty( $args['value'] ) ) { |
|
| 879 | + $selected = selected( $args['value'], $val, false ); |
|
| 880 | + } |
|
| 881 | + } elseif ( $args['value'] === $val ) { |
|
| 882 | + $selected = selected( $args['value'], $val, false ); |
|
| 883 | + } |
|
| 884 | + $output .= '<option value="' . esc_attr( $val ) . '" ' . $selected . '>' . esc_attr( $name ) . '</option>'; |
|
| 885 | + } |
|
| 886 | + } |
|
| 887 | + } |
|
| 888 | + |
|
| 889 | + } |
|
| 890 | + |
|
| 891 | + // closing tag |
|
| 892 | + $output .= '</select>'; |
|
| 893 | + |
|
| 894 | + $label = ''; |
|
| 895 | + $help_text = ''; |
|
| 896 | + // label |
|
| 897 | + if ( ! empty( $args['label'] ) && is_array( $args['label'] ) ) { |
|
| 898 | + } elseif ( ! empty( $args['label'] ) && ! $label_after ) { |
|
| 899 | + $label_args = array( |
|
| 900 | + 'title' => $args['label'], |
|
| 901 | + 'for' => $args['id'], |
|
| 902 | + 'class' => $args['label_class'] . " ", |
|
| 903 | + 'label_type' => $args['label_type'], |
|
| 904 | + 'label_col' => $args['label_col'] |
|
| 905 | + ); |
|
| 906 | + $label = self::label( $label_args ); |
|
| 907 | + } |
|
| 908 | + |
|
| 909 | + // help text |
|
| 910 | + if ( ! empty( $args['help_text'] ) ) { |
|
| 911 | + $help_text = AUI_Component_Helper::help_text( $args['help_text'] ); |
|
| 912 | + } |
|
| 913 | + |
|
| 914 | + // input group wraps |
|
| 915 | + if ( $args['input_group_left'] || $args['input_group_right'] ) { |
|
| 916 | + $w100 = strpos( $args['class'], 'w-100' ) !== false ? ' w-100' : ''; |
|
| 917 | + if ( $args['input_group_left'] ) { |
|
| 918 | + $output = self::wrap( array( |
|
| 919 | + 'content' => $output, |
|
| 920 | + 'class' => $args['input_group_left_inside'] ? 'input-group-inside position-relative' . $w100 : 'input-group', |
|
| 921 | + 'input_group_left' => $args['input_group_left'], |
|
| 922 | + 'input_group_left_inside' => $args['input_group_left_inside'] |
|
| 923 | + ) ); |
|
| 924 | + } |
|
| 925 | + if ( $args['input_group_right'] ) { |
|
| 926 | + $output = self::wrap( array( |
|
| 927 | + 'content' => $output, |
|
| 928 | + 'class' => $args['input_group_right_inside'] ? 'input-group-inside position-relative' . $w100 : 'input-group', |
|
| 929 | + 'input_group_right' => $args['input_group_right'], |
|
| 930 | + 'input_group_right_inside' => $args['input_group_right_inside'] |
|
| 931 | + ) ); |
|
| 932 | + } |
|
| 933 | + |
|
| 934 | + } |
|
| 935 | + |
|
| 936 | + if ( ! $label_after ) { |
|
| 937 | + $output .= $help_text; |
|
| 938 | + } |
|
| 939 | + |
|
| 940 | + |
|
| 941 | + if ( $args['label_type'] == 'horizontal' ) { |
|
| 942 | + $output = self::wrap( array( |
|
| 943 | + 'content' => $output, |
|
| 944 | + 'class' => AUI_Component_Helper::get_column_class( $args['label_col'], 'input' ) |
|
| 945 | + ) ); |
|
| 946 | + } |
|
| 947 | + |
|
| 948 | + if ( ! $label_after ) { |
|
| 949 | + $output = $label . $output; |
|
| 950 | + } |
|
| 951 | + |
|
| 952 | + // maybe horizontal label |
|
| 953 | 953 | // if ( $args['label_type'] == 'horizontal' ) { |
| 954 | 954 | // $output .= '</div>'; |
| 955 | 955 | // } |
| 956 | 956 | |
| 957 | 957 | |
| 958 | - // wrap |
|
| 959 | - if ( ! $args['no_wrap'] ) { |
|
| 960 | - $wrap_class = $args['label_type'] == 'horizontal' ? 'form-group row' : 'form-group'; |
|
| 961 | - $wrap_class = ! empty( $args['wrap_class'] ) ? $wrap_class . " " . $args['wrap_class'] : $wrap_class; |
|
| 962 | - $output = self::wrap( array( |
|
| 963 | - 'content' => $output, |
|
| 964 | - 'class' => $wrap_class, |
|
| 965 | - 'element_require' => $args['element_require'], |
|
| 966 | - 'argument_id' => $args['id'], |
|
| 967 | - 'wrap_attributes' => $args['wrap_attributes'], |
|
| 968 | - ) ); |
|
| 969 | - } |
|
| 970 | - |
|
| 971 | - |
|
| 972 | - return $output; |
|
| 973 | - } |
|
| 974 | - |
|
| 975 | - /** |
|
| 976 | - * Build the component. |
|
| 977 | - * |
|
| 978 | - * @param array $args |
|
| 979 | - * |
|
| 980 | - * @return string The rendered component. |
|
| 981 | - */ |
|
| 982 | - public static function radio( $args = array() ) { |
|
| 983 | - $defaults = array( |
|
| 984 | - 'class' => '', |
|
| 985 | - 'wrap_class' => '', |
|
| 986 | - 'id' => '', |
|
| 987 | - 'title' => '', |
|
| 988 | - 'horizontal' => false, |
|
| 989 | - // sets the lable horizontal |
|
| 990 | - 'value' => '', |
|
| 991 | - 'label' => '', |
|
| 992 | - 'label_class' => '', |
|
| 993 | - 'label_type' => '', |
|
| 994 | - 'label_col' => '', |
|
| 995 | - // sets the label type, default: hidden. Options: hidden, top, horizontal, floating |
|
| 996 | - 'help_text' => '', |
|
| 997 | - 'inline' => true, |
|
| 998 | - 'required' => false, |
|
| 999 | - 'options' => array(), |
|
| 1000 | - 'icon' => '', |
|
| 1001 | - 'no_wrap' => false, |
|
| 1002 | - 'element_require' => '', |
|
| 1003 | - // [%element_id%] == "1" |
|
| 1004 | - 'extra_attributes' => array(), |
|
| 1005 | - // an array of extra attributes |
|
| 1006 | - 'wrap_attributes' => array() |
|
| 1007 | - ); |
|
| 1008 | - |
|
| 1009 | - /** |
|
| 1010 | - * Parse incoming $args into an array and merge it with $defaults |
|
| 1011 | - */ |
|
| 1012 | - $args = wp_parse_args( $args, $defaults ); |
|
| 1013 | - |
|
| 1014 | - // for now lets use horizontal for floating |
|
| 1015 | - if ( $args['label_type'] == 'floating' ) { |
|
| 1016 | - $args['label_type'] = 'horizontal'; |
|
| 1017 | - } |
|
| 1018 | - |
|
| 1019 | - $label_args = array( |
|
| 1020 | - 'title' => $args['label'], |
|
| 1021 | - 'class' => $args['label_class'] . " pt-0 ", |
|
| 1022 | - 'label_type' => $args['label_type'], |
|
| 1023 | - 'label_col' => $args['label_col'] |
|
| 1024 | - ); |
|
| 1025 | - |
|
| 1026 | - $output = ''; |
|
| 1027 | - |
|
| 1028 | - |
|
| 1029 | - // label before |
|
| 1030 | - if ( ! empty( $args['label'] ) ) { |
|
| 1031 | - $output .= self::label( $label_args, 'radio' ); |
|
| 1032 | - } |
|
| 1033 | - |
|
| 1034 | - // maybe horizontal label |
|
| 1035 | - if ( $args['label_type'] == 'horizontal' ) { |
|
| 1036 | - $input_col = AUI_Component_Helper::get_column_class( $args['label_col'], 'input' ); |
|
| 1037 | - $output .= '<div class="' . $input_col . '">'; |
|
| 1038 | - } |
|
| 1039 | - |
|
| 1040 | - if ( ! empty( $args['options'] ) ) { |
|
| 1041 | - $count = 0; |
|
| 1042 | - foreach ( $args['options'] as $value => $label ) { |
|
| 1043 | - $option_args = $args; |
|
| 1044 | - $option_args['value'] = $value; |
|
| 1045 | - $option_args['label'] = $label; |
|
| 1046 | - $option_args['checked'] = $value == $args['value'] ? true : false; |
|
| 1047 | - $output .= self::radio_option( $option_args, $count ); |
|
| 1048 | - $count ++; |
|
| 1049 | - } |
|
| 1050 | - } |
|
| 1051 | - |
|
| 1052 | - // help text |
|
| 1053 | - $help_text = ! empty( $args['help_text'] ) ? AUI_Component_Helper::help_text( $args['help_text'] ) : ''; |
|
| 1054 | - $output .= $help_text; |
|
| 1055 | - |
|
| 1056 | - // maybe horizontal label |
|
| 1057 | - if ( $args['label_type'] == 'horizontal' ) { |
|
| 1058 | - $output .= '</div>'; |
|
| 1059 | - } |
|
| 1060 | - |
|
| 1061 | - // wrap |
|
| 1062 | - $wrap_class = $args['label_type'] == 'horizontal' ? 'form-group row' : 'form-group'; |
|
| 1063 | - $wrap_class = ! empty( $args['wrap_class'] ) ? $wrap_class . " " . $args['wrap_class'] : $wrap_class; |
|
| 1064 | - $output = self::wrap( array( |
|
| 1065 | - 'content' => $output, |
|
| 1066 | - 'class' => $wrap_class, |
|
| 1067 | - 'element_require' => $args['element_require'], |
|
| 1068 | - 'argument_id' => $args['id'], |
|
| 1069 | - 'wrap_attributes' => $args['wrap_attributes'], |
|
| 1070 | - ) ); |
|
| 1071 | - |
|
| 1072 | - |
|
| 1073 | - return $output; |
|
| 1074 | - } |
|
| 1075 | - |
|
| 1076 | - /** |
|
| 1077 | - * Build the component. |
|
| 1078 | - * |
|
| 1079 | - * @param array $args |
|
| 1080 | - * |
|
| 1081 | - * @return string The rendered component. |
|
| 1082 | - */ |
|
| 1083 | - public static function radio_option( $args = array(), $count = '' ) { |
|
| 1084 | - $defaults = array( |
|
| 1085 | - 'class' => '', |
|
| 1086 | - 'id' => '', |
|
| 1087 | - 'title' => '', |
|
| 1088 | - 'value' => '', |
|
| 1089 | - 'required' => false, |
|
| 1090 | - 'inline' => true, |
|
| 1091 | - 'label' => '', |
|
| 1092 | - 'options' => array(), |
|
| 1093 | - 'icon' => '', |
|
| 1094 | - 'no_wrap' => false, |
|
| 1095 | - 'extra_attributes' => array() // an array of extra attributes |
|
| 1096 | - ); |
|
| 1097 | - |
|
| 1098 | - /** |
|
| 1099 | - * Parse incoming $args into an array and merge it with $defaults |
|
| 1100 | - */ |
|
| 1101 | - $args = wp_parse_args( $args, $defaults ); |
|
| 1102 | - |
|
| 1103 | - $output = ''; |
|
| 1104 | - |
|
| 1105 | - // open/type |
|
| 1106 | - $output .= '<input type="radio"'; |
|
| 1107 | - |
|
| 1108 | - // class |
|
| 1109 | - $output .= ' class="form-check-input" '; |
|
| 1110 | - |
|
| 1111 | - // name |
|
| 1112 | - if ( ! empty( $args['name'] ) ) { |
|
| 1113 | - $output .= AUI_Component_Helper::name( $args['name'] ); |
|
| 1114 | - } |
|
| 1115 | - |
|
| 1116 | - // id |
|
| 1117 | - if ( ! empty( $args['id'] ) ) { |
|
| 1118 | - $output .= AUI_Component_Helper::id( $args['id'] . $count ); |
|
| 1119 | - } |
|
| 1120 | - |
|
| 1121 | - // title |
|
| 1122 | - if ( ! empty( $args['title'] ) ) { |
|
| 1123 | - $output .= AUI_Component_Helper::title( $args['title'] ); |
|
| 1124 | - } |
|
| 1125 | - |
|
| 1126 | - // value |
|
| 1127 | - if ( isset( $args['value'] ) ) { |
|
| 1128 | - $output .= AUI_Component_Helper::value( $args['value'] ); |
|
| 1129 | - } |
|
| 1130 | - |
|
| 1131 | - // checked, for radio and checkboxes |
|
| 1132 | - if ( $args['checked'] ) { |
|
| 1133 | - $output .= ' checked '; |
|
| 1134 | - } |
|
| 1135 | - |
|
| 1136 | - // data-attributes |
|
| 1137 | - $output .= AUI_Component_Helper::data_attributes( $args ); |
|
| 1138 | - |
|
| 1139 | - // aria-attributes |
|
| 1140 | - $output .= AUI_Component_Helper::aria_attributes( $args ); |
|
| 1141 | - |
|
| 1142 | - // extra attributes |
|
| 1143 | - if ( ! empty( $args['extra_attributes'] ) ) { |
|
| 1144 | - $output .= AUI_Component_Helper::extra_attributes( $args['extra_attributes'] ); |
|
| 1145 | - } |
|
| 1146 | - |
|
| 1147 | - // required |
|
| 1148 | - if ( ! empty( $args['required'] ) ) { |
|
| 1149 | - $output .= ' required '; |
|
| 1150 | - } |
|
| 1151 | - |
|
| 1152 | - // close opening tag |
|
| 1153 | - $output .= ' >'; |
|
| 1154 | - |
|
| 1155 | - // label |
|
| 1156 | - if ( ! empty( $args['label'] ) && is_array( $args['label'] ) ) { |
|
| 1157 | - } elseif ( ! empty( $args['label'] ) ) { |
|
| 1158 | - $output .= self::label( array( |
|
| 1159 | - 'title' => $args['label'], |
|
| 1160 | - 'for' => $args['id'] . $count, |
|
| 1161 | - 'class' => 'form-check-label' |
|
| 1162 | - ), 'radio' ); |
|
| 1163 | - } |
|
| 1164 | - |
|
| 1165 | - // wrap |
|
| 1166 | - if ( ! $args['no_wrap'] ) { |
|
| 1167 | - $wrap_class = $args['inline'] ? 'form-check form-check-inline' : 'form-check'; |
|
| 1168 | - |
|
| 1169 | - // Unique wrap class |
|
| 1170 | - $uniq_class = 'fwrap'; |
|
| 1171 | - if ( ! empty( $args['name'] ) ) { |
|
| 1172 | - $uniq_class .= '-' . $args['name']; |
|
| 1173 | - } else if ( ! empty( $args['id'] ) ) { |
|
| 1174 | - $uniq_class .= '-' . $args['id']; |
|
| 1175 | - } |
|
| 1176 | - |
|
| 1177 | - if ( isset( $args['value'] ) || $args['value'] !== "" ) { |
|
| 1178 | - $uniq_class .= '-' . $args['value']; |
|
| 1179 | - } else { |
|
| 1180 | - $uniq_class .= '-' . $count; |
|
| 1181 | - } |
|
| 1182 | - $wrap_class .= ' ' . sanitize_html_class( $uniq_class ); |
|
| 1183 | - |
|
| 1184 | - $output = self::wrap( array( |
|
| 1185 | - 'content' => $output, |
|
| 1186 | - 'class' => $wrap_class |
|
| 1187 | - ) ); |
|
| 1188 | - } |
|
| 1189 | - |
|
| 1190 | - return $output; |
|
| 1191 | - } |
|
| 958 | + // wrap |
|
| 959 | + if ( ! $args['no_wrap'] ) { |
|
| 960 | + $wrap_class = $args['label_type'] == 'horizontal' ? 'form-group row' : 'form-group'; |
|
| 961 | + $wrap_class = ! empty( $args['wrap_class'] ) ? $wrap_class . " " . $args['wrap_class'] : $wrap_class; |
|
| 962 | + $output = self::wrap( array( |
|
| 963 | + 'content' => $output, |
|
| 964 | + 'class' => $wrap_class, |
|
| 965 | + 'element_require' => $args['element_require'], |
|
| 966 | + 'argument_id' => $args['id'], |
|
| 967 | + 'wrap_attributes' => $args['wrap_attributes'], |
|
| 968 | + ) ); |
|
| 969 | + } |
|
| 970 | + |
|
| 971 | + |
|
| 972 | + return $output; |
|
| 973 | + } |
|
| 974 | + |
|
| 975 | + /** |
|
| 976 | + * Build the component. |
|
| 977 | + * |
|
| 978 | + * @param array $args |
|
| 979 | + * |
|
| 980 | + * @return string The rendered component. |
|
| 981 | + */ |
|
| 982 | + public static function radio( $args = array() ) { |
|
| 983 | + $defaults = array( |
|
| 984 | + 'class' => '', |
|
| 985 | + 'wrap_class' => '', |
|
| 986 | + 'id' => '', |
|
| 987 | + 'title' => '', |
|
| 988 | + 'horizontal' => false, |
|
| 989 | + // sets the lable horizontal |
|
| 990 | + 'value' => '', |
|
| 991 | + 'label' => '', |
|
| 992 | + 'label_class' => '', |
|
| 993 | + 'label_type' => '', |
|
| 994 | + 'label_col' => '', |
|
| 995 | + // sets the label type, default: hidden. Options: hidden, top, horizontal, floating |
|
| 996 | + 'help_text' => '', |
|
| 997 | + 'inline' => true, |
|
| 998 | + 'required' => false, |
|
| 999 | + 'options' => array(), |
|
| 1000 | + 'icon' => '', |
|
| 1001 | + 'no_wrap' => false, |
|
| 1002 | + 'element_require' => '', |
|
| 1003 | + // [%element_id%] == "1" |
|
| 1004 | + 'extra_attributes' => array(), |
|
| 1005 | + // an array of extra attributes |
|
| 1006 | + 'wrap_attributes' => array() |
|
| 1007 | + ); |
|
| 1008 | + |
|
| 1009 | + /** |
|
| 1010 | + * Parse incoming $args into an array and merge it with $defaults |
|
| 1011 | + */ |
|
| 1012 | + $args = wp_parse_args( $args, $defaults ); |
|
| 1013 | + |
|
| 1014 | + // for now lets use horizontal for floating |
|
| 1015 | + if ( $args['label_type'] == 'floating' ) { |
|
| 1016 | + $args['label_type'] = 'horizontal'; |
|
| 1017 | + } |
|
| 1018 | + |
|
| 1019 | + $label_args = array( |
|
| 1020 | + 'title' => $args['label'], |
|
| 1021 | + 'class' => $args['label_class'] . " pt-0 ", |
|
| 1022 | + 'label_type' => $args['label_type'], |
|
| 1023 | + 'label_col' => $args['label_col'] |
|
| 1024 | + ); |
|
| 1025 | + |
|
| 1026 | + $output = ''; |
|
| 1027 | + |
|
| 1028 | + |
|
| 1029 | + // label before |
|
| 1030 | + if ( ! empty( $args['label'] ) ) { |
|
| 1031 | + $output .= self::label( $label_args, 'radio' ); |
|
| 1032 | + } |
|
| 1033 | + |
|
| 1034 | + // maybe horizontal label |
|
| 1035 | + if ( $args['label_type'] == 'horizontal' ) { |
|
| 1036 | + $input_col = AUI_Component_Helper::get_column_class( $args['label_col'], 'input' ); |
|
| 1037 | + $output .= '<div class="' . $input_col . '">'; |
|
| 1038 | + } |
|
| 1039 | + |
|
| 1040 | + if ( ! empty( $args['options'] ) ) { |
|
| 1041 | + $count = 0; |
|
| 1042 | + foreach ( $args['options'] as $value => $label ) { |
|
| 1043 | + $option_args = $args; |
|
| 1044 | + $option_args['value'] = $value; |
|
| 1045 | + $option_args['label'] = $label; |
|
| 1046 | + $option_args['checked'] = $value == $args['value'] ? true : false; |
|
| 1047 | + $output .= self::radio_option( $option_args, $count ); |
|
| 1048 | + $count ++; |
|
| 1049 | + } |
|
| 1050 | + } |
|
| 1051 | + |
|
| 1052 | + // help text |
|
| 1053 | + $help_text = ! empty( $args['help_text'] ) ? AUI_Component_Helper::help_text( $args['help_text'] ) : ''; |
|
| 1054 | + $output .= $help_text; |
|
| 1055 | + |
|
| 1056 | + // maybe horizontal label |
|
| 1057 | + if ( $args['label_type'] == 'horizontal' ) { |
|
| 1058 | + $output .= '</div>'; |
|
| 1059 | + } |
|
| 1060 | + |
|
| 1061 | + // wrap |
|
| 1062 | + $wrap_class = $args['label_type'] == 'horizontal' ? 'form-group row' : 'form-group'; |
|
| 1063 | + $wrap_class = ! empty( $args['wrap_class'] ) ? $wrap_class . " " . $args['wrap_class'] : $wrap_class; |
|
| 1064 | + $output = self::wrap( array( |
|
| 1065 | + 'content' => $output, |
|
| 1066 | + 'class' => $wrap_class, |
|
| 1067 | + 'element_require' => $args['element_require'], |
|
| 1068 | + 'argument_id' => $args['id'], |
|
| 1069 | + 'wrap_attributes' => $args['wrap_attributes'], |
|
| 1070 | + ) ); |
|
| 1071 | + |
|
| 1072 | + |
|
| 1073 | + return $output; |
|
| 1074 | + } |
|
| 1075 | + |
|
| 1076 | + /** |
|
| 1077 | + * Build the component. |
|
| 1078 | + * |
|
| 1079 | + * @param array $args |
|
| 1080 | + * |
|
| 1081 | + * @return string The rendered component. |
|
| 1082 | + */ |
|
| 1083 | + public static function radio_option( $args = array(), $count = '' ) { |
|
| 1084 | + $defaults = array( |
|
| 1085 | + 'class' => '', |
|
| 1086 | + 'id' => '', |
|
| 1087 | + 'title' => '', |
|
| 1088 | + 'value' => '', |
|
| 1089 | + 'required' => false, |
|
| 1090 | + 'inline' => true, |
|
| 1091 | + 'label' => '', |
|
| 1092 | + 'options' => array(), |
|
| 1093 | + 'icon' => '', |
|
| 1094 | + 'no_wrap' => false, |
|
| 1095 | + 'extra_attributes' => array() // an array of extra attributes |
|
| 1096 | + ); |
|
| 1097 | + |
|
| 1098 | + /** |
|
| 1099 | + * Parse incoming $args into an array and merge it with $defaults |
|
| 1100 | + */ |
|
| 1101 | + $args = wp_parse_args( $args, $defaults ); |
|
| 1102 | + |
|
| 1103 | + $output = ''; |
|
| 1104 | + |
|
| 1105 | + // open/type |
|
| 1106 | + $output .= '<input type="radio"'; |
|
| 1107 | + |
|
| 1108 | + // class |
|
| 1109 | + $output .= ' class="form-check-input" '; |
|
| 1110 | + |
|
| 1111 | + // name |
|
| 1112 | + if ( ! empty( $args['name'] ) ) { |
|
| 1113 | + $output .= AUI_Component_Helper::name( $args['name'] ); |
|
| 1114 | + } |
|
| 1115 | + |
|
| 1116 | + // id |
|
| 1117 | + if ( ! empty( $args['id'] ) ) { |
|
| 1118 | + $output .= AUI_Component_Helper::id( $args['id'] . $count ); |
|
| 1119 | + } |
|
| 1120 | + |
|
| 1121 | + // title |
|
| 1122 | + if ( ! empty( $args['title'] ) ) { |
|
| 1123 | + $output .= AUI_Component_Helper::title( $args['title'] ); |
|
| 1124 | + } |
|
| 1125 | + |
|
| 1126 | + // value |
|
| 1127 | + if ( isset( $args['value'] ) ) { |
|
| 1128 | + $output .= AUI_Component_Helper::value( $args['value'] ); |
|
| 1129 | + } |
|
| 1130 | + |
|
| 1131 | + // checked, for radio and checkboxes |
|
| 1132 | + if ( $args['checked'] ) { |
|
| 1133 | + $output .= ' checked '; |
|
| 1134 | + } |
|
| 1135 | + |
|
| 1136 | + // data-attributes |
|
| 1137 | + $output .= AUI_Component_Helper::data_attributes( $args ); |
|
| 1138 | + |
|
| 1139 | + // aria-attributes |
|
| 1140 | + $output .= AUI_Component_Helper::aria_attributes( $args ); |
|
| 1141 | + |
|
| 1142 | + // extra attributes |
|
| 1143 | + if ( ! empty( $args['extra_attributes'] ) ) { |
|
| 1144 | + $output .= AUI_Component_Helper::extra_attributes( $args['extra_attributes'] ); |
|
| 1145 | + } |
|
| 1146 | + |
|
| 1147 | + // required |
|
| 1148 | + if ( ! empty( $args['required'] ) ) { |
|
| 1149 | + $output .= ' required '; |
|
| 1150 | + } |
|
| 1151 | + |
|
| 1152 | + // close opening tag |
|
| 1153 | + $output .= ' >'; |
|
| 1154 | + |
|
| 1155 | + // label |
|
| 1156 | + if ( ! empty( $args['label'] ) && is_array( $args['label'] ) ) { |
|
| 1157 | + } elseif ( ! empty( $args['label'] ) ) { |
|
| 1158 | + $output .= self::label( array( |
|
| 1159 | + 'title' => $args['label'], |
|
| 1160 | + 'for' => $args['id'] . $count, |
|
| 1161 | + 'class' => 'form-check-label' |
|
| 1162 | + ), 'radio' ); |
|
| 1163 | + } |
|
| 1164 | + |
|
| 1165 | + // wrap |
|
| 1166 | + if ( ! $args['no_wrap'] ) { |
|
| 1167 | + $wrap_class = $args['inline'] ? 'form-check form-check-inline' : 'form-check'; |
|
| 1168 | + |
|
| 1169 | + // Unique wrap class |
|
| 1170 | + $uniq_class = 'fwrap'; |
|
| 1171 | + if ( ! empty( $args['name'] ) ) { |
|
| 1172 | + $uniq_class .= '-' . $args['name']; |
|
| 1173 | + } else if ( ! empty( $args['id'] ) ) { |
|
| 1174 | + $uniq_class .= '-' . $args['id']; |
|
| 1175 | + } |
|
| 1176 | + |
|
| 1177 | + if ( isset( $args['value'] ) || $args['value'] !== "" ) { |
|
| 1178 | + $uniq_class .= '-' . $args['value']; |
|
| 1179 | + } else { |
|
| 1180 | + $uniq_class .= '-' . $count; |
|
| 1181 | + } |
|
| 1182 | + $wrap_class .= ' ' . sanitize_html_class( $uniq_class ); |
|
| 1183 | + |
|
| 1184 | + $output = self::wrap( array( |
|
| 1185 | + 'content' => $output, |
|
| 1186 | + 'class' => $wrap_class |
|
| 1187 | + ) ); |
|
| 1188 | + } |
|
| 1189 | + |
|
| 1190 | + return $output; |
|
| 1191 | + } |
|
| 1192 | 1192 | |
| 1193 | 1193 | } |
| 1194 | 1194 | \ No newline at end of file |
@@ -7,40 +7,40 @@ |
||
| 7 | 7 | * Bail if we are not in WP. |
| 8 | 8 | */ |
| 9 | 9 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | - exit; |
|
| 10 | + exit; |
|
| 11 | 11 | } |
| 12 | 12 | |
| 13 | 13 | /** |
| 14 | 14 | * Set the version only if its the current newest while loading. |
| 15 | 15 | */ |
| 16 | 16 | add_action('after_setup_theme', function () { |
| 17 | - global $ayecode_ui_version,$ayecode_ui_file_key; |
|
| 18 | - $this_version = "0.1.65"; |
|
| 19 | - if(empty($ayecode_ui_version) || version_compare($this_version , $ayecode_ui_version, '>')){ |
|
| 20 | - $ayecode_ui_version = $this_version ; |
|
| 21 | - $ayecode_ui_file_key = wp_hash( __FILE__ ); |
|
| 22 | - } |
|
| 17 | + global $ayecode_ui_version,$ayecode_ui_file_key; |
|
| 18 | + $this_version = "0.1.65"; |
|
| 19 | + if(empty($ayecode_ui_version) || version_compare($this_version , $ayecode_ui_version, '>')){ |
|
| 20 | + $ayecode_ui_version = $this_version ; |
|
| 21 | + $ayecode_ui_file_key = wp_hash( __FILE__ ); |
|
| 22 | + } |
|
| 23 | 23 | },0); |
| 24 | 24 | |
| 25 | 25 | /** |
| 26 | 26 | * Load this version of WP Bootstrap Settings only if the file hash is the current one. |
| 27 | 27 | */ |
| 28 | 28 | add_action('after_setup_theme', function () { |
| 29 | - global $ayecode_ui_file_key; |
|
| 30 | - if($ayecode_ui_file_key && $ayecode_ui_file_key == wp_hash( __FILE__ )){ |
|
| 31 | - include_once( dirname( __FILE__ ) . '/includes/class-aui.php' ); |
|
| 32 | - include_once( dirname( __FILE__ ) . '/includes/ayecode-ui-settings.php' ); |
|
| 33 | - } |
|
| 29 | + global $ayecode_ui_file_key; |
|
| 30 | + if($ayecode_ui_file_key && $ayecode_ui_file_key == wp_hash( __FILE__ )){ |
|
| 31 | + include_once( dirname( __FILE__ ) . '/includes/class-aui.php' ); |
|
| 32 | + include_once( dirname( __FILE__ ) . '/includes/ayecode-ui-settings.php' ); |
|
| 33 | + } |
|
| 34 | 34 | },1); |
| 35 | 35 | |
| 36 | 36 | /** |
| 37 | 37 | * Add the function that calls the class. |
| 38 | 38 | */ |
| 39 | 39 | if(!function_exists('aui')){ |
| 40 | - function aui(){ |
|
| 41 | - if(!class_exists("AUI",false)){ |
|
| 42 | - return false; |
|
| 43 | - } |
|
| 44 | - return AUI::instance(); |
|
| 45 | - } |
|
| 40 | + function aui(){ |
|
| 41 | + if(!class_exists("AUI",false)){ |
|
| 42 | + return false; |
|
| 43 | + } |
|
| 44 | + return AUI::instance(); |
|
| 45 | + } |
|
| 46 | 46 | } |
| 47 | 47 | \ No newline at end of file |
@@ -12,8 +12,8 @@ discard block |
||
| 12 | 12 | class InstalledVersions |
| 13 | 13 | { |
| 14 | 14 | private static $installed = array ( |
| 15 | - 'root' => |
|
| 16 | - array ( |
|
| 15 | + 'root' => |
|
| 16 | + array ( |
|
| 17 | 17 | 'pretty_version' => 'dev-master', |
| 18 | 18 | 'version' => 'dev-master', |
| 19 | 19 | 'aliases' => |
@@ -21,96 +21,96 @@ discard block |
||
| 21 | 21 | ), |
| 22 | 22 | 'reference' => 'da0522a682b6afa9f09baaf1c048b651a2f367b6', |
| 23 | 23 | 'name' => 'ayecode/invoicing', |
| 24 | - ), |
|
| 25 | - 'versions' => |
|
| 26 | - array ( |
|
| 24 | + ), |
|
| 25 | + 'versions' => |
|
| 26 | + array ( |
|
| 27 | 27 | 'ayecode/ayecode-connect-helper' => |
| 28 | 28 | array ( |
| 29 | - 'pretty_version' => '1.0.3', |
|
| 30 | - 'version' => '1.0.3.0', |
|
| 31 | - 'aliases' => |
|
| 32 | - array ( |
|
| 33 | - ), |
|
| 34 | - 'reference' => '1af7cdefdbd20d4443a3ab4834e4c1cd8fe57fb4', |
|
| 29 | + 'pretty_version' => '1.0.3', |
|
| 30 | + 'version' => '1.0.3.0', |
|
| 31 | + 'aliases' => |
|
| 32 | + array ( |
|
| 33 | + ), |
|
| 34 | + 'reference' => '1af7cdefdbd20d4443a3ab4834e4c1cd8fe57fb4', |
|
| 35 | 35 | ), |
| 36 | 36 | 'ayecode/invoicing' => |
| 37 | 37 | array ( |
| 38 | - 'pretty_version' => 'dev-master', |
|
| 39 | - 'version' => 'dev-master', |
|
| 40 | - 'aliases' => |
|
| 41 | - array ( |
|
| 42 | - ), |
|
| 43 | - 'reference' => 'da0522a682b6afa9f09baaf1c048b651a2f367b6', |
|
| 38 | + 'pretty_version' => 'dev-master', |
|
| 39 | + 'version' => 'dev-master', |
|
| 40 | + 'aliases' => |
|
| 41 | + array ( |
|
| 42 | + ), |
|
| 43 | + 'reference' => 'da0522a682b6afa9f09baaf1c048b651a2f367b6', |
|
| 44 | 44 | ), |
| 45 | 45 | 'ayecode/wp-ayecode-ui' => |
| 46 | 46 | array ( |
| 47 | - 'pretty_version' => '0.1.65', |
|
| 48 | - 'version' => '0.1.65.0', |
|
| 49 | - 'aliases' => |
|
| 50 | - array ( |
|
| 51 | - ), |
|
| 52 | - 'reference' => '55b3420e600331a0a12976c361e7d55be952b898', |
|
| 47 | + 'pretty_version' => '0.1.65', |
|
| 48 | + 'version' => '0.1.65.0', |
|
| 49 | + 'aliases' => |
|
| 50 | + array ( |
|
| 51 | + ), |
|
| 52 | + 'reference' => '55b3420e600331a0a12976c361e7d55be952b898', |
|
| 53 | 53 | ), |
| 54 | 54 | 'ayecode/wp-deactivation-survey' => |
| 55 | 55 | array ( |
| 56 | - 'pretty_version' => '1.0.4', |
|
| 57 | - 'version' => '1.0.4.0', |
|
| 58 | - 'aliases' => |
|
| 59 | - array ( |
|
| 60 | - ), |
|
| 61 | - 'reference' => 'd2777fed30acfc4da53b45bf3b4fec2fb27d8398', |
|
| 56 | + 'pretty_version' => '1.0.4', |
|
| 57 | + 'version' => '1.0.4.0', |
|
| 58 | + 'aliases' => |
|
| 59 | + array ( |
|
| 60 | + ), |
|
| 61 | + 'reference' => 'd2777fed30acfc4da53b45bf3b4fec2fb27d8398', |
|
| 62 | 62 | ), |
| 63 | 63 | 'ayecode/wp-font-awesome-settings' => |
| 64 | 64 | array ( |
| 65 | - 'pretty_version' => '1.0.13', |
|
| 66 | - 'version' => '1.0.13.0', |
|
| 67 | - 'aliases' => |
|
| 68 | - array ( |
|
| 69 | - ), |
|
| 70 | - 'reference' => 'a7a11ee4290674ec214d1fe694139af275350402', |
|
| 65 | + 'pretty_version' => '1.0.13', |
|
| 66 | + 'version' => '1.0.13.0', |
|
| 67 | + 'aliases' => |
|
| 68 | + array ( |
|
| 69 | + ), |
|
| 70 | + 'reference' => 'a7a11ee4290674ec214d1fe694139af275350402', |
|
| 71 | 71 | ), |
| 72 | 72 | 'ayecode/wp-super-duper' => |
| 73 | 73 | array ( |
| 74 | - 'pretty_version' => '1.0.29', |
|
| 75 | - 'version' => '1.0.29.0', |
|
| 76 | - 'aliases' => |
|
| 77 | - array ( |
|
| 78 | - ), |
|
| 79 | - 'reference' => '18f3da558aec750cfeaa582f72619fbf8867677a', |
|
| 74 | + 'pretty_version' => '1.0.29', |
|
| 75 | + 'version' => '1.0.29.0', |
|
| 76 | + 'aliases' => |
|
| 77 | + array ( |
|
| 78 | + ), |
|
| 79 | + 'reference' => '18f3da558aec750cfeaa582f72619fbf8867677a', |
|
| 80 | 80 | ), |
| 81 | 81 | 'composer/installers' => |
| 82 | 82 | array ( |
| 83 | - 'pretty_version' => 'v1.12.0', |
|
| 84 | - 'version' => '1.12.0.0', |
|
| 85 | - 'aliases' => |
|
| 86 | - array ( |
|
| 87 | - ), |
|
| 88 | - 'reference' => 'd20a64ed3c94748397ff5973488761b22f6d3f19', |
|
| 83 | + 'pretty_version' => 'v1.12.0', |
|
| 84 | + 'version' => '1.12.0.0', |
|
| 85 | + 'aliases' => |
|
| 86 | + array ( |
|
| 87 | + ), |
|
| 88 | + 'reference' => 'd20a64ed3c94748397ff5973488761b22f6d3f19', |
|
| 89 | 89 | ), |
| 90 | 90 | 'maxmind-db/reader' => |
| 91 | 91 | array ( |
| 92 | - 'pretty_version' => 'v1.6.0', |
|
| 93 | - 'version' => '1.6.0.0', |
|
| 94 | - 'aliases' => |
|
| 95 | - array ( |
|
| 96 | - ), |
|
| 97 | - 'reference' => 'febd4920bf17c1da84cef58e56a8227dfb37fbe4', |
|
| 92 | + 'pretty_version' => 'v1.6.0', |
|
| 93 | + 'version' => '1.6.0.0', |
|
| 94 | + 'aliases' => |
|
| 95 | + array ( |
|
| 96 | + ), |
|
| 97 | + 'reference' => 'febd4920bf17c1da84cef58e56a8227dfb37fbe4', |
|
| 98 | 98 | ), |
| 99 | 99 | 'roundcube/plugin-installer' => |
| 100 | 100 | array ( |
| 101 | - 'replaced' => |
|
| 102 | - array ( |
|
| 101 | + 'replaced' => |
|
| 102 | + array ( |
|
| 103 | 103 | 0 => '*', |
| 104 | - ), |
|
| 104 | + ), |
|
| 105 | 105 | ), |
| 106 | 106 | 'shama/baton' => |
| 107 | 107 | array ( |
| 108 | - 'replaced' => |
|
| 109 | - array ( |
|
| 108 | + 'replaced' => |
|
| 109 | + array ( |
|
| 110 | 110 | 0 => '*', |
| 111 | - ), |
|
| 111 | + ), |
|
| 112 | + ), |
|
| 112 | 113 | ), |
| 113 | - ), |
|
| 114 | 114 | ); |
| 115 | 115 | |
| 116 | 116 | |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php return array ( |
| 2 | - 'root' => |
|
| 3 | - array ( |
|
| 2 | + 'root' => |
|
| 3 | + array ( |
|
| 4 | 4 | 'pretty_version' => 'dev-master', |
| 5 | 5 | 'version' => 'dev-master', |
| 6 | 6 | 'aliases' => |
@@ -8,94 +8,94 @@ discard block |
||
| 8 | 8 | ), |
| 9 | 9 | 'reference' => 'da0522a682b6afa9f09baaf1c048b651a2f367b6', |
| 10 | 10 | 'name' => 'ayecode/invoicing', |
| 11 | - ), |
|
| 12 | - 'versions' => |
|
| 13 | - array ( |
|
| 11 | + ), |
|
| 12 | + 'versions' => |
|
| 13 | + array ( |
|
| 14 | 14 | 'ayecode/ayecode-connect-helper' => |
| 15 | 15 | array ( |
| 16 | - 'pretty_version' => '1.0.3', |
|
| 17 | - 'version' => '1.0.3.0', |
|
| 18 | - 'aliases' => |
|
| 19 | - array ( |
|
| 20 | - ), |
|
| 21 | - 'reference' => '1af7cdefdbd20d4443a3ab4834e4c1cd8fe57fb4', |
|
| 16 | + 'pretty_version' => '1.0.3', |
|
| 17 | + 'version' => '1.0.3.0', |
|
| 18 | + 'aliases' => |
|
| 19 | + array ( |
|
| 20 | + ), |
|
| 21 | + 'reference' => '1af7cdefdbd20d4443a3ab4834e4c1cd8fe57fb4', |
|
| 22 | 22 | ), |
| 23 | 23 | 'ayecode/invoicing' => |
| 24 | 24 | array ( |
| 25 | - 'pretty_version' => 'dev-master', |
|
| 26 | - 'version' => 'dev-master', |
|
| 27 | - 'aliases' => |
|
| 28 | - array ( |
|
| 29 | - ), |
|
| 30 | - 'reference' => 'da0522a682b6afa9f09baaf1c048b651a2f367b6', |
|
| 25 | + 'pretty_version' => 'dev-master', |
|
| 26 | + 'version' => 'dev-master', |
|
| 27 | + 'aliases' => |
|
| 28 | + array ( |
|
| 29 | + ), |
|
| 30 | + 'reference' => 'da0522a682b6afa9f09baaf1c048b651a2f367b6', |
|
| 31 | 31 | ), |
| 32 | 32 | 'ayecode/wp-ayecode-ui' => |
| 33 | 33 | array ( |
| 34 | - 'pretty_version' => '0.1.65', |
|
| 35 | - 'version' => '0.1.65.0', |
|
| 36 | - 'aliases' => |
|
| 37 | - array ( |
|
| 38 | - ), |
|
| 39 | - 'reference' => '55b3420e600331a0a12976c361e7d55be952b898', |
|
| 34 | + 'pretty_version' => '0.1.65', |
|
| 35 | + 'version' => '0.1.65.0', |
|
| 36 | + 'aliases' => |
|
| 37 | + array ( |
|
| 38 | + ), |
|
| 39 | + 'reference' => '55b3420e600331a0a12976c361e7d55be952b898', |
|
| 40 | 40 | ), |
| 41 | 41 | 'ayecode/wp-deactivation-survey' => |
| 42 | 42 | array ( |
| 43 | - 'pretty_version' => '1.0.4', |
|
| 44 | - 'version' => '1.0.4.0', |
|
| 45 | - 'aliases' => |
|
| 46 | - array ( |
|
| 47 | - ), |
|
| 48 | - 'reference' => 'd2777fed30acfc4da53b45bf3b4fec2fb27d8398', |
|
| 43 | + 'pretty_version' => '1.0.4', |
|
| 44 | + 'version' => '1.0.4.0', |
|
| 45 | + 'aliases' => |
|
| 46 | + array ( |
|
| 47 | + ), |
|
| 48 | + 'reference' => 'd2777fed30acfc4da53b45bf3b4fec2fb27d8398', |
|
| 49 | 49 | ), |
| 50 | 50 | 'ayecode/wp-font-awesome-settings' => |
| 51 | 51 | array ( |
| 52 | - 'pretty_version' => '1.0.13', |
|
| 53 | - 'version' => '1.0.13.0', |
|
| 54 | - 'aliases' => |
|
| 55 | - array ( |
|
| 56 | - ), |
|
| 57 | - 'reference' => 'a7a11ee4290674ec214d1fe694139af275350402', |
|
| 52 | + 'pretty_version' => '1.0.13', |
|
| 53 | + 'version' => '1.0.13.0', |
|
| 54 | + 'aliases' => |
|
| 55 | + array ( |
|
| 56 | + ), |
|
| 57 | + 'reference' => 'a7a11ee4290674ec214d1fe694139af275350402', |
|
| 58 | 58 | ), |
| 59 | 59 | 'ayecode/wp-super-duper' => |
| 60 | 60 | array ( |
| 61 | - 'pretty_version' => '1.0.29', |
|
| 62 | - 'version' => '1.0.29.0', |
|
| 63 | - 'aliases' => |
|
| 64 | - array ( |
|
| 65 | - ), |
|
| 66 | - 'reference' => '18f3da558aec750cfeaa582f72619fbf8867677a', |
|
| 61 | + 'pretty_version' => '1.0.29', |
|
| 62 | + 'version' => '1.0.29.0', |
|
| 63 | + 'aliases' => |
|
| 64 | + array ( |
|
| 65 | + ), |
|
| 66 | + 'reference' => '18f3da558aec750cfeaa582f72619fbf8867677a', |
|
| 67 | 67 | ), |
| 68 | 68 | 'composer/installers' => |
| 69 | 69 | array ( |
| 70 | - 'pretty_version' => 'v1.12.0', |
|
| 71 | - 'version' => '1.12.0.0', |
|
| 72 | - 'aliases' => |
|
| 73 | - array ( |
|
| 74 | - ), |
|
| 75 | - 'reference' => 'd20a64ed3c94748397ff5973488761b22f6d3f19', |
|
| 70 | + 'pretty_version' => 'v1.12.0', |
|
| 71 | + 'version' => '1.12.0.0', |
|
| 72 | + 'aliases' => |
|
| 73 | + array ( |
|
| 74 | + ), |
|
| 75 | + 'reference' => 'd20a64ed3c94748397ff5973488761b22f6d3f19', |
|
| 76 | 76 | ), |
| 77 | 77 | 'maxmind-db/reader' => |
| 78 | 78 | array ( |
| 79 | - 'pretty_version' => 'v1.6.0', |
|
| 80 | - 'version' => '1.6.0.0', |
|
| 81 | - 'aliases' => |
|
| 82 | - array ( |
|
| 83 | - ), |
|
| 84 | - 'reference' => 'febd4920bf17c1da84cef58e56a8227dfb37fbe4', |
|
| 79 | + 'pretty_version' => 'v1.6.0', |
|
| 80 | + 'version' => '1.6.0.0', |
|
| 81 | + 'aliases' => |
|
| 82 | + array ( |
|
| 83 | + ), |
|
| 84 | + 'reference' => 'febd4920bf17c1da84cef58e56a8227dfb37fbe4', |
|
| 85 | 85 | ), |
| 86 | 86 | 'roundcube/plugin-installer' => |
| 87 | 87 | array ( |
| 88 | - 'replaced' => |
|
| 89 | - array ( |
|
| 88 | + 'replaced' => |
|
| 89 | + array ( |
|
| 90 | 90 | 0 => '*', |
| 91 | - ), |
|
| 91 | + ), |
|
| 92 | 92 | ), |
| 93 | 93 | 'shama/baton' => |
| 94 | 94 | array ( |
| 95 | - 'replaced' => |
|
| 96 | - array ( |
|
| 95 | + 'replaced' => |
|
| 96 | + array ( |
|
| 97 | 97 | 0 => '*', |
| 98 | - ), |
|
| 98 | + ), |
|
| 99 | + ), |
|
| 99 | 100 | ), |
| 100 | - ), |
|
| 101 | 101 | ); |
@@ -13,300 +13,300 @@ |
||
| 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}' => sanitize_text_field( wpinv_price( $subscription->get_recurring_amount(), $invoice->get_currency() ) ), |
|
| 108 | - '{subscription_initial_amount}' => sanitize_text_field( wpinv_price( $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 | - if ( apply_filters( 'getpaid_skip_subscription_email', false, $type, $subscription ) ) { |
|
| 162 | - return; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - do_action( 'getpaid_before_send_subscription_notification', $type, $subscription, $email ); |
|
| 166 | - |
|
| 167 | - $recipients = $this->get_recipients( $invoice ); |
|
| 168 | - $mailer = new GetPaid_Notification_Email_Sender(); |
|
| 169 | - $merge_tags = $email->get_merge_tags(); |
|
| 170 | - $content = $email->get_content( $merge_tags, $extra_args ); |
|
| 171 | - $subject = $email->add_merge_tags( $email->get_subject(), $merge_tags ); |
|
| 172 | - $attachments = $email->get_attachments(); |
|
| 173 | - |
|
| 174 | - $result = $mailer->send( |
|
| 175 | - apply_filters( 'getpaid_subscription_email_recipients', wpinv_parse_list( $recipients ), $email ), |
|
| 176 | - $subject, |
|
| 177 | - $content, |
|
| 178 | - $attachments |
|
| 179 | - ); |
|
| 180 | - |
|
| 181 | - // Maybe send a copy to the admin. |
|
| 182 | - if ( $email->include_admin_bcc() ) { |
|
| 183 | - $mailer->send( |
|
| 184 | - wpinv_get_admin_email(), |
|
| 185 | - $subject . __( ' - ADMIN BCC COPY', 'invoicing' ), |
|
| 186 | - $content, |
|
| 187 | - $attachments |
|
| 188 | - ); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - if ( $result ) { |
|
| 192 | - $invoice->add_system_note( |
|
| 193 | - sprintf( |
|
| 194 | - __( 'Successfully sent %s notification email to %s.', 'invoicing' ), |
|
| 195 | - sanitize_key( $type ), |
|
| 196 | - $email->is_admin_email() ? __( 'admin' ) : __( 'the customer' ) |
|
| 197 | - ) |
|
| 198 | - ); |
|
| 199 | - } else { |
|
| 200 | - $invoice->add_system_note( |
|
| 201 | - sprintf( |
|
| 202 | - __( 'Failed sending %s notification email to %s.', 'invoicing' ), |
|
| 203 | - sanitize_key( $type ), |
|
| 204 | - $email->is_admin_email() ? __( 'admin' ) : __( 'the customer' ) |
|
| 205 | - ) |
|
| 206 | - ); |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - do_action( 'getpaid_after_send_subscription_notification', $type, $subscription, $email ); |
|
| 210 | - |
|
| 211 | - } |
|
| 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 | + } |
|
| 212 | 67 | |
| 213 | 68 | /** |
| 214 | - * Sends a new trial notification. |
|
| 215 | - * |
|
| 216 | - * @param WPInv_Subscription $subscription |
|
| 217 | - */ |
|
| 218 | - 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 ) { |
|
| 219 | 75 | |
| 220 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 221 | - $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 | + } |
|
| 222 | 82 | |
| 223 | - } |
|
| 83 | + return $merge_tags; |
|
| 224 | 84 | |
| 225 | - /** |
|
| 226 | - * Sends a cancelled subscription notification. |
|
| 227 | - * |
|
| 228 | - * @param WPInv_Subscription $subscription |
|
| 229 | - */ |
|
| 230 | - public function subscription_cancelled( $subscription ) { |
|
| 85 | + } |
|
| 231 | 86 | |
| 232 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 233 | - $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}' => sanitize_text_field( wpinv_price( $subscription->get_recurring_amount(), $invoice->get_currency() ) ), |
|
| 108 | + '{subscription_initial_amount}' => sanitize_text_field( wpinv_price( $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 | + ); |
|
| 234 | 113 | |
| 235 | - } |
|
| 114 | + } |
|
| 236 | 115 | |
| 237 | - /** |
|
| 238 | - * Sends a subscription expired notification. |
|
| 239 | - * |
|
| 240 | - * @param WPInv_Subscription $subscription |
|
| 241 | - */ |
|
| 242 | - 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 | + } |
|
| 243 | 125 | |
| 244 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 245 | - $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() ); |
|
| 246 | 134 | |
| 247 | - } |
|
| 135 | + $cc = $invoice->get_email_cc(); |
|
| 248 | 136 | |
| 249 | - /** |
|
| 250 | - * Sends a completed subscription notification. |
|
| 251 | - * |
|
| 252 | - * @param WPInv_Subscription $subscription |
|
| 253 | - */ |
|
| 254 | - 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 | + } |
|
| 255 | 141 | |
| 256 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 257 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 142 | + return $recipients; |
|
| 143 | + } |
|
| 258 | 144 | |
| 259 | - } |
|
| 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 | + if ( apply_filters( 'getpaid_skip_subscription_email', false, $type, $subscription ) ) { |
|
| 162 | + return; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + do_action( 'getpaid_before_send_subscription_notification', $type, $subscription, $email ); |
|
| 166 | + |
|
| 167 | + $recipients = $this->get_recipients( $invoice ); |
|
| 168 | + $mailer = new GetPaid_Notification_Email_Sender(); |
|
| 169 | + $merge_tags = $email->get_merge_tags(); |
|
| 170 | + $content = $email->get_content( $merge_tags, $extra_args ); |
|
| 171 | + $subject = $email->add_merge_tags( $email->get_subject(), $merge_tags ); |
|
| 172 | + $attachments = $email->get_attachments(); |
|
| 173 | + |
|
| 174 | + $result = $mailer->send( |
|
| 175 | + apply_filters( 'getpaid_subscription_email_recipients', wpinv_parse_list( $recipients ), $email ), |
|
| 176 | + $subject, |
|
| 177 | + $content, |
|
| 178 | + $attachments |
|
| 179 | + ); |
|
| 180 | + |
|
| 181 | + // Maybe send a copy to the admin. |
|
| 182 | + if ( $email->include_admin_bcc() ) { |
|
| 183 | + $mailer->send( |
|
| 184 | + wpinv_get_admin_email(), |
|
| 185 | + $subject . __( ' - ADMIN BCC COPY', 'invoicing' ), |
|
| 186 | + $content, |
|
| 187 | + $attachments |
|
| 188 | + ); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + if ( $result ) { |
|
| 192 | + $invoice->add_system_note( |
|
| 193 | + sprintf( |
|
| 194 | + __( 'Successfully sent %s notification email to %s.', 'invoicing' ), |
|
| 195 | + sanitize_key( $type ), |
|
| 196 | + $email->is_admin_email() ? __( 'admin' ) : __( 'the customer' ) |
|
| 197 | + ) |
|
| 198 | + ); |
|
| 199 | + } else { |
|
| 200 | + $invoice->add_system_note( |
|
| 201 | + sprintf( |
|
| 202 | + __( 'Failed sending %s notification email to %s.', 'invoicing' ), |
|
| 203 | + sanitize_key( $type ), |
|
| 204 | + $email->is_admin_email() ? __( 'admin' ) : __( 'the customer' ) |
|
| 205 | + ) |
|
| 206 | + ); |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + do_action( 'getpaid_after_send_subscription_notification', $type, $subscription, $email ); |
|
| 260 | 210 | |
| 261 | - /** |
|
| 262 | - * Sends a subscription renewal reminder notification. |
|
| 263 | - * |
|
| 264 | - */ |
|
| 265 | - public function renewal_reminder() { |
|
| 211 | + } |
|
| 266 | 212 | |
| 267 | - $email = new GetPaid_Notification_Email( __FUNCTION__ ); |
|
| 213 | + /** |
|
| 214 | + * Sends a new trial notification. |
|
| 215 | + * |
|
| 216 | + * @param WPInv_Subscription $subscription |
|
| 217 | + */ |
|
| 218 | + public function subscription_trial( $subscription ) { |
|
| 268 | 219 | |
| 269 | - // Fetch reminder days. |
|
| 270 | - $reminder_days = array_unique( wp_parse_id_list( $email->get_option( 'days' ) ) ); |
|
| 220 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 221 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 271 | 222 | |
| 272 | - // Abort if non is set. |
|
| 273 | - if ( empty( $reminder_days ) ) { |
|
| 274 | - return; |
|
| 275 | - } |
|
| 223 | + } |
|
| 276 | 224 | |
| 277 | - // Fetch matching subscriptions. |
|
| 225 | + /** |
|
| 226 | + * Sends a cancelled subscription notification. |
|
| 227 | + * |
|
| 228 | + * @param WPInv_Subscription $subscription |
|
| 229 | + */ |
|
| 230 | + public function subscription_cancelled( $subscription ) { |
|
| 231 | + |
|
| 232 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 233 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 234 | + |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + /** |
|
| 238 | + * Sends a subscription expired notification. |
|
| 239 | + * |
|
| 240 | + * @param WPInv_Subscription $subscription |
|
| 241 | + */ |
|
| 242 | + public function subscription_expired( $subscription ) { |
|
| 243 | + |
|
| 244 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 245 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 246 | + |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + /** |
|
| 250 | + * Sends a completed subscription notification. |
|
| 251 | + * |
|
| 252 | + * @param WPInv_Subscription $subscription |
|
| 253 | + */ |
|
| 254 | + public function subscription_complete( $subscription ) { |
|
| 255 | + |
|
| 256 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 257 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 258 | + |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * Sends a subscription renewal reminder notification. |
|
| 263 | + * |
|
| 264 | + */ |
|
| 265 | + public function renewal_reminder() { |
|
| 266 | + |
|
| 267 | + $email = new GetPaid_Notification_Email( __FUNCTION__ ); |
|
| 268 | + |
|
| 269 | + // Fetch reminder days. |
|
| 270 | + $reminder_days = array_unique( wp_parse_id_list( $email->get_option( 'days' ) ) ); |
|
| 271 | + |
|
| 272 | + // Abort if non is set. |
|
| 273 | + if ( empty( $reminder_days ) ) { |
|
| 274 | + return; |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + // Fetch matching subscriptions. |
|
| 278 | 278 | $args = array( |
| 279 | 279 | 'number' => -1, |
| 280 | - 'count_total' => false, |
|
| 281 | - 'status' => 'trialling active', |
|
| 280 | + 'count_total' => false, |
|
| 281 | + 'status' => 'trialling active', |
|
| 282 | 282 | 'date_expires_query' => array( |
| 283 | - 'relation' => 'OR' |
|
| 283 | + 'relation' => 'OR' |
|
| 284 | 284 | ), |
| 285 | - ); |
|
| 285 | + ); |
|
| 286 | 286 | |
| 287 | - foreach ( $reminder_days as $days ) { |
|
| 288 | - $date = date_parse( date( 'Y-m-d', strtotime( "+$days days", current_time( 'timestamp' ) ) ) ); |
|
| 287 | + foreach ( $reminder_days as $days ) { |
|
| 288 | + $date = date_parse( date( 'Y-m-d', strtotime( "+$days days", current_time( 'timestamp' ) ) ) ); |
|
| 289 | 289 | |
| 290 | - $args['date_expires_query'][] = array( |
|
| 291 | - 'year' => $date['year'], |
|
| 292 | - 'month' => $date['month'], |
|
| 293 | - 'day' => $date['day'], |
|
| 294 | - ); |
|
| 290 | + $args['date_expires_query'][] = array( |
|
| 291 | + 'year' => $date['year'], |
|
| 292 | + 'month' => $date['month'], |
|
| 293 | + 'day' => $date['day'], |
|
| 294 | + ); |
|
| 295 | 295 | |
| 296 | - } |
|
| 296 | + } |
|
| 297 | 297 | |
| 298 | - $subscriptions = new GetPaid_Subscriptions_Query( $args ); |
|
| 298 | + $subscriptions = new GetPaid_Subscriptions_Query( $args ); |
|
| 299 | 299 | |
| 300 | 300 | foreach ( $subscriptions as $subscription ) { |
| 301 | 301 | |
| 302 | - // Skip packages. |
|
| 303 | - if ( apply_filters( 'getpaid_send_subscription_renewal_reminder_email', true ) ) { |
|
| 304 | - $email->object = $subscription; |
|
| 305 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 306 | - } |
|
| 302 | + // Skip packages. |
|
| 303 | + if ( apply_filters( 'getpaid_send_subscription_renewal_reminder_email', true ) ) { |
|
| 304 | + $email->object = $subscription; |
|
| 305 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 306 | + } |
|
| 307 | 307 | |
| 308 | - } |
|
| 308 | + } |
|
| 309 | 309 | |
| 310 | - } |
|
| 310 | + } |
|
| 311 | 311 | |
| 312 | 312 | } |
@@ -13,36 +13,36 @@ discard block |
||
| 13 | 13 | class GetPaid_Notification_Email { |
| 14 | 14 | |
| 15 | 15 | /** |
| 16 | - * Contains the type of this notification email. |
|
| 17 | - * |
|
| 18 | - * @var string |
|
| 19 | - */ |
|
| 16 | + * Contains the type of this notification email. |
|
| 17 | + * |
|
| 18 | + * @var string |
|
| 19 | + */ |
|
| 20 | 20 | public $id; |
| 21 | 21 | |
| 22 | 22 | /** |
| 23 | - * Contains any object to use in filters. |
|
| 24 | - * |
|
| 25 | - * @var false|WPInv_Invoice|WPInv_Item|WPInv_Subscription |
|
| 26 | - */ |
|
| 23 | + * Contains any object to use in filters. |
|
| 24 | + * |
|
| 25 | + * @var false|WPInv_Invoice|WPInv_Item|WPInv_Subscription |
|
| 26 | + */ |
|
| 27 | 27 | public $object; |
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | - * Class constructor. |
|
| 31 | - * |
|
| 30 | + * Class constructor. |
|
| 31 | + * |
|
| 32 | 32 | * @param string $id Email Type. |
| 33 | 33 | * @param mixed $object Optional. Associated object. |
| 34 | - */ |
|
| 35 | - public function __construct( $id, $object = false ) { |
|
| 34 | + */ |
|
| 35 | + public function __construct( $id, $object = false ) { |
|
| 36 | 36 | $this->id = $id; |
| 37 | 37 | $this->object = $object; |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | /** |
| 41 | - * Retrieves an option |
|
| 42 | - * |
|
| 41 | + * Retrieves an option |
|
| 42 | + * |
|
| 43 | 43 | * @return mixed |
| 44 | - */ |
|
| 45 | - public function get_option( $key ) { |
|
| 44 | + */ |
|
| 45 | + public function get_option( $key ) { |
|
| 46 | 46 | |
| 47 | 47 | $key = "email_{$this->id}_$key"; |
| 48 | 48 | $value = wpinv_get_option( $key, null ); |
@@ -60,80 +60,80 @@ discard block |
||
| 60 | 60 | } |
| 61 | 61 | |
| 62 | 62 | /** |
| 63 | - * Retrieves the email body. |
|
| 64 | - * |
|
| 63 | + * Retrieves the email body. |
|
| 64 | + * |
|
| 65 | 65 | * @return string |
| 66 | - */ |
|
| 67 | - public function get_body() { |
|
| 66 | + */ |
|
| 67 | + public function get_body() { |
|
| 68 | 68 | $body = $this->get_option( 'body' ); |
| 69 | 69 | return apply_filters( 'getpaid_get_email_body', $body, $this->id, $this->object ); |
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | /** |
| 73 | - * Retrieves the email subject. |
|
| 74 | - * |
|
| 73 | + * Retrieves the email subject. |
|
| 74 | + * |
|
| 75 | 75 | * @return string |
| 76 | - */ |
|
| 77 | - public function get_subject() { |
|
| 76 | + */ |
|
| 77 | + public function get_subject() { |
|
| 78 | 78 | $subject = $this->get_option( 'subject' ); |
| 79 | 79 | return apply_filters( 'getpaid_get_email_subject', $subject, $this->id, $this->object ); |
| 80 | 80 | } |
| 81 | 81 | |
| 82 | 82 | /** |
| 83 | - * Retrieves the email heading. |
|
| 84 | - * |
|
| 83 | + * Retrieves the email heading. |
|
| 84 | + * |
|
| 85 | 85 | * @return string |
| 86 | - */ |
|
| 87 | - public function get_heading() { |
|
| 86 | + */ |
|
| 87 | + public function get_heading() { |
|
| 88 | 88 | $heading = $this->get_option( 'heading' ); |
| 89 | 89 | return apply_filters( 'getpaid_get_email_heading', $heading, $this->id, $this->object ); |
| 90 | 90 | } |
| 91 | 91 | |
| 92 | 92 | /** |
| 93 | - * Checks if an email is active. |
|
| 94 | - * |
|
| 93 | + * Checks if an email is active. |
|
| 94 | + * |
|
| 95 | 95 | * @return bool |
| 96 | - */ |
|
| 97 | - public function is_active() { |
|
| 96 | + */ |
|
| 97 | + public function is_active() { |
|
| 98 | 98 | $is_active = ! empty( $this->get_option( 'active' ) ); |
| 99 | 99 | return apply_filters( 'getpaid_email_type_is_active', $is_active, $this->id, $this->object ); |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | /** |
| 103 | - * Checks if the site's admin should receive email notifications. |
|
| 104 | - * |
|
| 103 | + * Checks if the site's admin should receive email notifications. |
|
| 104 | + * |
|
| 105 | 105 | * @return bool |
| 106 | - */ |
|
| 107 | - public function include_admin_bcc() { |
|
| 106 | + */ |
|
| 107 | + public function include_admin_bcc() { |
|
| 108 | 108 | $include_admin_bcc = ! empty( $this->get_option( 'admin_bcc' ) ); |
| 109 | 109 | return apply_filters( 'getpaid_email_type_include_admin_bcc', $include_admin_bcc, $this->id, $this->object ); |
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | /** |
| 113 | - * Checks whether this email should be sent to the customer or admin. |
|
| 114 | - * |
|
| 113 | + * Checks whether this email should be sent to the customer or admin. |
|
| 114 | + * |
|
| 115 | 115 | * @return bool |
| 116 | - */ |
|
| 117 | - public function is_admin_email() { |
|
| 116 | + */ |
|
| 117 | + public function is_admin_email() { |
|
| 118 | 118 | $is_admin_email = in_array( $this->id, array( 'new_invoice', 'failed_invoice' ) ); |
| 119 | 119 | return apply_filters( 'getpaid_email_type_is_admin_email', $is_admin_email, $this->id, $this->object ); |
| 120 | 120 | } |
| 121 | 121 | |
| 122 | 122 | /** |
| 123 | - * Returns email attachments. |
|
| 124 | - * |
|
| 123 | + * Returns email attachments. |
|
| 124 | + * |
|
| 125 | 125 | * @return array |
| 126 | - */ |
|
| 127 | - public function get_attachments() { |
|
| 126 | + */ |
|
| 127 | + public function get_attachments() { |
|
| 128 | 128 | return apply_filters( 'getpaid_get_email_attachments', array(), $this->id, $this->object ); |
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | /** |
| 132 | - * Returns an array of merge tags. |
|
| 133 | - * |
|
| 132 | + * Returns an array of merge tags. |
|
| 133 | + * |
|
| 134 | 134 | * @return array |
| 135 | - */ |
|
| 136 | - public function get_merge_tags() { |
|
| 135 | + */ |
|
| 136 | + public function get_merge_tags() { |
|
| 137 | 137 | |
| 138 | 138 | $merge_tags = array( |
| 139 | 139 | '{site_title}' => wpinv_get_blogname(), |
@@ -144,13 +144,13 @@ discard block |
||
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | /** |
| 147 | - * Adds merge tags to a text. |
|
| 148 | - * |
|
| 147 | + * Adds merge tags to a text. |
|
| 148 | + * |
|
| 149 | 149 | * @param string string $text |
| 150 | 150 | * @param array $merge_tags |
| 151 | 151 | * @return string |
| 152 | - */ |
|
| 153 | - public function add_merge_tags( $text, $merge_tags = array() ) { |
|
| 152 | + */ |
|
| 153 | + public function add_merge_tags( $text, $merge_tags = array() ) { |
|
| 154 | 154 | |
| 155 | 155 | foreach ( $merge_tags as $key => $value ) { |
| 156 | 156 | $text = str_replace( $key, $value, $text ); |
@@ -160,13 +160,13 @@ discard block |
||
| 160 | 160 | } |
| 161 | 161 | |
| 162 | 162 | /** |
| 163 | - * Returns the email content |
|
| 164 | - * |
|
| 163 | + * Returns the email content |
|
| 164 | + * |
|
| 165 | 165 | * @param array $merge_tags |
| 166 | 166 | * @param array $extra_args Extra template args |
| 167 | 167 | * @return string |
| 168 | - */ |
|
| 169 | - public function get_content( $merge_tags = array(), $extra_args = array() ) { |
|
| 168 | + */ |
|
| 169 | + public function get_content( $merge_tags = array(), $extra_args = array() ) { |
|
| 170 | 170 | |
| 171 | 171 | $content = wpinv_get_template_html( |
| 172 | 172 | "emails/wpinv-email-{$this->id}.php", |
@@ -12,491 +12,491 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class GetPaid_Invoice_Notification_Emails { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * The array of invoice email actions. |
|
| 17 | - * |
|
| 18 | - * @param array |
|
| 19 | - */ |
|
| 20 | - public $invoice_actions; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * Class constructor |
|
| 24 | - * |
|
| 25 | - */ |
|
| 26 | - public function __construct() { |
|
| 27 | - |
|
| 28 | - $this->invoice_actions = apply_filters( |
|
| 29 | - 'getpaid_notification_email_invoice_triggers', |
|
| 30 | - array( |
|
| 31 | - 'getpaid_new_invoice' => array( 'new_invoice', 'user_invoice' ), |
|
| 32 | - 'getpaid_invoice_status_wpi-cancelled' => 'cancelled_invoice', |
|
| 33 | - 'getpaid_invoice_status_wpi-failed' => 'failed_invoice', |
|
| 34 | - 'getpaid_invoice_status_wpi-onhold' => 'onhold_invoice', |
|
| 35 | - 'getpaid_invoice_status_wpi-processing' => 'processing_invoice', |
|
| 36 | - 'getpaid_invoice_status_publish' => 'completed_invoice', |
|
| 37 | - 'getpaid_invoice_status_wpi-renewal' => 'completed_invoice', |
|
| 38 | - 'getpaid_invoice_status_wpi-refunded' => 'refunded_invoice', |
|
| 39 | - 'getpaid_new_customer_note' => 'user_note', |
|
| 40 | - 'getpaid_daily_maintenance' => 'overdue', |
|
| 41 | - ) |
|
| 42 | - ); |
|
| 43 | - |
|
| 44 | - $this->init_hooks(); |
|
| 45 | - |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Registers email hooks. |
|
| 50 | - */ |
|
| 51 | - public function init_hooks() { |
|
| 52 | - |
|
| 53 | - add_filter( 'getpaid_get_email_merge_tags', array( $this, 'invoice_merge_tags' ), 10, 2 ); |
|
| 54 | - add_filter( 'getpaid_invoice_email_recipients', array( $this, 'filter_email_recipients' ), 10, 2 ); |
|
| 55 | - |
|
| 56 | - foreach ( $this->invoice_actions as $hook => $email_type ) { |
|
| 57 | - $this->init_email_type_hook( $hook, $email_type ); |
|
| 58 | - } |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Registers an email hook for an invoice action. |
|
| 63 | - * |
|
| 64 | - * @param string $hook |
|
| 65 | - * @param string|array $email_type |
|
| 66 | - */ |
|
| 67 | - public function init_email_type_hook( $hook, $email_type ) { |
|
| 68 | - |
|
| 69 | - $email_type = wpinv_parse_list( $email_type ); |
|
| 70 | - |
|
| 71 | - foreach ( $email_type as $type ) { |
|
| 72 | - |
|
| 73 | - $email = new GetPaid_Notification_Email( $type ); |
|
| 74 | - |
|
| 75 | - // Abort if it is not active. |
|
| 76 | - if ( ! $email->is_active() ) { |
|
| 77 | - continue; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - if ( method_exists( $this, $type ) ) { |
|
| 81 | - add_action( $hook, array( $this, $type ), 100, 2 ); |
|
| 82 | - continue; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - do_action( 'getpaid_invoice_init_email_type_hook', $type, $hook ); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Filters invoice merge tags. |
|
| 92 | - * |
|
| 93 | - * @param array $merge_tags |
|
| 94 | - * @param mixed|WPInv_Invoice|WPInv_Subscription $object |
|
| 95 | - */ |
|
| 96 | - public function invoice_merge_tags( $merge_tags, $object ) { |
|
| 97 | - |
|
| 98 | - if ( is_a( $object, 'WPInv_Invoice' ) ) { |
|
| 99 | - return array_merge( |
|
| 100 | - $merge_tags, |
|
| 101 | - $this->get_invoice_merge_tags( $object ) |
|
| 102 | - ); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - if ( is_a( $object, 'WPInv_Subscription' ) ) { |
|
| 106 | - return array_merge( |
|
| 107 | - $merge_tags, |
|
| 108 | - $this->get_invoice_merge_tags( $object->get_parent_payment() ) |
|
| 109 | - ); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - return $merge_tags; |
|
| 113 | - |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * Generates invoice merge tags. |
|
| 118 | - * |
|
| 119 | - * @param WPInv_Invoice $invoice |
|
| 120 | - * @return array |
|
| 121 | - */ |
|
| 122 | - public function get_invoice_merge_tags( $invoice ) { |
|
| 123 | - |
|
| 124 | - // Abort if it does not exist. |
|
| 125 | - if ( ! $invoice->get_id() ) { |
|
| 126 | - return array(); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - $merge_tags = array( |
|
| 130 | - '{name}' => sanitize_text_field( $invoice->get_user_full_name() ), |
|
| 131 | - '{full_name}' => sanitize_text_field( $invoice->get_user_full_name() ), |
|
| 132 | - '{first_name}' => sanitize_text_field( $invoice->get_first_name() ), |
|
| 133 | - '{last_name}' => sanitize_text_field( $invoice->get_last_name() ), |
|
| 134 | - '{email}' => sanitize_email( $invoice->get_email() ), |
|
| 135 | - '{invoice_number}' => sanitize_text_field( $invoice->get_number() ), |
|
| 136 | - '{invoice_currency}' => sanitize_text_field( $invoice->get_currency() ), |
|
| 137 | - '{invoice_total}' => sanitize_text_field( wpinv_price( $invoice->get_total(), $invoice->get_currency() ) ), |
|
| 138 | - '{invoice_link}' => esc_url( $invoice->get_view_url() ), |
|
| 139 | - '{invoice_pay_link}' => esc_url( $invoice->get_checkout_payment_url() ), |
|
| 140 | - '{invoice_receipt_link}'=> esc_url( $invoice->get_receipt_url() ), |
|
| 141 | - '{invoice_date}' => getpaid_format_date_value( $invoice->get_date_created() ), |
|
| 142 | - '{invoice_due_date}' => getpaid_format_date_value( $invoice->get_due_date(), __( 'on receipt', 'invoicing' ) ), |
|
| 143 | - '{invoice_quote}' => sanitize_text_field( strtolower( $invoice->get_label() ) ), |
|
| 144 | - '{invoice_label}' => sanitize_text_field( ucfirst( $invoice->get_label() ) ), |
|
| 145 | - '{invoice_description}' => wp_kses_post( $invoice->get_description() ), |
|
| 146 | - '{subscription_name}' => wp_kses_post( $invoice->get_subscription_name() ), |
|
| 147 | - '{is_was}' => strtotime( $invoice->get_due_date() ) < current_time( 'timestamp' ) ? __( 'was', 'invoicing' ) : __( 'is', 'invoicing' ), |
|
| 148 | - ); |
|
| 149 | - |
|
| 150 | - $payment_form_data = $invoice->get_meta( 'payment_form_data', true ); |
|
| 151 | - |
|
| 152 | - if ( is_array( $payment_form_data ) ) { |
|
| 153 | - |
|
| 154 | - foreach ( $payment_form_data as $label => $value ) { |
|
| 155 | - |
|
| 156 | - $label = preg_replace( '/[^a-z0-9]+/', '_', strtolower( $label ) ); |
|
| 157 | - $value = is_array( $value ) ? implode( ', ', $value ) : $value; |
|
| 158 | - |
|
| 159 | - if ( is_scalar ( $value ) ) { |
|
| 160 | - $merge_tags[ "{{$label}}" ] = wp_kses_post( $value ); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - return apply_filters( 'getpaid_invoice_email_merge_tags', $merge_tags, $invoice ); |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * Helper function to send an email. |
|
| 172 | - * |
|
| 173 | - * @param WPInv_Invoice $invoice |
|
| 174 | - * @param GetPaid_Notification_Email $email |
|
| 175 | - * @param string $type |
|
| 176 | - * @param string|array $recipients |
|
| 177 | - * @param array $extra_args Extra template args. |
|
| 178 | - */ |
|
| 179 | - public function send_email( $invoice, $email, $type, $recipients, $extra_args = array() ) { |
|
| 180 | - |
|
| 181 | - do_action( 'getpaid_before_send_invoice_notification', $type, $invoice, $email ); |
|
| 182 | - |
|
| 183 | - $skip = $invoice->is_free() && wpinv_get_option( 'skip_email_free_invoice' ); |
|
| 184 | - if ( apply_filters( 'getpaid_skip_invoice_email', $skip, $type, $invoice ) ) { |
|
| 185 | - return; |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - $mailer = new GetPaid_Notification_Email_Sender(); |
|
| 189 | - $merge_tags = $email->get_merge_tags(); |
|
| 190 | - |
|
| 191 | - $result = $mailer->send( |
|
| 192 | - apply_filters( 'getpaid_invoice_email_recipients', wpinv_parse_list( $recipients ), $email ), |
|
| 193 | - $email->add_merge_tags( $email->get_subject(), $merge_tags ), |
|
| 194 | - $email->get_content( $merge_tags, $extra_args ), |
|
| 195 | - $email->get_attachments() |
|
| 196 | - ); |
|
| 197 | - |
|
| 198 | - // Maybe send a copy to the admin. |
|
| 199 | - if ( $email->include_admin_bcc() ) { |
|
| 200 | - $mailer->send( |
|
| 201 | - wpinv_get_admin_email(), |
|
| 202 | - $email->add_merge_tags( $email->get_subject() . __( ' - ADMIN BCC COPY', 'invoicing' ), $merge_tags ), |
|
| 203 | - $email->get_content( $merge_tags ), |
|
| 204 | - $email->get_attachments() |
|
| 205 | - ); |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - if ( $result ) { |
|
| 209 | - $invoice->add_system_note( |
|
| 210 | - sprintf( |
|
| 211 | - __( 'Successfully sent %s notification email to %s.', 'invoicing' ), |
|
| 212 | - sanitize_key( $type ), |
|
| 213 | - $email->is_admin_email() ? __( 'admin' ) : __( 'the customer' ) |
|
| 214 | - ) |
|
| 215 | - ); |
|
| 216 | - } else { |
|
| 217 | - $invoice->add_system_note( |
|
| 218 | - sprintf( |
|
| 219 | - __( 'Failed sending %s notification email to %s.', 'invoicing' ), |
|
| 220 | - sanitize_key( $type ), |
|
| 221 | - $email->is_admin_email() ? __( 'admin' ) : __( 'the customer' ) |
|
| 222 | - ) |
|
| 223 | - ); |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - do_action( 'getpaid_after_send_invoice_notification', $type, $invoice, $email ); |
|
| 227 | - |
|
| 228 | - return $result; |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - /** |
|
| 232 | - * Also send emails to any cc users. |
|
| 233 | - * |
|
| 234 | - * @param array $recipients |
|
| 235 | - * @param GetPaid_Notification_Email $email |
|
| 236 | - */ |
|
| 237 | - public function filter_email_recipients( $recipients, $email ) { |
|
| 238 | - |
|
| 239 | - if ( ! $email->is_admin_email() ) { |
|
| 240 | - $cc = $email->object->get_email_cc(); |
|
| 241 | - $cc_2 = get_user_meta( $email->object->get_user_id(), '_wpinv_email_cc', true ); |
|
| 242 | - |
|
| 243 | - if ( ! empty( $cc ) ) { |
|
| 244 | - $cc = array_map( 'sanitize_email', wpinv_parse_list( $cc ) ); |
|
| 245 | - $recipients = array_filter( array_unique( array_merge( $recipients, $cc ) ) ); |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - if ( ! empty( $cc_2 ) ) { |
|
| 249 | - $cc_2 = array_map( 'sanitize_email', wpinv_parse_list( $cc_2 ) ); |
|
| 250 | - $recipients = array_filter( array_unique( array_merge( $recipients, $cc_2 ) ) ); |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - return $recipients; |
|
| 256 | - |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - /** |
|
| 260 | - * Sends a new invoice notification. |
|
| 261 | - * |
|
| 262 | - * @param WPInv_Invoice $invoice |
|
| 263 | - */ |
|
| 264 | - public function new_invoice( $invoice ) { |
|
| 265 | - |
|
| 266 | - // Only send this email for invoices created via the admin page. |
|
| 267 | - if ( ! $invoice->is_type( 'invoice' ) || $invoice->is_paid() || $this->is_payment_form_invoice( $invoice->get_id() ) ) { |
|
| 268 | - return; |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 272 | - $recipient = wpinv_get_admin_email(); |
|
| 273 | - |
|
| 274 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 275 | - |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - /** |
|
| 279 | - * Sends a cancelled invoice notification. |
|
| 280 | - * |
|
| 281 | - * @param WPInv_Invoice $invoice |
|
| 282 | - */ |
|
| 283 | - public function cancelled_invoice( $invoice ) { |
|
| 284 | - |
|
| 285 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 286 | - $recipient = $invoice->get_email(); |
|
| 287 | - |
|
| 288 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - /** |
|
| 292 | - * Sends a failed invoice notification. |
|
| 293 | - * |
|
| 294 | - * @param WPInv_Invoice $invoice |
|
| 295 | - */ |
|
| 296 | - public function failed_invoice( $invoice ) { |
|
| 297 | - |
|
| 298 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 299 | - $recipient = wpinv_get_admin_email(); |
|
| 300 | - |
|
| 301 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 302 | - |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - /** |
|
| 306 | - * Sends a notification whenever an invoice is put on hold. |
|
| 307 | - * |
|
| 308 | - * @param WPInv_Invoice $invoice |
|
| 309 | - */ |
|
| 310 | - public function onhold_invoice( $invoice ) { |
|
| 311 | - |
|
| 312 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 313 | - $recipient = $invoice->get_email(); |
|
| 314 | - |
|
| 315 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 15 | + /** |
|
| 16 | + * The array of invoice email actions. |
|
| 17 | + * |
|
| 18 | + * @param array |
|
| 19 | + */ |
|
| 20 | + public $invoice_actions; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * Class constructor |
|
| 24 | + * |
|
| 25 | + */ |
|
| 26 | + public function __construct() { |
|
| 27 | + |
|
| 28 | + $this->invoice_actions = apply_filters( |
|
| 29 | + 'getpaid_notification_email_invoice_triggers', |
|
| 30 | + array( |
|
| 31 | + 'getpaid_new_invoice' => array( 'new_invoice', 'user_invoice' ), |
|
| 32 | + 'getpaid_invoice_status_wpi-cancelled' => 'cancelled_invoice', |
|
| 33 | + 'getpaid_invoice_status_wpi-failed' => 'failed_invoice', |
|
| 34 | + 'getpaid_invoice_status_wpi-onhold' => 'onhold_invoice', |
|
| 35 | + 'getpaid_invoice_status_wpi-processing' => 'processing_invoice', |
|
| 36 | + 'getpaid_invoice_status_publish' => 'completed_invoice', |
|
| 37 | + 'getpaid_invoice_status_wpi-renewal' => 'completed_invoice', |
|
| 38 | + 'getpaid_invoice_status_wpi-refunded' => 'refunded_invoice', |
|
| 39 | + 'getpaid_new_customer_note' => 'user_note', |
|
| 40 | + 'getpaid_daily_maintenance' => 'overdue', |
|
| 41 | + ) |
|
| 42 | + ); |
|
| 43 | + |
|
| 44 | + $this->init_hooks(); |
|
| 45 | + |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Registers email hooks. |
|
| 50 | + */ |
|
| 51 | + public function init_hooks() { |
|
| 52 | + |
|
| 53 | + add_filter( 'getpaid_get_email_merge_tags', array( $this, 'invoice_merge_tags' ), 10, 2 ); |
|
| 54 | + add_filter( 'getpaid_invoice_email_recipients', array( $this, 'filter_email_recipients' ), 10, 2 ); |
|
| 55 | + |
|
| 56 | + foreach ( $this->invoice_actions as $hook => $email_type ) { |
|
| 57 | + $this->init_email_type_hook( $hook, $email_type ); |
|
| 58 | + } |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Registers an email hook for an invoice action. |
|
| 63 | + * |
|
| 64 | + * @param string $hook |
|
| 65 | + * @param string|array $email_type |
|
| 66 | + */ |
|
| 67 | + public function init_email_type_hook( $hook, $email_type ) { |
|
| 68 | + |
|
| 69 | + $email_type = wpinv_parse_list( $email_type ); |
|
| 70 | + |
|
| 71 | + foreach ( $email_type as $type ) { |
|
| 72 | + |
|
| 73 | + $email = new GetPaid_Notification_Email( $type ); |
|
| 74 | + |
|
| 75 | + // Abort if it is not active. |
|
| 76 | + if ( ! $email->is_active() ) { |
|
| 77 | + continue; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + if ( method_exists( $this, $type ) ) { |
|
| 81 | + add_action( $hook, array( $this, $type ), 100, 2 ); |
|
| 82 | + continue; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + do_action( 'getpaid_invoice_init_email_type_hook', $type, $hook ); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Filters invoice merge tags. |
|
| 92 | + * |
|
| 93 | + * @param array $merge_tags |
|
| 94 | + * @param mixed|WPInv_Invoice|WPInv_Subscription $object |
|
| 95 | + */ |
|
| 96 | + public function invoice_merge_tags( $merge_tags, $object ) { |
|
| 97 | + |
|
| 98 | + if ( is_a( $object, 'WPInv_Invoice' ) ) { |
|
| 99 | + return array_merge( |
|
| 100 | + $merge_tags, |
|
| 101 | + $this->get_invoice_merge_tags( $object ) |
|
| 102 | + ); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + if ( is_a( $object, 'WPInv_Subscription' ) ) { |
|
| 106 | + return array_merge( |
|
| 107 | + $merge_tags, |
|
| 108 | + $this->get_invoice_merge_tags( $object->get_parent_payment() ) |
|
| 109 | + ); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + return $merge_tags; |
|
| 113 | + |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * Generates invoice merge tags. |
|
| 118 | + * |
|
| 119 | + * @param WPInv_Invoice $invoice |
|
| 120 | + * @return array |
|
| 121 | + */ |
|
| 122 | + public function get_invoice_merge_tags( $invoice ) { |
|
| 123 | + |
|
| 124 | + // Abort if it does not exist. |
|
| 125 | + if ( ! $invoice->get_id() ) { |
|
| 126 | + return array(); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + $merge_tags = array( |
|
| 130 | + '{name}' => sanitize_text_field( $invoice->get_user_full_name() ), |
|
| 131 | + '{full_name}' => sanitize_text_field( $invoice->get_user_full_name() ), |
|
| 132 | + '{first_name}' => sanitize_text_field( $invoice->get_first_name() ), |
|
| 133 | + '{last_name}' => sanitize_text_field( $invoice->get_last_name() ), |
|
| 134 | + '{email}' => sanitize_email( $invoice->get_email() ), |
|
| 135 | + '{invoice_number}' => sanitize_text_field( $invoice->get_number() ), |
|
| 136 | + '{invoice_currency}' => sanitize_text_field( $invoice->get_currency() ), |
|
| 137 | + '{invoice_total}' => sanitize_text_field( wpinv_price( $invoice->get_total(), $invoice->get_currency() ) ), |
|
| 138 | + '{invoice_link}' => esc_url( $invoice->get_view_url() ), |
|
| 139 | + '{invoice_pay_link}' => esc_url( $invoice->get_checkout_payment_url() ), |
|
| 140 | + '{invoice_receipt_link}'=> esc_url( $invoice->get_receipt_url() ), |
|
| 141 | + '{invoice_date}' => getpaid_format_date_value( $invoice->get_date_created() ), |
|
| 142 | + '{invoice_due_date}' => getpaid_format_date_value( $invoice->get_due_date(), __( 'on receipt', 'invoicing' ) ), |
|
| 143 | + '{invoice_quote}' => sanitize_text_field( strtolower( $invoice->get_label() ) ), |
|
| 144 | + '{invoice_label}' => sanitize_text_field( ucfirst( $invoice->get_label() ) ), |
|
| 145 | + '{invoice_description}' => wp_kses_post( $invoice->get_description() ), |
|
| 146 | + '{subscription_name}' => wp_kses_post( $invoice->get_subscription_name() ), |
|
| 147 | + '{is_was}' => strtotime( $invoice->get_due_date() ) < current_time( 'timestamp' ) ? __( 'was', 'invoicing' ) : __( 'is', 'invoicing' ), |
|
| 148 | + ); |
|
| 149 | + |
|
| 150 | + $payment_form_data = $invoice->get_meta( 'payment_form_data', true ); |
|
| 151 | + |
|
| 152 | + if ( is_array( $payment_form_data ) ) { |
|
| 153 | + |
|
| 154 | + foreach ( $payment_form_data as $label => $value ) { |
|
| 155 | + |
|
| 156 | + $label = preg_replace( '/[^a-z0-9]+/', '_', strtolower( $label ) ); |
|
| 157 | + $value = is_array( $value ) ? implode( ', ', $value ) : $value; |
|
| 158 | + |
|
| 159 | + if ( is_scalar ( $value ) ) { |
|
| 160 | + $merge_tags[ "{{$label}}" ] = wp_kses_post( $value ); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + return apply_filters( 'getpaid_invoice_email_merge_tags', $merge_tags, $invoice ); |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * Helper function to send an email. |
|
| 172 | + * |
|
| 173 | + * @param WPInv_Invoice $invoice |
|
| 174 | + * @param GetPaid_Notification_Email $email |
|
| 175 | + * @param string $type |
|
| 176 | + * @param string|array $recipients |
|
| 177 | + * @param array $extra_args Extra template args. |
|
| 178 | + */ |
|
| 179 | + public function send_email( $invoice, $email, $type, $recipients, $extra_args = array() ) { |
|
| 180 | + |
|
| 181 | + do_action( 'getpaid_before_send_invoice_notification', $type, $invoice, $email ); |
|
| 182 | + |
|
| 183 | + $skip = $invoice->is_free() && wpinv_get_option( 'skip_email_free_invoice' ); |
|
| 184 | + if ( apply_filters( 'getpaid_skip_invoice_email', $skip, $type, $invoice ) ) { |
|
| 185 | + return; |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + $mailer = new GetPaid_Notification_Email_Sender(); |
|
| 189 | + $merge_tags = $email->get_merge_tags(); |
|
| 190 | + |
|
| 191 | + $result = $mailer->send( |
|
| 192 | + apply_filters( 'getpaid_invoice_email_recipients', wpinv_parse_list( $recipients ), $email ), |
|
| 193 | + $email->add_merge_tags( $email->get_subject(), $merge_tags ), |
|
| 194 | + $email->get_content( $merge_tags, $extra_args ), |
|
| 195 | + $email->get_attachments() |
|
| 196 | + ); |
|
| 197 | + |
|
| 198 | + // Maybe send a copy to the admin. |
|
| 199 | + if ( $email->include_admin_bcc() ) { |
|
| 200 | + $mailer->send( |
|
| 201 | + wpinv_get_admin_email(), |
|
| 202 | + $email->add_merge_tags( $email->get_subject() . __( ' - ADMIN BCC COPY', 'invoicing' ), $merge_tags ), |
|
| 203 | + $email->get_content( $merge_tags ), |
|
| 204 | + $email->get_attachments() |
|
| 205 | + ); |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + if ( $result ) { |
|
| 209 | + $invoice->add_system_note( |
|
| 210 | + sprintf( |
|
| 211 | + __( 'Successfully sent %s notification email to %s.', 'invoicing' ), |
|
| 212 | + sanitize_key( $type ), |
|
| 213 | + $email->is_admin_email() ? __( 'admin' ) : __( 'the customer' ) |
|
| 214 | + ) |
|
| 215 | + ); |
|
| 216 | + } else { |
|
| 217 | + $invoice->add_system_note( |
|
| 218 | + sprintf( |
|
| 219 | + __( 'Failed sending %s notification email to %s.', 'invoicing' ), |
|
| 220 | + sanitize_key( $type ), |
|
| 221 | + $email->is_admin_email() ? __( 'admin' ) : __( 'the customer' ) |
|
| 222 | + ) |
|
| 223 | + ); |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + do_action( 'getpaid_after_send_invoice_notification', $type, $invoice, $email ); |
|
| 227 | + |
|
| 228 | + return $result; |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + /** |
|
| 232 | + * Also send emails to any cc users. |
|
| 233 | + * |
|
| 234 | + * @param array $recipients |
|
| 235 | + * @param GetPaid_Notification_Email $email |
|
| 236 | + */ |
|
| 237 | + public function filter_email_recipients( $recipients, $email ) { |
|
| 238 | + |
|
| 239 | + if ( ! $email->is_admin_email() ) { |
|
| 240 | + $cc = $email->object->get_email_cc(); |
|
| 241 | + $cc_2 = get_user_meta( $email->object->get_user_id(), '_wpinv_email_cc', true ); |
|
| 242 | + |
|
| 243 | + if ( ! empty( $cc ) ) { |
|
| 244 | + $cc = array_map( 'sanitize_email', wpinv_parse_list( $cc ) ); |
|
| 245 | + $recipients = array_filter( array_unique( array_merge( $recipients, $cc ) ) ); |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + if ( ! empty( $cc_2 ) ) { |
|
| 249 | + $cc_2 = array_map( 'sanitize_email', wpinv_parse_list( $cc_2 ) ); |
|
| 250 | + $recipients = array_filter( array_unique( array_merge( $recipients, $cc_2 ) ) ); |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + return $recipients; |
|
| 256 | + |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + /** |
|
| 260 | + * Sends a new invoice notification. |
|
| 261 | + * |
|
| 262 | + * @param WPInv_Invoice $invoice |
|
| 263 | + */ |
|
| 264 | + public function new_invoice( $invoice ) { |
|
| 265 | + |
|
| 266 | + // Only send this email for invoices created via the admin page. |
|
| 267 | + if ( ! $invoice->is_type( 'invoice' ) || $invoice->is_paid() || $this->is_payment_form_invoice( $invoice->get_id() ) ) { |
|
| 268 | + return; |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 272 | + $recipient = wpinv_get_admin_email(); |
|
| 273 | + |
|
| 274 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 275 | + |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + /** |
|
| 279 | + * Sends a cancelled invoice notification. |
|
| 280 | + * |
|
| 281 | + * @param WPInv_Invoice $invoice |
|
| 282 | + */ |
|
| 283 | + public function cancelled_invoice( $invoice ) { |
|
| 284 | + |
|
| 285 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 286 | + $recipient = $invoice->get_email(); |
|
| 287 | + |
|
| 288 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + /** |
|
| 292 | + * Sends a failed invoice notification. |
|
| 293 | + * |
|
| 294 | + * @param WPInv_Invoice $invoice |
|
| 295 | + */ |
|
| 296 | + public function failed_invoice( $invoice ) { |
|
| 297 | + |
|
| 298 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 299 | + $recipient = wpinv_get_admin_email(); |
|
| 300 | + |
|
| 301 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 302 | + |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + /** |
|
| 306 | + * Sends a notification whenever an invoice is put on hold. |
|
| 307 | + * |
|
| 308 | + * @param WPInv_Invoice $invoice |
|
| 309 | + */ |
|
| 310 | + public function onhold_invoice( $invoice ) { |
|
| 311 | + |
|
| 312 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 313 | + $recipient = $invoice->get_email(); |
|
| 314 | + |
|
| 315 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 316 | 316 | |
| 317 | - } |
|
| 317 | + } |
|
| 318 | 318 | |
| 319 | - /** |
|
| 320 | - * Sends a notification whenever an invoice is marked as processing payment. |
|
| 321 | - * |
|
| 322 | - * @param WPInv_Invoice $invoice |
|
| 323 | - */ |
|
| 324 | - public function processing_invoice( $invoice ) { |
|
| 319 | + /** |
|
| 320 | + * Sends a notification whenever an invoice is marked as processing payment. |
|
| 321 | + * |
|
| 322 | + * @param WPInv_Invoice $invoice |
|
| 323 | + */ |
|
| 324 | + public function processing_invoice( $invoice ) { |
|
| 325 | 325 | |
| 326 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 327 | - $recipient = $invoice->get_email(); |
|
| 328 | - |
|
| 329 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 330 | - |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - /** |
|
| 334 | - * Sends a notification whenever an invoice is paid. |
|
| 335 | - * |
|
| 336 | - * @param WPInv_Invoice $invoice |
|
| 337 | - */ |
|
| 338 | - public function completed_invoice( $invoice ) { |
|
| 326 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 327 | + $recipient = $invoice->get_email(); |
|
| 328 | + |
|
| 329 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 330 | + |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + /** |
|
| 334 | + * Sends a notification whenever an invoice is paid. |
|
| 335 | + * |
|
| 336 | + * @param WPInv_Invoice $invoice |
|
| 337 | + */ |
|
| 338 | + public function completed_invoice( $invoice ) { |
|
| 339 | 339 | |
| 340 | - // (Maybe) abort if it is a renewal invoice. |
|
| 341 | - if ( $invoice->is_renewal() && ! wpinv_get_option( 'email_completed_invoice_renewal_active', false ) ) { |
|
| 342 | - return; |
|
| 343 | - } |
|
| 340 | + // (Maybe) abort if it is a renewal invoice. |
|
| 341 | + if ( $invoice->is_renewal() && ! wpinv_get_option( 'email_completed_invoice_renewal_active', false ) ) { |
|
| 342 | + return; |
|
| 343 | + } |
|
| 344 | 344 | |
| 345 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 346 | - $recipient = $invoice->get_email(); |
|
| 345 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 346 | + $recipient = $invoice->get_email(); |
|
| 347 | 347 | |
| 348 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 348 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 349 | 349 | |
| 350 | - } |
|
| 350 | + } |
|
| 351 | 351 | |
| 352 | - /** |
|
| 353 | - * Sends a notification whenever an invoice is refunded. |
|
| 354 | - * |
|
| 355 | - * @param WPInv_Invoice $invoice |
|
| 356 | - */ |
|
| 357 | - public function refunded_invoice( $invoice ) { |
|
| 352 | + /** |
|
| 353 | + * Sends a notification whenever an invoice is refunded. |
|
| 354 | + * |
|
| 355 | + * @param WPInv_Invoice $invoice |
|
| 356 | + */ |
|
| 357 | + public function refunded_invoice( $invoice ) { |
|
| 358 | 358 | |
| 359 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 360 | - $recipient = $invoice->get_email(); |
|
| 361 | - |
|
| 362 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 359 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 360 | + $recipient = $invoice->get_email(); |
|
| 361 | + |
|
| 362 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 363 | 363 | |
| 364 | - } |
|
| 364 | + } |
|
| 365 | 365 | |
| 366 | - /** |
|
| 367 | - * Notifies a user about new invoices |
|
| 368 | - * |
|
| 369 | - * @param WPInv_Invoice $invoice |
|
| 370 | - * @param bool $force |
|
| 371 | - */ |
|
| 372 | - public function user_invoice( $invoice, $force = false ) { |
|
| 366 | + /** |
|
| 367 | + * Notifies a user about new invoices |
|
| 368 | + * |
|
| 369 | + * @param WPInv_Invoice $invoice |
|
| 370 | + * @param bool $force |
|
| 371 | + */ |
|
| 372 | + public function user_invoice( $invoice, $force = false ) { |
|
| 373 | 373 | |
| 374 | - if ( ! $force && ! empty( $GLOBALS['wpinv_skip_invoice_notification'] ) ) { |
|
| 375 | - return; |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - // Only send this email for invoices created via the admin page. |
|
| 379 | - if ( ! $invoice->is_type( 'invoice' ) || ( empty( $force ) && $invoice->is_paid() ) || ( empty( $force ) && $this->is_payment_form_invoice( $invoice->get_id() ) ) ) { |
|
| 380 | - return; |
|
| 381 | - } |
|
| 374 | + if ( ! $force && ! empty( $GLOBALS['wpinv_skip_invoice_notification'] ) ) { |
|
| 375 | + return; |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + // Only send this email for invoices created via the admin page. |
|
| 379 | + if ( ! $invoice->is_type( 'invoice' ) || ( empty( $force ) && $invoice->is_paid() ) || ( empty( $force ) && $this->is_payment_form_invoice( $invoice->get_id() ) ) ) { |
|
| 380 | + return; |
|
| 381 | + } |
|
| 382 | 382 | |
| 383 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 384 | - $recipient = $invoice->get_email(); |
|
| 385 | - |
|
| 386 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 383 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 384 | + $recipient = $invoice->get_email(); |
|
| 385 | + |
|
| 386 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 387 | 387 | |
| 388 | - } |
|
| 389 | - |
|
| 390 | - /** |
|
| 391 | - * Checks if an invoice is a payment form invoice. |
|
| 392 | - * |
|
| 393 | - * @param int $invoice |
|
| 394 | - * @return bool |
|
| 395 | - */ |
|
| 396 | - public function is_payment_form_invoice( $invoice ) { |
|
| 397 | - $is_payment_form_invoice = empty( $_GET['getpaid-admin-action'] ) && ( 'payment_form' == get_post_meta( $invoice, 'wpinv_created_via', true ) || 'geodirectory' == get_post_meta( $invoice, 'wpinv_created_via', true ) ); |
|
| 398 | - return apply_filters( 'getpaid_invoice_notifications_is_payment_form_invoice', $is_payment_form_invoice, $invoice ); |
|
| 399 | - } |
|
| 388 | + } |
|
| 389 | + |
|
| 390 | + /** |
|
| 391 | + * Checks if an invoice is a payment form invoice. |
|
| 392 | + * |
|
| 393 | + * @param int $invoice |
|
| 394 | + * @return bool |
|
| 395 | + */ |
|
| 396 | + public function is_payment_form_invoice( $invoice ) { |
|
| 397 | + $is_payment_form_invoice = empty( $_GET['getpaid-admin-action'] ) && ( 'payment_form' == get_post_meta( $invoice, 'wpinv_created_via', true ) || 'geodirectory' == get_post_meta( $invoice, 'wpinv_created_via', true ) ); |
|
| 398 | + return apply_filters( 'getpaid_invoice_notifications_is_payment_form_invoice', $is_payment_form_invoice, $invoice ); |
|
| 399 | + } |
|
| 400 | 400 | |
| 401 | - /** |
|
| 402 | - * Notifies admin about new invoice notes |
|
| 403 | - * |
|
| 404 | - * @param WPInv_Invoice $invoice |
|
| 405 | - * @param string $note |
|
| 406 | - */ |
|
| 407 | - public function user_note( $invoice, $note ) { |
|
| 408 | - |
|
| 409 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 410 | - $recipient = $invoice->get_email(); |
|
| 401 | + /** |
|
| 402 | + * Notifies admin about new invoice notes |
|
| 403 | + * |
|
| 404 | + * @param WPInv_Invoice $invoice |
|
| 405 | + * @param string $note |
|
| 406 | + */ |
|
| 407 | + public function user_note( $invoice, $note ) { |
|
| 408 | + |
|
| 409 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 410 | + $recipient = $invoice->get_email(); |
|
| 411 | 411 | |
| 412 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient, array( 'customer_note' => $note ) ); |
|
| 413 | - |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - /** |
|
| 417 | - * (Force) Sends overdue notices. |
|
| 418 | - * |
|
| 419 | - * @param WPInv_Invoice $invoice |
|
| 420 | - */ |
|
| 421 | - public function force_send_overdue_notice( $invoice ) { |
|
| 422 | - $email = new GetPaid_Notification_Email( 'overdue', $invoice ); |
|
| 423 | - return $this->send_email( $invoice, $email, 'overdue', $invoice->get_email() ); |
|
| 424 | - } |
|
| 425 | - |
|
| 426 | - /** |
|
| 427 | - * Sends overdue notices. |
|
| 428 | - * |
|
| 429 | - * @TODO: Create an invoices query class. |
|
| 430 | - */ |
|
| 431 | - public function overdue() { |
|
| 432 | - global $wpdb; |
|
| 433 | - |
|
| 434 | - $email = new GetPaid_Notification_Email( __FUNCTION__ ); |
|
| 435 | - |
|
| 436 | - // Fetch reminder days. |
|
| 437 | - $reminder_days = array_unique( wp_parse_id_list( $email->get_option( 'days' ) ) ); |
|
| 438 | - |
|
| 439 | - // Abort if non is set. |
|
| 440 | - if ( empty( $reminder_days ) ) { |
|
| 441 | - return; |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - // Retrieve date query. |
|
| 445 | - $date_query = $this->get_date_query( $reminder_days ); |
|
| 446 | - |
|
| 447 | - // Invoices table. |
|
| 448 | - $table = $wpdb->prefix . 'getpaid_invoices'; |
|
| 449 | - |
|
| 450 | - // Fetch invoices. |
|
| 451 | - $invoices = $wpdb->get_col( |
|
| 452 | - "SELECT posts.ID FROM $wpdb->posts as posts |
|
| 412 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient, array( 'customer_note' => $note ) ); |
|
| 413 | + |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + /** |
|
| 417 | + * (Force) Sends overdue notices. |
|
| 418 | + * |
|
| 419 | + * @param WPInv_Invoice $invoice |
|
| 420 | + */ |
|
| 421 | + public function force_send_overdue_notice( $invoice ) { |
|
| 422 | + $email = new GetPaid_Notification_Email( 'overdue', $invoice ); |
|
| 423 | + return $this->send_email( $invoice, $email, 'overdue', $invoice->get_email() ); |
|
| 424 | + } |
|
| 425 | + |
|
| 426 | + /** |
|
| 427 | + * Sends overdue notices. |
|
| 428 | + * |
|
| 429 | + * @TODO: Create an invoices query class. |
|
| 430 | + */ |
|
| 431 | + public function overdue() { |
|
| 432 | + global $wpdb; |
|
| 433 | + |
|
| 434 | + $email = new GetPaid_Notification_Email( __FUNCTION__ ); |
|
| 435 | + |
|
| 436 | + // Fetch reminder days. |
|
| 437 | + $reminder_days = array_unique( wp_parse_id_list( $email->get_option( 'days' ) ) ); |
|
| 438 | + |
|
| 439 | + // Abort if non is set. |
|
| 440 | + if ( empty( $reminder_days ) ) { |
|
| 441 | + return; |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + // Retrieve date query. |
|
| 445 | + $date_query = $this->get_date_query( $reminder_days ); |
|
| 446 | + |
|
| 447 | + // Invoices table. |
|
| 448 | + $table = $wpdb->prefix . 'getpaid_invoices'; |
|
| 449 | + |
|
| 450 | + // Fetch invoices. |
|
| 451 | + $invoices = $wpdb->get_col( |
|
| 452 | + "SELECT posts.ID FROM $wpdb->posts as posts |
|
| 453 | 453 | LEFT JOIN $table as invoices ON invoices.post_id = posts.ID |
| 454 | 454 | WHERE posts.post_type = 'wpi_invoice' AND posts.post_status = 'wpi-pending' $date_query"); |
| 455 | 455 | |
| 456 | - foreach ( $invoices as $invoice ) { |
|
| 456 | + foreach ( $invoices as $invoice ) { |
|
| 457 | 457 | |
| 458 | - // Only send this email for invoices created via the admin page. |
|
| 459 | - if ( ! $this->is_payment_form_invoice( $invoice ) ) { |
|
| 460 | - $invoice = new WPInv_Invoice( $invoice ); |
|
| 461 | - $email->object = $invoice; |
|
| 458 | + // Only send this email for invoices created via the admin page. |
|
| 459 | + if ( ! $this->is_payment_form_invoice( $invoice ) ) { |
|
| 460 | + $invoice = new WPInv_Invoice( $invoice ); |
|
| 461 | + $email->object = $invoice; |
|
| 462 | 462 | |
| 463 | - if ( $invoice->needs_payment() ) { |
|
| 464 | - $this->send_email( $invoice, $email, __FUNCTION__, $invoice->get_email() ); |
|
| 465 | - } |
|
| 463 | + if ( $invoice->needs_payment() ) { |
|
| 464 | + $this->send_email( $invoice, $email, __FUNCTION__, $invoice->get_email() ); |
|
| 465 | + } |
|
| 466 | 466 | |
| 467 | - } |
|
| 467 | + } |
|
| 468 | 468 | |
| 469 | - } |
|
| 469 | + } |
|
| 470 | 470 | |
| 471 | - } |
|
| 471 | + } |
|
| 472 | 472 | |
| 473 | - /** |
|
| 474 | - * Calculates the date query for an invoices query |
|
| 475 | - * |
|
| 476 | - * @param array $reminder_days |
|
| 477 | - * @return string |
|
| 478 | - */ |
|
| 479 | - public function get_date_query( $reminder_days ) { |
|
| 473 | + /** |
|
| 474 | + * Calculates the date query for an invoices query |
|
| 475 | + * |
|
| 476 | + * @param array $reminder_days |
|
| 477 | + * @return string |
|
| 478 | + */ |
|
| 479 | + public function get_date_query( $reminder_days ) { |
|
| 480 | 480 | |
| 481 | - $date_query = array( |
|
| 482 | - 'relation' => 'OR' |
|
| 483 | - ); |
|
| 481 | + $date_query = array( |
|
| 482 | + 'relation' => 'OR' |
|
| 483 | + ); |
|
| 484 | 484 | |
| 485 | - foreach ( $reminder_days as $days ) { |
|
| 486 | - $date = date_parse( date( 'Y-m-d', strtotime( "-$days days", current_time( 'timestamp' ) ) ) ); |
|
| 485 | + foreach ( $reminder_days as $days ) { |
|
| 486 | + $date = date_parse( date( 'Y-m-d', strtotime( "-$days days", current_time( 'timestamp' ) ) ) ); |
|
| 487 | 487 | |
| 488 | - $date_query[] = array( |
|
| 489 | - 'year' => $date['year'], |
|
| 490 | - 'month' => $date['month'], |
|
| 491 | - 'day' => $date['day'], |
|
| 492 | - ); |
|
| 488 | + $date_query[] = array( |
|
| 489 | + 'year' => $date['year'], |
|
| 490 | + 'month' => $date['month'], |
|
| 491 | + 'day' => $date['day'], |
|
| 492 | + ); |
|
| 493 | 493 | |
| 494 | - } |
|
| 494 | + } |
|
| 495 | 495 | |
| 496 | - $date_query = new WP_Date_Query( $date_query, 'invoices.due_date' ); |
|
| 496 | + $date_query = new WP_Date_Query( $date_query, 'invoices.due_date' ); |
|
| 497 | 497 | |
| 498 | - return $date_query->get_sql(); |
|
| 498 | + return $date_query->get_sql(); |
|
| 499 | 499 | |
| 500 | - } |
|
| 500 | + } |
|
| 501 | 501 | |
| 502 | 502 | } |
@@ -42,94 +42,94 @@ discard block |
||
| 42 | 42 | <tr class="wpinv-item wpinv-item-<?php echo $invoice_status = $invoice->get_status(); ?>"> |
| 43 | 43 | <?php |
| 44 | 44 | |
| 45 | - foreach ( wpinv_get_user_invoices_columns( $post_type ) as $column_id => $column_name ) : |
|
| 45 | + foreach ( wpinv_get_user_invoices_columns( $post_type ) as $column_id => $column_name ) : |
|
| 46 | 46 | |
| 47 | - $column_id = sanitize_html_class( $column_id ); |
|
| 48 | - $class = empty( $column_name['class'] ) ? '' : sanitize_html_class( $column_name['class'] ); |
|
| 47 | + $column_id = sanitize_html_class( $column_id ); |
|
| 48 | + $class = empty( $column_name['class'] ) ? '' : sanitize_html_class( $column_name['class'] ); |
|
| 49 | 49 | |
| 50 | - echo "<td class='$column_id $class'>"; |
|
| 51 | - switch ( $column_id ) { |
|
| 50 | + echo "<td class='$column_id $class'>"; |
|
| 51 | + switch ( $column_id ) { |
|
| 52 | 52 | |
| 53 | - case 'invoice-number': |
|
| 54 | - echo wpinv_invoice_link( $invoice ); |
|
| 55 | - break; |
|
| 53 | + case 'invoice-number': |
|
| 54 | + echo wpinv_invoice_link( $invoice ); |
|
| 55 | + break; |
|
| 56 | 56 | |
| 57 | - case 'created-date': |
|
| 58 | - echo getpaid_format_date_value( $invoice->get_date_created() ); |
|
| 59 | - break; |
|
| 57 | + case 'created-date': |
|
| 58 | + echo getpaid_format_date_value( $invoice->get_date_created() ); |
|
| 59 | + break; |
|
| 60 | 60 | |
| 61 | - case 'payment-date': |
|
| 61 | + case 'payment-date': |
|
| 62 | 62 | |
| 63 | - if ( $invoice->needs_payment() ) { |
|
| 64 | - echo "—"; |
|
| 65 | - } else { |
|
| 66 | - echo getpaid_format_date_value( $invoice->get_date_completed() ); |
|
| 67 | - } |
|
| 63 | + if ( $invoice->needs_payment() ) { |
|
| 64 | + echo "—"; |
|
| 65 | + } else { |
|
| 66 | + echo getpaid_format_date_value( $invoice->get_date_completed() ); |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - break; |
|
| 69 | + break; |
|
| 70 | 70 | |
| 71 | - case 'invoice-status': |
|
| 72 | - echo $invoice->get_status_label_html(); |
|
| 71 | + case 'invoice-status': |
|
| 72 | + echo $invoice->get_status_label_html(); |
|
| 73 | 73 | |
| 74 | - break; |
|
| 74 | + break; |
|
| 75 | 75 | |
| 76 | - case 'invoice-total': |
|
| 77 | - echo wpinv_price( $invoice->get_total(), $invoice->get_currency() ); |
|
| 76 | + case 'invoice-total': |
|
| 77 | + echo wpinv_price( $invoice->get_total(), $invoice->get_currency() ); |
|
| 78 | 78 | |
| 79 | - break; |
|
| 79 | + break; |
|
| 80 | 80 | |
| 81 | - case 'invoice-actions': |
|
| 81 | + case 'invoice-actions': |
|
| 82 | 82 | |
| 83 | - $actions = array( |
|
| 83 | + $actions = array( |
|
| 84 | 84 | |
| 85 | - 'pay' => array( |
|
| 86 | - 'url' => $invoice->get_checkout_payment_url(), |
|
| 87 | - 'name' => __( 'Pay Now', 'invoicing' ), |
|
| 88 | - 'class' => 'btn-success' |
|
| 89 | - ), |
|
| 85 | + 'pay' => array( |
|
| 86 | + 'url' => $invoice->get_checkout_payment_url(), |
|
| 87 | + 'name' => __( 'Pay Now', 'invoicing' ), |
|
| 88 | + 'class' => 'btn-success' |
|
| 89 | + ), |
|
| 90 | 90 | |
| 91 | - 'print' => array( |
|
| 92 | - 'url' => $invoice->get_view_url(), |
|
| 93 | - 'name' => __( 'View', 'invoicing' ), |
|
| 94 | - 'class' => 'btn-secondary', |
|
| 95 | - 'attrs' => 'target="_blank"' |
|
| 96 | - ) |
|
| 97 | - ); |
|
| 91 | + 'print' => array( |
|
| 92 | + 'url' => $invoice->get_view_url(), |
|
| 93 | + 'name' => __( 'View', 'invoicing' ), |
|
| 94 | + 'class' => 'btn-secondary', |
|
| 95 | + 'attrs' => 'target="_blank"' |
|
| 96 | + ) |
|
| 97 | + ); |
|
| 98 | 98 | |
| 99 | - if ( ! $invoice->needs_payment() ) { |
|
| 100 | - unset( $actions['pay'] ); |
|
| 101 | - } |
|
| 99 | + if ( ! $invoice->needs_payment() ) { |
|
| 100 | + unset( $actions['pay'] ); |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - if ( $invoice->needs_payment() ) { |
|
| 104 | - $actions['delete'] = array( |
|
| 105 | - 'url' => getpaid_get_authenticated_action_url( 'delete_invoice', add_query_arg( 'invoice_id', $invoice->get_id() ) ), |
|
| 106 | - 'name' => __( 'Delete', 'invoicing' ), |
|
| 107 | - 'class' => 'btn-danger' |
|
| 108 | - ); |
|
| 109 | - } |
|
| 103 | + if ( $invoice->needs_payment() ) { |
|
| 104 | + $actions['delete'] = array( |
|
| 105 | + 'url' => getpaid_get_authenticated_action_url( 'delete_invoice', add_query_arg( 'invoice_id', $invoice->get_id() ) ), |
|
| 106 | + 'name' => __( 'Delete', 'invoicing' ), |
|
| 107 | + 'class' => 'btn-danger' |
|
| 108 | + ); |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - $actions = apply_filters( 'wpinv_user_invoices_actions', $actions, $invoice, $post_type ); |
|
| 111 | + $actions = apply_filters( 'wpinv_user_invoices_actions', $actions, $invoice, $post_type ); |
|
| 112 | 112 | |
| 113 | - foreach ( $actions as $key => $action ) { |
|
| 114 | - $class = !empty($action['class']) ? sanitize_html_class($action['class']) : ''; |
|
| 115 | - echo '<a href="' . esc_url( $action['url'] ) . '" class="btn btn-sm btn-block ' . $class . ' ' . sanitize_html_class( $key ) . '" ' . ( !empty($action['attrs']) ? $action['attrs'] : '' ) . '>' . $action['name'] . '</a>'; |
|
| 116 | - } |
|
| 113 | + foreach ( $actions as $key => $action ) { |
|
| 114 | + $class = !empty($action['class']) ? sanitize_html_class($action['class']) : ''; |
|
| 115 | + echo '<a href="' . esc_url( $action['url'] ) . '" class="btn btn-sm btn-block ' . $class . ' ' . sanitize_html_class( $key ) . '" ' . ( !empty($action['attrs']) ? $action['attrs'] : '' ) . '>' . $action['name'] . '</a>'; |
|
| 116 | + } |
|
| 117 | 117 | |
| 118 | - break; |
|
| 118 | + break; |
|
| 119 | 119 | |
| 120 | - default: |
|
| 121 | - do_action( "wpinv_user_invoices_column_$column_id", $invoice ); |
|
| 122 | - break; |
|
| 120 | + default: |
|
| 121 | + do_action( "wpinv_user_invoices_column_$column_id", $invoice ); |
|
| 122 | + break; |
|
| 123 | 123 | |
| 124 | 124 | |
| 125 | - } |
|
| 125 | + } |
|
| 126 | 126 | |
| 127 | - do_action( "wpinv_user_invoices_column_after_$column_id", $invoice ); |
|
| 127 | + do_action( "wpinv_user_invoices_column_after_$column_id", $invoice ); |
|
| 128 | 128 | |
| 129 | - echo '</td>'; |
|
| 129 | + echo '</td>'; |
|
| 130 | 130 | |
| 131 | - endforeach; |
|
| 132 | - ?> |
|
| 131 | + endforeach; |
|
| 132 | + ?> |
|
| 133 | 133 | </tr> |
| 134 | 134 | |
| 135 | 135 | <?php endforeach; ?> |
@@ -143,14 +143,14 @@ discard block |
||
| 143 | 143 | <?php if ( 1 < $invoices->max_num_pages ) : ?> |
| 144 | 144 | <div class="invoicing-Pagination"> |
| 145 | 145 | <?php |
| 146 | - $big = 999999; |
|
| 147 | - |
|
| 148 | - echo paginate_links( array( |
|
| 149 | - 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ), |
|
| 150 | - 'format' => '?paged=%#%', |
|
| 151 | - 'total' => $invoices->max_num_pages, |
|
| 152 | - ) ); |
|
| 153 | - ?> |
|
| 146 | + $big = 999999; |
|
| 147 | + |
|
| 148 | + echo paginate_links( array( |
|
| 149 | + 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ), |
|
| 150 | + 'format' => '?paged=%#%', |
|
| 151 | + 'total' => $invoices->max_num_pages, |
|
| 152 | + ) ); |
|
| 153 | + ?> |
|
| 154 | 154 | </div> |
| 155 | 155 | <?php endif; ?> |
| 156 | 156 | |
@@ -15,125 +15,125 @@ discard block |
||
| 15 | 15 | */ |
| 16 | 16 | class WPInv_Subscription extends GetPaid_Data { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Which data store to load. |
|
| 20 | - * |
|
| 21 | - * @var string |
|
| 22 | - */ |
|
| 23 | - protected $data_store_name = 'subscription'; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * This is the name of this object type. |
|
| 27 | - * |
|
| 28 | - * @var string |
|
| 29 | - */ |
|
| 30 | - protected $object_type = 'subscription'; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Item Data array. This is the core item data exposed in APIs. |
|
| 34 | - * |
|
| 35 | - * @since 1.0.19 |
|
| 36 | - * @var array |
|
| 37 | - */ |
|
| 38 | - protected $data = array( |
|
| 39 | - 'customer_id' => 0, |
|
| 40 | - 'frequency' => 1, |
|
| 41 | - 'period' => 'D', |
|
| 42 | - 'initial_amount' => null, |
|
| 43 | - 'recurring_amount' => null, |
|
| 44 | - 'bill_times' => 0, |
|
| 45 | - 'transaction_id' => '', |
|
| 46 | - 'parent_payment_id' => null, |
|
| 47 | - 'product_id' => 0, |
|
| 48 | - 'created' => '0000-00-00 00:00:00', |
|
| 49 | - 'expiration' => '0000-00-00 00:00:00', |
|
| 50 | - 'trial_period' => '', |
|
| 51 | - 'status' => 'pending', |
|
| 52 | - 'profile_id' => '', |
|
| 53 | - 'gateway' => '', |
|
| 54 | - 'customer' => '', |
|
| 55 | - ); |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Stores the status transition information. |
|
| 59 | - * |
|
| 60 | - * @since 1.0.19 |
|
| 61 | - * @var bool |
|
| 62 | - */ |
|
| 63 | - protected $status_transition = false; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Get the subscription if ID is passed, otherwise the subscription is new and empty. |
|
| 67 | - * |
|
| 68 | - * @param int|string|object|WPInv_Subscription $subscription Subscription id, profile_id, or object to read. |
|
| 69 | - * @param bool $deprecated |
|
| 70 | - */ |
|
| 71 | - function __construct( $subscription = 0, $deprecated = false ) { |
|
| 72 | - |
|
| 73 | - parent::__construct( $subscription ); |
|
| 74 | - |
|
| 75 | - if ( ! $deprecated && ! empty( $subscription ) && is_numeric( $subscription ) ) { |
|
| 76 | - $this->set_id( $subscription ); |
|
| 77 | - } elseif ( $subscription instanceof self ) { |
|
| 78 | - $this->set_id( $subscription->get_id() ); |
|
| 79 | - } elseif ( $deprecated && $subscription_id = self::get_subscription_id_by_field( $subscription, 'profile_id' ) ) { |
|
| 80 | - $this->set_id( $subscription_id ); |
|
| 81 | - } elseif ( ! empty( $subscription->id ) ) { |
|
| 82 | - $this->set_id( $subscription->id ); |
|
| 83 | - } else { |
|
| 84 | - $this->set_object_read( true ); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - // Load the datastore. |
|
| 88 | - $this->data_store = GetPaid_Data_Store::load( $this->data_store_name ); |
|
| 89 | - |
|
| 90 | - if ( $this->get_id() > 0 ) { |
|
| 91 | - $this->data_store->read( $this ); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Given an invoice id, profile id, transaction id, it returns the subscription's id. |
|
| 98 | - * |
|
| 99 | - * |
|
| 100 | - * @static |
|
| 101 | - * @param string $value |
|
| 102 | - * @param string $field Either invoice_id, transaction_id or profile_id. |
|
| 103 | - * @since 1.0.19 |
|
| 104 | - * @return int |
|
| 105 | - */ |
|
| 106 | - public static function get_subscription_id_by_field( $value, $field = 'profile_id' ) { |
|
| 18 | + /** |
|
| 19 | + * Which data store to load. |
|
| 20 | + * |
|
| 21 | + * @var string |
|
| 22 | + */ |
|
| 23 | + protected $data_store_name = 'subscription'; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * This is the name of this object type. |
|
| 27 | + * |
|
| 28 | + * @var string |
|
| 29 | + */ |
|
| 30 | + protected $object_type = 'subscription'; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Item Data array. This is the core item data exposed in APIs. |
|
| 34 | + * |
|
| 35 | + * @since 1.0.19 |
|
| 36 | + * @var array |
|
| 37 | + */ |
|
| 38 | + protected $data = array( |
|
| 39 | + 'customer_id' => 0, |
|
| 40 | + 'frequency' => 1, |
|
| 41 | + 'period' => 'D', |
|
| 42 | + 'initial_amount' => null, |
|
| 43 | + 'recurring_amount' => null, |
|
| 44 | + 'bill_times' => 0, |
|
| 45 | + 'transaction_id' => '', |
|
| 46 | + 'parent_payment_id' => null, |
|
| 47 | + 'product_id' => 0, |
|
| 48 | + 'created' => '0000-00-00 00:00:00', |
|
| 49 | + 'expiration' => '0000-00-00 00:00:00', |
|
| 50 | + 'trial_period' => '', |
|
| 51 | + 'status' => 'pending', |
|
| 52 | + 'profile_id' => '', |
|
| 53 | + 'gateway' => '', |
|
| 54 | + 'customer' => '', |
|
| 55 | + ); |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Stores the status transition information. |
|
| 59 | + * |
|
| 60 | + * @since 1.0.19 |
|
| 61 | + * @var bool |
|
| 62 | + */ |
|
| 63 | + protected $status_transition = false; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Get the subscription if ID is passed, otherwise the subscription is new and empty. |
|
| 67 | + * |
|
| 68 | + * @param int|string|object|WPInv_Subscription $subscription Subscription id, profile_id, or object to read. |
|
| 69 | + * @param bool $deprecated |
|
| 70 | + */ |
|
| 71 | + function __construct( $subscription = 0, $deprecated = false ) { |
|
| 72 | + |
|
| 73 | + parent::__construct( $subscription ); |
|
| 74 | + |
|
| 75 | + if ( ! $deprecated && ! empty( $subscription ) && is_numeric( $subscription ) ) { |
|
| 76 | + $this->set_id( $subscription ); |
|
| 77 | + } elseif ( $subscription instanceof self ) { |
|
| 78 | + $this->set_id( $subscription->get_id() ); |
|
| 79 | + } elseif ( $deprecated && $subscription_id = self::get_subscription_id_by_field( $subscription, 'profile_id' ) ) { |
|
| 80 | + $this->set_id( $subscription_id ); |
|
| 81 | + } elseif ( ! empty( $subscription->id ) ) { |
|
| 82 | + $this->set_id( $subscription->id ); |
|
| 83 | + } else { |
|
| 84 | + $this->set_object_read( true ); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + // Load the datastore. |
|
| 88 | + $this->data_store = GetPaid_Data_Store::load( $this->data_store_name ); |
|
| 89 | + |
|
| 90 | + if ( $this->get_id() > 0 ) { |
|
| 91 | + $this->data_store->read( $this ); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Given an invoice id, profile id, transaction id, it returns the subscription's id. |
|
| 98 | + * |
|
| 99 | + * |
|
| 100 | + * @static |
|
| 101 | + * @param string $value |
|
| 102 | + * @param string $field Either invoice_id, transaction_id or profile_id. |
|
| 103 | + * @since 1.0.19 |
|
| 104 | + * @return int |
|
| 105 | + */ |
|
| 106 | + public static function get_subscription_id_by_field( $value, $field = 'profile_id' ) { |
|
| 107 | 107 | global $wpdb; |
| 108 | 108 | |
| 109 | - // Trim the value. |
|
| 110 | - $value = trim( $value ); |
|
| 109 | + // Trim the value. |
|
| 110 | + $value = trim( $value ); |
|
| 111 | 111 | |
| 112 | - if ( empty( $value ) ) { |
|
| 113 | - return 0; |
|
| 114 | - } |
|
| 112 | + if ( empty( $value ) ) { |
|
| 113 | + return 0; |
|
| 114 | + } |
|
| 115 | 115 | |
| 116 | - if ( 'invoice_id' == $field ) { |
|
| 117 | - $field = 'parent_payment_id'; |
|
| 118 | - } |
|
| 116 | + if ( 'invoice_id' == $field ) { |
|
| 117 | + $field = 'parent_payment_id'; |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | 120 | // Valid fields. |
| 121 | 121 | $fields = array( |
| 122 | - 'parent_payment_id', |
|
| 123 | - 'transaction_id', |
|
| 124 | - 'profile_id' |
|
| 125 | - ); |
|
| 126 | - |
|
| 127 | - // Ensure a field has been passed. |
|
| 128 | - if ( empty( $field ) || ! in_array( $field, $fields ) ) { |
|
| 129 | - return 0; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - // Maybe retrieve from the cache. |
|
| 133 | - $subscription_id = wp_cache_get( $value, "getpaid_subscription_{$field}s_to_subscription_ids" ); |
|
| 134 | - if ( ! empty( $subscription_id ) ) { |
|
| 135 | - return $subscription_id; |
|
| 136 | - } |
|
| 122 | + 'parent_payment_id', |
|
| 123 | + 'transaction_id', |
|
| 124 | + 'profile_id' |
|
| 125 | + ); |
|
| 126 | + |
|
| 127 | + // Ensure a field has been passed. |
|
| 128 | + if ( empty( $field ) || ! in_array( $field, $fields ) ) { |
|
| 129 | + return 0; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + // Maybe retrieve from the cache. |
|
| 133 | + $subscription_id = wp_cache_get( $value, "getpaid_subscription_{$field}s_to_subscription_ids" ); |
|
| 134 | + if ( ! empty( $subscription_id ) ) { |
|
| 135 | + return $subscription_id; |
|
| 136 | + } |
|
| 137 | 137 | |
| 138 | 138 | // Fetch from the db. |
| 139 | 139 | $table = $wpdb->prefix . 'wpinv_subscriptions'; |
@@ -141,34 +141,34 @@ discard block |
||
| 141 | 141 | $wpdb->prepare( "SELECT `id` FROM $table WHERE `$field`=%s LIMIT 1", $value ) |
| 142 | 142 | ); |
| 143 | 143 | |
| 144 | - if ( empty( $subscription_id ) ) { |
|
| 145 | - return 0; |
|
| 146 | - } |
|
| 144 | + if ( empty( $subscription_id ) ) { |
|
| 145 | + return 0; |
|
| 146 | + } |
|
| 147 | 147 | |
| 148 | - // Update the cache with our data. |
|
| 149 | - wp_cache_set( $value, $subscription_id, "getpaid_subscription_{$field}s_to_subscription_ids" ); |
|
| 148 | + // Update the cache with our data. |
|
| 149 | + wp_cache_set( $value, $subscription_id, "getpaid_subscription_{$field}s_to_subscription_ids" ); |
|
| 150 | 150 | |
| 151 | - return $subscription_id; |
|
| 152 | - } |
|
| 151 | + return $subscription_id; |
|
| 152 | + } |
|
| 153 | 153 | |
| 154 | - /** |
|
| 154 | + /** |
|
| 155 | 155 | * Clears the subscription's cache. |
| 156 | 156 | */ |
| 157 | 157 | public function clear_cache() { |
| 158 | - wp_cache_delete( $this->get_parent_payment_id(), 'getpaid_subscription_parent_payment_ids_to_subscription_ids' ); |
|
| 159 | - wp_cache_delete( $this->get_transaction_id(), 'getpaid_subscription_transaction_ids_to_subscription_ids' ); |
|
| 160 | - wp_cache_delete( $this->get_profile_id(), 'getpaid_subscription_profile_ids_to_subscription_ids' ); |
|
| 161 | - wp_cache_delete( $this->get_id(), 'getpaid_subscriptions' ); |
|
| 162 | - } |
|
| 158 | + wp_cache_delete( $this->get_parent_payment_id(), 'getpaid_subscription_parent_payment_ids_to_subscription_ids' ); |
|
| 159 | + wp_cache_delete( $this->get_transaction_id(), 'getpaid_subscription_transaction_ids_to_subscription_ids' ); |
|
| 160 | + wp_cache_delete( $this->get_profile_id(), 'getpaid_subscription_profile_ids_to_subscription_ids' ); |
|
| 161 | + wp_cache_delete( $this->get_id(), 'getpaid_subscriptions' ); |
|
| 162 | + } |
|
| 163 | 163 | |
| 164 | - /** |
|
| 164 | + /** |
|
| 165 | 165 | * Checks if a subscription key is set. |
| 166 | 166 | */ |
| 167 | 167 | public function _isset( $key ) { |
| 168 | 168 | return isset( $this->data[$key] ) || method_exists( $this, "get_$key" ); |
| 169 | - } |
|
| 169 | + } |
|
| 170 | 170 | |
| 171 | - /* |
|
| 171 | + /* |
|
| 172 | 172 | |-------------------------------------------------------------------------- |
| 173 | 173 | | CRUD methods |
| 174 | 174 | |-------------------------------------------------------------------------- |
@@ -177,545 +177,545 @@ discard block |
||
| 177 | 177 | | |
| 178 | 178 | */ |
| 179 | 179 | |
| 180 | - /* |
|
| 181 | - |-------------------------------------------------------------------------- |
|
| 182 | - | Getters |
|
| 183 | - |-------------------------------------------------------------------------- |
|
| 184 | - */ |
|
| 180 | + /* |
|
| 181 | + |-------------------------------------------------------------------------- |
|
| 182 | + | Getters |
|
| 183 | + |-------------------------------------------------------------------------- |
|
| 184 | + */ |
|
| 185 | + |
|
| 186 | + /** |
|
| 187 | + * Get customer id. |
|
| 188 | + * |
|
| 189 | + * @since 1.0.19 |
|
| 190 | + * @param string $context View or edit context. |
|
| 191 | + * @return int |
|
| 192 | + */ |
|
| 193 | + public function get_customer_id( $context = 'view' ) { |
|
| 194 | + return (int) $this->get_prop( 'customer_id', $context ); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * Get customer information. |
|
| 199 | + * |
|
| 200 | + * @since 1.0.19 |
|
| 201 | + * @param string $context View or edit context. |
|
| 202 | + * @return WP_User|false WP_User object on success, false on failure. |
|
| 203 | + */ |
|
| 204 | + public function get_customer( $context = 'view' ) { |
|
| 205 | + return get_userdata( $this->get_customer_id( $context ) ); |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * Get parent invoice id. |
|
| 210 | + * |
|
| 211 | + * @since 1.0.19 |
|
| 212 | + * @param string $context View or edit context. |
|
| 213 | + * @return int |
|
| 214 | + */ |
|
| 215 | + public function get_parent_invoice_id( $context = 'view' ) { |
|
| 216 | + return (int) $this->get_prop( 'parent_payment_id', $context ); |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * Alias for self::get_parent_invoice_id(). |
|
| 221 | + * |
|
| 222 | + * @since 1.0.19 |
|
| 223 | + * @param string $context View or edit context. |
|
| 224 | + * @return int |
|
| 225 | + */ |
|
| 226 | + public function get_parent_payment_id( $context = 'view' ) { |
|
| 227 | + return $this->get_parent_invoice_id( $context ); |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + /** |
|
| 231 | + * Alias for self::get_parent_invoice_id(). |
|
| 232 | + * |
|
| 233 | + * @since 1.0.0 |
|
| 234 | + * @return int |
|
| 235 | + */ |
|
| 236 | + public function get_original_payment_id( $context = 'view' ) { |
|
| 237 | + return $this->get_parent_invoice_id( $context ); |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + /** |
|
| 241 | + * Get parent invoice. |
|
| 242 | + * |
|
| 243 | + * @since 1.0.19 |
|
| 244 | + * @param string $context View or edit context. |
|
| 245 | + * @return WPInv_Invoice |
|
| 246 | + */ |
|
| 247 | + public function get_parent_invoice( $context = 'view' ) { |
|
| 248 | + return new WPInv_Invoice( $this->get_parent_invoice_id( $context ) ); |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + /** |
|
| 252 | + * Alias for self::get_parent_invoice(). |
|
| 253 | + * |
|
| 254 | + * @since 1.0.19 |
|
| 255 | + * @param string $context View or edit context. |
|
| 256 | + * @return WPInv_Invoice |
|
| 257 | + */ |
|
| 258 | + public function get_parent_payment( $context = 'view' ) { |
|
| 259 | + return $this->get_parent_invoice( $context ); |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + /** |
|
| 263 | + * Get subscription's product id. |
|
| 264 | + * |
|
| 265 | + * @since 1.0.19 |
|
| 266 | + * @param string $context View or edit context. |
|
| 267 | + * @return int |
|
| 268 | + */ |
|
| 269 | + public function get_product_id( $context = 'view' ) { |
|
| 270 | + return (int) $this->get_prop( 'product_id', $context ); |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + /** |
|
| 274 | + * Get the subscription product. |
|
| 275 | + * |
|
| 276 | + * @since 1.0.19 |
|
| 277 | + * @param string $context View or edit context. |
|
| 278 | + * @return WPInv_Item |
|
| 279 | + */ |
|
| 280 | + public function get_product( $context = 'view' ) { |
|
| 281 | + return new WPInv_Item( $this->get_product_id( $context ) ); |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + /** |
|
| 285 | + * Get parent invoice's gateway. |
|
| 286 | + * |
|
| 287 | + * Here for backwards compatibility. |
|
| 288 | + * |
|
| 289 | + * @since 1.0.19 |
|
| 290 | + * @param string $context View or edit context. |
|
| 291 | + * @return string |
|
| 292 | + */ |
|
| 293 | + public function get_gateway( $context = 'view' ) { |
|
| 294 | + return $this->get_parent_invoice( $context )->get_gateway(); |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + /** |
|
| 298 | + * Get the period of a renewal. |
|
| 299 | + * |
|
| 300 | + * @since 1.0.19 |
|
| 301 | + * @param string $context View or edit context. |
|
| 302 | + * @return string |
|
| 303 | + */ |
|
| 304 | + public function get_period( $context = 'view' ) { |
|
| 305 | + return $this->get_prop( 'period', $context ); |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + /** |
|
| 309 | + * Get number of periods each renewal is valid for. |
|
| 310 | + * |
|
| 311 | + * @since 1.0.19 |
|
| 312 | + * @param string $context View or edit context. |
|
| 313 | + * @return int |
|
| 314 | + */ |
|
| 315 | + public function get_frequency( $context = 'view' ) { |
|
| 316 | + return (int) $this->get_prop( 'frequency', $context ); |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + /** |
|
| 320 | + * Get the initial amount for the subscription. |
|
| 321 | + * |
|
| 322 | + * @since 1.0.19 |
|
| 323 | + * @param string $context View or edit context. |
|
| 324 | + * @return float |
|
| 325 | + */ |
|
| 326 | + public function get_initial_amount( $context = 'view' ) { |
|
| 327 | + return (float) wpinv_sanitize_amount( $this->get_prop( 'initial_amount', $context ) ); |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + /** |
|
| 331 | + * Get the recurring amount for the subscription. |
|
| 332 | + * |
|
| 333 | + * @since 1.0.19 |
|
| 334 | + * @param string $context View or edit context. |
|
| 335 | + * @return float |
|
| 336 | + */ |
|
| 337 | + public function get_recurring_amount( $context = 'view' ) { |
|
| 338 | + return (float) wpinv_sanitize_amount( $this->get_prop( 'recurring_amount', $context ) ); |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + /** |
|
| 342 | + * Get number of times that this subscription can be renewed. |
|
| 343 | + * |
|
| 344 | + * @since 1.0.19 |
|
| 345 | + * @param string $context View or edit context. |
|
| 346 | + * @return int |
|
| 347 | + */ |
|
| 348 | + public function get_bill_times( $context = 'view' ) { |
|
| 349 | + return (int) $this->get_prop( 'bill_times', $context ); |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + /** |
|
| 353 | + * Get transaction id of this subscription's parent invoice. |
|
| 354 | + * |
|
| 355 | + * @since 1.0.19 |
|
| 356 | + * @param string $context View or edit context. |
|
| 357 | + * @return string |
|
| 358 | + */ |
|
| 359 | + public function get_transaction_id( $context = 'view' ) { |
|
| 360 | + return $this->get_prop( 'transaction_id', $context ); |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + /** |
|
| 364 | + * Get the date that the subscription was created. |
|
| 365 | + * |
|
| 366 | + * @since 1.0.19 |
|
| 367 | + * @param string $context View or edit context. |
|
| 368 | + * @return string |
|
| 369 | + */ |
|
| 370 | + public function get_created( $context = 'view' ) { |
|
| 371 | + return $this->get_prop( 'created', $context ); |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + /** |
|
| 375 | + * Alias for self::get_created(). |
|
| 376 | + * |
|
| 377 | + * @since 1.0.19 |
|
| 378 | + * @param string $context View or edit context. |
|
| 379 | + * @return string |
|
| 380 | + */ |
|
| 381 | + public function get_date_created( $context = 'view' ) { |
|
| 382 | + return $this->get_created( $context ); |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + /** |
|
| 386 | + * Retrieves the creation date in a timestamp |
|
| 387 | + * |
|
| 388 | + * @since 1.0.0 |
|
| 389 | + * @return int |
|
| 390 | + */ |
|
| 391 | + public function get_time_created() { |
|
| 392 | + $created = $this->get_date_created(); |
|
| 393 | + return empty( $created ) ? current_time( 'timestamp' ) : strtotime( $created, current_time( 'timestamp' ) ); |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + /** |
|
| 397 | + * Get GMT date when the subscription was created. |
|
| 398 | + * |
|
| 399 | + * @since 1.0.19 |
|
| 400 | + * @param string $context View or edit context. |
|
| 401 | + * @return string |
|
| 402 | + */ |
|
| 403 | + public function get_date_created_gmt( $context = 'view' ) { |
|
| 404 | + $date = $this->get_date_created( $context ); |
|
| 405 | + |
|
| 406 | + if ( $date ) { |
|
| 407 | + $date = get_gmt_from_date( $date ); |
|
| 408 | + } |
|
| 409 | + return $date; |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + /** |
|
| 413 | + * Get the date that the subscription will renew. |
|
| 414 | + * |
|
| 415 | + * @since 1.0.19 |
|
| 416 | + * @param string $context View or edit context. |
|
| 417 | + * @return string |
|
| 418 | + */ |
|
| 419 | + public function get_next_renewal_date( $context = 'view' ) { |
|
| 420 | + return $this->get_prop( 'expiration', $context ); |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + /** |
|
| 424 | + * Alias for self::get_next_renewal_date(). |
|
| 425 | + * |
|
| 426 | + * @since 1.0.19 |
|
| 427 | + * @param string $context View or edit context. |
|
| 428 | + * @return string |
|
| 429 | + */ |
|
| 430 | + public function get_expiration( $context = 'view' ) { |
|
| 431 | + return $this->get_next_renewal_date( $context ); |
|
| 432 | + } |
|
| 433 | + |
|
| 434 | + /** |
|
| 435 | + * Retrieves the expiration date in a timestamp |
|
| 436 | + * |
|
| 437 | + * @since 1.0.0 |
|
| 438 | + * @return int |
|
| 439 | + */ |
|
| 440 | + public function get_expiration_time() { |
|
| 441 | + $expiration = $this->get_expiration(); |
|
| 442 | + |
|
| 443 | + if ( empty( $expiration ) || '0000-00-00 00:00:00' == $expiration ) { |
|
| 444 | + return current_time( 'timestamp' ); |
|
| 445 | + } |
|
| 446 | + |
|
| 447 | + $expiration = strtotime( $expiration, current_time( 'timestamp' ) ); |
|
| 448 | + return $expiration < current_time( 'timestamp' ) ? current_time( 'timestamp' ) : $expiration; |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + /** |
|
| 452 | + * Get GMT date when the subscription will renew. |
|
| 453 | + * |
|
| 454 | + * @since 1.0.19 |
|
| 455 | + * @param string $context View or edit context. |
|
| 456 | + * @return string |
|
| 457 | + */ |
|
| 458 | + public function get_next_renewal_date_gmt( $context = 'view' ) { |
|
| 459 | + $date = $this->get_next_renewal_date( $context ); |
|
| 460 | + |
|
| 461 | + if ( $date ) { |
|
| 462 | + $date = get_gmt_from_date( $date ); |
|
| 463 | + } |
|
| 464 | + return $date; |
|
| 465 | + } |
|
| 466 | + |
|
| 467 | + /** |
|
| 468 | + * Get the subscription's trial period. |
|
| 469 | + * |
|
| 470 | + * @since 1.0.19 |
|
| 471 | + * @param string $context View or edit context. |
|
| 472 | + * @return string |
|
| 473 | + */ |
|
| 474 | + public function get_trial_period( $context = 'view' ) { |
|
| 475 | + return $this->get_prop( 'trial_period', $context ); |
|
| 476 | + } |
|
| 477 | + |
|
| 478 | + /** |
|
| 479 | + * Get the subscription's status. |
|
| 480 | + * |
|
| 481 | + * @since 1.0.19 |
|
| 482 | + * @param string $context View or edit context. |
|
| 483 | + * @return string |
|
| 484 | + */ |
|
| 485 | + public function get_status( $context = 'view' ) { |
|
| 486 | + return $this->get_prop( 'status', $context ); |
|
| 487 | + } |
|
| 488 | + |
|
| 489 | + /** |
|
| 490 | + * Get the subscription's profile id. |
|
| 491 | + * |
|
| 492 | + * @since 1.0.19 |
|
| 493 | + * @param string $context View or edit context. |
|
| 494 | + * @return string |
|
| 495 | + */ |
|
| 496 | + public function get_profile_id( $context = 'view' ) { |
|
| 497 | + return $this->get_prop( 'profile_id', $context ); |
|
| 498 | + } |
|
| 499 | + |
|
| 500 | + /* |
|
| 501 | + |-------------------------------------------------------------------------- |
|
| 502 | + | Setters |
|
| 503 | + |-------------------------------------------------------------------------- |
|
| 504 | + */ |
|
| 505 | + |
|
| 506 | + /** |
|
| 507 | + * Set customer id. |
|
| 508 | + * |
|
| 509 | + * @since 1.0.19 |
|
| 510 | + * @param int $value The customer's id. |
|
| 511 | + */ |
|
| 512 | + public function set_customer_id( $value ) { |
|
| 513 | + $this->set_prop( 'customer_id', (int) $value ); |
|
| 514 | + } |
|
| 515 | + |
|
| 516 | + /** |
|
| 517 | + * Set parent invoice id. |
|
| 518 | + * |
|
| 519 | + * @since 1.0.19 |
|
| 520 | + * @param int $value The parent invoice id. |
|
| 521 | + */ |
|
| 522 | + public function set_parent_invoice_id( $value ) { |
|
| 523 | + $this->set_prop( 'parent_payment_id', (int) $value ); |
|
| 524 | + } |
|
| 525 | + |
|
| 526 | + /** |
|
| 527 | + * Alias for self::set_parent_invoice_id(). |
|
| 528 | + * |
|
| 529 | + * @since 1.0.19 |
|
| 530 | + * @param int $value The parent invoice id. |
|
| 531 | + */ |
|
| 532 | + public function set_parent_payment_id( $value ) { |
|
| 533 | + $this->set_parent_invoice_id( $value ); |
|
| 534 | + } |
|
| 535 | + |
|
| 536 | + /** |
|
| 537 | + * Alias for self::set_parent_invoice_id(). |
|
| 538 | + * |
|
| 539 | + * @since 1.0.19 |
|
| 540 | + * @param int $value The parent invoice id. |
|
| 541 | + */ |
|
| 542 | + public function set_original_payment_id( $value ) { |
|
| 543 | + $this->set_parent_invoice_id( $value ); |
|
| 544 | + } |
|
| 545 | + |
|
| 546 | + /** |
|
| 547 | + * Set subscription's product id. |
|
| 548 | + * |
|
| 549 | + * @since 1.0.19 |
|
| 550 | + * @param int $value The subscription product id. |
|
| 551 | + */ |
|
| 552 | + public function set_product_id( $value ) { |
|
| 553 | + $this->set_prop( 'product_id', (int) $value ); |
|
| 554 | + } |
|
| 555 | + |
|
| 556 | + /** |
|
| 557 | + * Set the period of a renewal. |
|
| 558 | + * |
|
| 559 | + * @since 1.0.19 |
|
| 560 | + * @param string $value The renewal period. |
|
| 561 | + */ |
|
| 562 | + public function set_period( $value ) { |
|
| 563 | + $this->set_prop( 'period', $value ); |
|
| 564 | + } |
|
| 565 | + |
|
| 566 | + /** |
|
| 567 | + * Set number of periods each renewal is valid for. |
|
| 568 | + * |
|
| 569 | + * @since 1.0.19 |
|
| 570 | + * @param int $value The subscription frequency. |
|
| 571 | + */ |
|
| 572 | + public function set_frequency( $value ) { |
|
| 573 | + $value = empty( $value ) ? 1 : (int) $value; |
|
| 574 | + $this->set_prop( 'frequency', absint( $value ) ); |
|
| 575 | + } |
|
| 576 | + |
|
| 577 | + /** |
|
| 578 | + * Set the initial amount for the subscription. |
|
| 579 | + * |
|
| 580 | + * @since 1.0.19 |
|
| 581 | + * @param float $value The initial subcription amount. |
|
| 582 | + */ |
|
| 583 | + public function set_initial_amount( $value ) { |
|
| 584 | + $this->set_prop( 'initial_amount', wpinv_sanitize_amount( $value ) ); |
|
| 585 | + } |
|
| 586 | + |
|
| 587 | + /** |
|
| 588 | + * Set the recurring amount for the subscription. |
|
| 589 | + * |
|
| 590 | + * @since 1.0.19 |
|
| 591 | + * @param float $value The recurring subcription amount. |
|
| 592 | + */ |
|
| 593 | + public function set_recurring_amount( $value ) { |
|
| 594 | + $this->set_prop( 'recurring_amount', wpinv_sanitize_amount( $value ) ); |
|
| 595 | + } |
|
| 596 | + |
|
| 597 | + /** |
|
| 598 | + * Set number of times that this subscription can be renewed. |
|
| 599 | + * |
|
| 600 | + * @since 1.0.19 |
|
| 601 | + * @param int $value Bill times. |
|
| 602 | + */ |
|
| 603 | + public function set_bill_times( $value ) { |
|
| 604 | + $this->set_prop( 'bill_times', (int) $value ); |
|
| 605 | + } |
|
| 606 | + |
|
| 607 | + /** |
|
| 608 | + * Get transaction id of this subscription's parent invoice. |
|
| 609 | + * |
|
| 610 | + * @since 1.0.19 |
|
| 611 | + * @param string $value Bill times. |
|
| 612 | + */ |
|
| 613 | + public function set_transaction_id( $value ) { |
|
| 614 | + $this->set_prop( 'transaction_id', sanitize_text_field( $value ) ); |
|
| 615 | + } |
|
| 616 | + |
|
| 617 | + /** |
|
| 618 | + * Set date when this subscription started. |
|
| 619 | + * |
|
| 620 | + * @since 1.0.19 |
|
| 621 | + * @param string $value strtotime compliant date. |
|
| 622 | + */ |
|
| 623 | + public function set_created( $value ) { |
|
| 624 | + $date = strtotime( $value ); |
|
| 625 | + |
|
| 626 | + if ( $date && $value !== '0000-00-00 00:00:00' ) { |
|
| 627 | + $this->set_prop( 'created', date( 'Y-m-d H:i:s', $date ) ); |
|
| 628 | + return; |
|
| 629 | + } |
|
| 630 | + |
|
| 631 | + $this->set_prop( 'created', '' ); |
|
| 185 | 632 | |
| 186 | - /** |
|
| 187 | - * Get customer id. |
|
| 188 | - * |
|
| 189 | - * @since 1.0.19 |
|
| 190 | - * @param string $context View or edit context. |
|
| 191 | - * @return int |
|
| 192 | - */ |
|
| 193 | - public function get_customer_id( $context = 'view' ) { |
|
| 194 | - return (int) $this->get_prop( 'customer_id', $context ); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * Get customer information. |
|
| 199 | - * |
|
| 200 | - * @since 1.0.19 |
|
| 201 | - * @param string $context View or edit context. |
|
| 202 | - * @return WP_User|false WP_User object on success, false on failure. |
|
| 203 | - */ |
|
| 204 | - public function get_customer( $context = 'view' ) { |
|
| 205 | - return get_userdata( $this->get_customer_id( $context ) ); |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * Get parent invoice id. |
|
| 210 | - * |
|
| 211 | - * @since 1.0.19 |
|
| 212 | - * @param string $context View or edit context. |
|
| 213 | - * @return int |
|
| 214 | - */ |
|
| 215 | - public function get_parent_invoice_id( $context = 'view' ) { |
|
| 216 | - return (int) $this->get_prop( 'parent_payment_id', $context ); |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * Alias for self::get_parent_invoice_id(). |
|
| 221 | - * |
|
| 222 | - * @since 1.0.19 |
|
| 223 | - * @param string $context View or edit context. |
|
| 224 | - * @return int |
|
| 225 | - */ |
|
| 226 | - public function get_parent_payment_id( $context = 'view' ) { |
|
| 227 | - return $this->get_parent_invoice_id( $context ); |
|
| 228 | - } |
|
| 633 | + } |
|
| 229 | 634 | |
| 230 | - /** |
|
| 231 | - * Alias for self::get_parent_invoice_id(). |
|
| 635 | + /** |
|
| 636 | + * Alias for self::set_created(). |
|
| 232 | 637 | * |
| 233 | - * @since 1.0.0 |
|
| 234 | - * @return int |
|
| 638 | + * @since 1.0.19 |
|
| 639 | + * @param string $value strtotime compliant date. |
|
| 235 | 640 | */ |
| 236 | - public function get_original_payment_id( $context = 'view' ) { |
|
| 237 | - return $this->get_parent_invoice_id( $context ); |
|
| 641 | + public function set_date_created( $value ) { |
|
| 642 | + $this->set_created( $value ); |
|
| 238 | 643 | } |
| 239 | 644 | |
| 240 | - /** |
|
| 241 | - * Get parent invoice. |
|
| 242 | - * |
|
| 243 | - * @since 1.0.19 |
|
| 244 | - * @param string $context View or edit context. |
|
| 245 | - * @return WPInv_Invoice |
|
| 246 | - */ |
|
| 247 | - public function get_parent_invoice( $context = 'view' ) { |
|
| 248 | - return new WPInv_Invoice( $this->get_parent_invoice_id( $context ) ); |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - /** |
|
| 252 | - * Alias for self::get_parent_invoice(). |
|
| 253 | - * |
|
| 254 | - * @since 1.0.19 |
|
| 255 | - * @param string $context View or edit context. |
|
| 256 | - * @return WPInv_Invoice |
|
| 257 | - */ |
|
| 258 | - public function get_parent_payment( $context = 'view' ) { |
|
| 259 | - return $this->get_parent_invoice( $context ); |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - /** |
|
| 263 | - * Get subscription's product id. |
|
| 264 | - * |
|
| 265 | - * @since 1.0.19 |
|
| 266 | - * @param string $context View or edit context. |
|
| 267 | - * @return int |
|
| 268 | - */ |
|
| 269 | - public function get_product_id( $context = 'view' ) { |
|
| 270 | - return (int) $this->get_prop( 'product_id', $context ); |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - /** |
|
| 274 | - * Get the subscription product. |
|
| 275 | - * |
|
| 276 | - * @since 1.0.19 |
|
| 277 | - * @param string $context View or edit context. |
|
| 278 | - * @return WPInv_Item |
|
| 279 | - */ |
|
| 280 | - public function get_product( $context = 'view' ) { |
|
| 281 | - return new WPInv_Item( $this->get_product_id( $context ) ); |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - /** |
|
| 285 | - * Get parent invoice's gateway. |
|
| 286 | - * |
|
| 287 | - * Here for backwards compatibility. |
|
| 288 | - * |
|
| 289 | - * @since 1.0.19 |
|
| 290 | - * @param string $context View or edit context. |
|
| 291 | - * @return string |
|
| 292 | - */ |
|
| 293 | - public function get_gateway( $context = 'view' ) { |
|
| 294 | - return $this->get_parent_invoice( $context )->get_gateway(); |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - /** |
|
| 298 | - * Get the period of a renewal. |
|
| 299 | - * |
|
| 300 | - * @since 1.0.19 |
|
| 301 | - * @param string $context View or edit context. |
|
| 302 | - * @return string |
|
| 303 | - */ |
|
| 304 | - public function get_period( $context = 'view' ) { |
|
| 305 | - return $this->get_prop( 'period', $context ); |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - /** |
|
| 309 | - * Get number of periods each renewal is valid for. |
|
| 310 | - * |
|
| 311 | - * @since 1.0.19 |
|
| 312 | - * @param string $context View or edit context. |
|
| 313 | - * @return int |
|
| 314 | - */ |
|
| 315 | - public function get_frequency( $context = 'view' ) { |
|
| 316 | - return (int) $this->get_prop( 'frequency', $context ); |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - /** |
|
| 320 | - * Get the initial amount for the subscription. |
|
| 321 | - * |
|
| 322 | - * @since 1.0.19 |
|
| 323 | - * @param string $context View or edit context. |
|
| 324 | - * @return float |
|
| 325 | - */ |
|
| 326 | - public function get_initial_amount( $context = 'view' ) { |
|
| 327 | - return (float) wpinv_sanitize_amount( $this->get_prop( 'initial_amount', $context ) ); |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - /** |
|
| 331 | - * Get the recurring amount for the subscription. |
|
| 332 | - * |
|
| 333 | - * @since 1.0.19 |
|
| 334 | - * @param string $context View or edit context. |
|
| 335 | - * @return float |
|
| 336 | - */ |
|
| 337 | - public function get_recurring_amount( $context = 'view' ) { |
|
| 338 | - return (float) wpinv_sanitize_amount( $this->get_prop( 'recurring_amount', $context ) ); |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - /** |
|
| 342 | - * Get number of times that this subscription can be renewed. |
|
| 343 | - * |
|
| 344 | - * @since 1.0.19 |
|
| 345 | - * @param string $context View or edit context. |
|
| 346 | - * @return int |
|
| 347 | - */ |
|
| 348 | - public function get_bill_times( $context = 'view' ) { |
|
| 349 | - return (int) $this->get_prop( 'bill_times', $context ); |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - /** |
|
| 353 | - * Get transaction id of this subscription's parent invoice. |
|
| 354 | - * |
|
| 355 | - * @since 1.0.19 |
|
| 356 | - * @param string $context View or edit context. |
|
| 357 | - * @return string |
|
| 358 | - */ |
|
| 359 | - public function get_transaction_id( $context = 'view' ) { |
|
| 360 | - return $this->get_prop( 'transaction_id', $context ); |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - /** |
|
| 364 | - * Get the date that the subscription was created. |
|
| 365 | - * |
|
| 366 | - * @since 1.0.19 |
|
| 367 | - * @param string $context View or edit context. |
|
| 368 | - * @return string |
|
| 369 | - */ |
|
| 370 | - public function get_created( $context = 'view' ) { |
|
| 371 | - return $this->get_prop( 'created', $context ); |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - /** |
|
| 375 | - * Alias for self::get_created(). |
|
| 376 | - * |
|
| 377 | - * @since 1.0.19 |
|
| 378 | - * @param string $context View or edit context. |
|
| 379 | - * @return string |
|
| 380 | - */ |
|
| 381 | - public function get_date_created( $context = 'view' ) { |
|
| 382 | - return $this->get_created( $context ); |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - /** |
|
| 386 | - * Retrieves the creation date in a timestamp |
|
| 387 | - * |
|
| 388 | - * @since 1.0.0 |
|
| 389 | - * @return int |
|
| 390 | - */ |
|
| 391 | - public function get_time_created() { |
|
| 392 | - $created = $this->get_date_created(); |
|
| 393 | - return empty( $created ) ? current_time( 'timestamp' ) : strtotime( $created, current_time( 'timestamp' ) ); |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - /** |
|
| 397 | - * Get GMT date when the subscription was created. |
|
| 398 | - * |
|
| 399 | - * @since 1.0.19 |
|
| 400 | - * @param string $context View or edit context. |
|
| 401 | - * @return string |
|
| 402 | - */ |
|
| 403 | - public function get_date_created_gmt( $context = 'view' ) { |
|
| 404 | - $date = $this->get_date_created( $context ); |
|
| 645 | + /** |
|
| 646 | + * Set the date that the subscription will renew. |
|
| 647 | + * |
|
| 648 | + * @since 1.0.19 |
|
| 649 | + * @param string $value strtotime compliant date. |
|
| 650 | + */ |
|
| 651 | + public function set_next_renewal_date( $value ) { |
|
| 652 | + $date = strtotime( $value ); |
|
| 405 | 653 | |
| 406 | - if ( $date ) { |
|
| 407 | - $date = get_gmt_from_date( $date ); |
|
| 654 | + if ( $date && $value !== '0000-00-00 00:00:00' ) { |
|
| 655 | + $this->set_prop( 'expiration', date( 'Y-m-d H:i:s', $date ) ); |
|
| 656 | + return; |
|
| 408 | 657 | } |
| 409 | - return $date; |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - /** |
|
| 413 | - * Get the date that the subscription will renew. |
|
| 414 | - * |
|
| 415 | - * @since 1.0.19 |
|
| 416 | - * @param string $context View or edit context. |
|
| 417 | - * @return string |
|
| 418 | - */ |
|
| 419 | - public function get_next_renewal_date( $context = 'view' ) { |
|
| 420 | - return $this->get_prop( 'expiration', $context ); |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - /** |
|
| 424 | - * Alias for self::get_next_renewal_date(). |
|
| 425 | - * |
|
| 426 | - * @since 1.0.19 |
|
| 427 | - * @param string $context View or edit context. |
|
| 428 | - * @return string |
|
| 429 | - */ |
|
| 430 | - public function get_expiration( $context = 'view' ) { |
|
| 431 | - return $this->get_next_renewal_date( $context ); |
|
| 432 | - } |
|
| 433 | - |
|
| 434 | - /** |
|
| 435 | - * Retrieves the expiration date in a timestamp |
|
| 436 | - * |
|
| 437 | - * @since 1.0.0 |
|
| 438 | - * @return int |
|
| 439 | - */ |
|
| 440 | - public function get_expiration_time() { |
|
| 441 | - $expiration = $this->get_expiration(); |
|
| 442 | - |
|
| 443 | - if ( empty( $expiration ) || '0000-00-00 00:00:00' == $expiration ) { |
|
| 444 | - return current_time( 'timestamp' ); |
|
| 445 | - } |
|
| 446 | - |
|
| 447 | - $expiration = strtotime( $expiration, current_time( 'timestamp' ) ); |
|
| 448 | - return $expiration < current_time( 'timestamp' ) ? current_time( 'timestamp' ) : $expiration; |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - /** |
|
| 452 | - * Get GMT date when the subscription will renew. |
|
| 453 | - * |
|
| 454 | - * @since 1.0.19 |
|
| 455 | - * @param string $context View or edit context. |
|
| 456 | - * @return string |
|
| 457 | - */ |
|
| 458 | - public function get_next_renewal_date_gmt( $context = 'view' ) { |
|
| 459 | - $date = $this->get_next_renewal_date( $context ); |
|
| 460 | 658 | |
| 461 | - if ( $date ) { |
|
| 462 | - $date = get_gmt_from_date( $date ); |
|
| 463 | - } |
|
| 464 | - return $date; |
|
| 465 | - } |
|
| 466 | - |
|
| 467 | - /** |
|
| 468 | - * Get the subscription's trial period. |
|
| 469 | - * |
|
| 470 | - * @since 1.0.19 |
|
| 471 | - * @param string $context View or edit context. |
|
| 472 | - * @return string |
|
| 473 | - */ |
|
| 474 | - public function get_trial_period( $context = 'view' ) { |
|
| 475 | - return $this->get_prop( 'trial_period', $context ); |
|
| 476 | - } |
|
| 477 | - |
|
| 478 | - /** |
|
| 479 | - * Get the subscription's status. |
|
| 480 | - * |
|
| 481 | - * @since 1.0.19 |
|
| 482 | - * @param string $context View or edit context. |
|
| 483 | - * @return string |
|
| 484 | - */ |
|
| 485 | - public function get_status( $context = 'view' ) { |
|
| 486 | - return $this->get_prop( 'status', $context ); |
|
| 487 | - } |
|
| 488 | - |
|
| 489 | - /** |
|
| 490 | - * Get the subscription's profile id. |
|
| 491 | - * |
|
| 492 | - * @since 1.0.19 |
|
| 493 | - * @param string $context View or edit context. |
|
| 494 | - * @return string |
|
| 495 | - */ |
|
| 496 | - public function get_profile_id( $context = 'view' ) { |
|
| 497 | - return $this->get_prop( 'profile_id', $context ); |
|
| 498 | - } |
|
| 499 | - |
|
| 500 | - /* |
|
| 501 | - |-------------------------------------------------------------------------- |
|
| 502 | - | Setters |
|
| 503 | - |-------------------------------------------------------------------------- |
|
| 504 | - */ |
|
| 659 | + $this->set_prop( 'expiration', '' ); |
|
| 505 | 660 | |
| 506 | - /** |
|
| 507 | - * Set customer id. |
|
| 508 | - * |
|
| 509 | - * @since 1.0.19 |
|
| 510 | - * @param int $value The customer's id. |
|
| 511 | - */ |
|
| 512 | - public function set_customer_id( $value ) { |
|
| 513 | - $this->set_prop( 'customer_id', (int) $value ); |
|
| 514 | - } |
|
| 515 | - |
|
| 516 | - /** |
|
| 517 | - * Set parent invoice id. |
|
| 518 | - * |
|
| 519 | - * @since 1.0.19 |
|
| 520 | - * @param int $value The parent invoice id. |
|
| 521 | - */ |
|
| 522 | - public function set_parent_invoice_id( $value ) { |
|
| 523 | - $this->set_prop( 'parent_payment_id', (int) $value ); |
|
| 524 | - } |
|
| 525 | - |
|
| 526 | - /** |
|
| 527 | - * Alias for self::set_parent_invoice_id(). |
|
| 528 | - * |
|
| 529 | - * @since 1.0.19 |
|
| 530 | - * @param int $value The parent invoice id. |
|
| 531 | - */ |
|
| 532 | - public function set_parent_payment_id( $value ) { |
|
| 533 | - $this->set_parent_invoice_id( $value ); |
|
| 534 | - } |
|
| 661 | + } |
|
| 535 | 662 | |
| 536 | - /** |
|
| 537 | - * Alias for self::set_parent_invoice_id(). |
|
| 663 | + /** |
|
| 664 | + * Alias for self::set_next_renewal_date(). |
|
| 538 | 665 | * |
| 539 | 666 | * @since 1.0.19 |
| 540 | - * @param int $value The parent invoice id. |
|
| 667 | + * @param string $value strtotime compliant date. |
|
| 541 | 668 | */ |
| 542 | - public function set_original_payment_id( $value ) { |
|
| 543 | - $this->set_parent_invoice_id( $value ); |
|
| 544 | - } |
|
| 545 | - |
|
| 546 | - /** |
|
| 547 | - * Set subscription's product id. |
|
| 548 | - * |
|
| 549 | - * @since 1.0.19 |
|
| 550 | - * @param int $value The subscription product id. |
|
| 551 | - */ |
|
| 552 | - public function set_product_id( $value ) { |
|
| 553 | - $this->set_prop( 'product_id', (int) $value ); |
|
| 554 | - } |
|
| 555 | - |
|
| 556 | - /** |
|
| 557 | - * Set the period of a renewal. |
|
| 558 | - * |
|
| 559 | - * @since 1.0.19 |
|
| 560 | - * @param string $value The renewal period. |
|
| 561 | - */ |
|
| 562 | - public function set_period( $value ) { |
|
| 563 | - $this->set_prop( 'period', $value ); |
|
| 564 | - } |
|
| 565 | - |
|
| 566 | - /** |
|
| 567 | - * Set number of periods each renewal is valid for. |
|
| 568 | - * |
|
| 569 | - * @since 1.0.19 |
|
| 570 | - * @param int $value The subscription frequency. |
|
| 571 | - */ |
|
| 572 | - public function set_frequency( $value ) { |
|
| 573 | - $value = empty( $value ) ? 1 : (int) $value; |
|
| 574 | - $this->set_prop( 'frequency', absint( $value ) ); |
|
| 575 | - } |
|
| 576 | - |
|
| 577 | - /** |
|
| 578 | - * Set the initial amount for the subscription. |
|
| 579 | - * |
|
| 580 | - * @since 1.0.19 |
|
| 581 | - * @param float $value The initial subcription amount. |
|
| 582 | - */ |
|
| 583 | - public function set_initial_amount( $value ) { |
|
| 584 | - $this->set_prop( 'initial_amount', wpinv_sanitize_amount( $value ) ); |
|
| 585 | - } |
|
| 586 | - |
|
| 587 | - /** |
|
| 588 | - * Set the recurring amount for the subscription. |
|
| 589 | - * |
|
| 590 | - * @since 1.0.19 |
|
| 591 | - * @param float $value The recurring subcription amount. |
|
| 592 | - */ |
|
| 593 | - public function set_recurring_amount( $value ) { |
|
| 594 | - $this->set_prop( 'recurring_amount', wpinv_sanitize_amount( $value ) ); |
|
| 595 | - } |
|
| 596 | - |
|
| 597 | - /** |
|
| 598 | - * Set number of times that this subscription can be renewed. |
|
| 599 | - * |
|
| 600 | - * @since 1.0.19 |
|
| 601 | - * @param int $value Bill times. |
|
| 602 | - */ |
|
| 603 | - public function set_bill_times( $value ) { |
|
| 604 | - $this->set_prop( 'bill_times', (int) $value ); |
|
| 605 | - } |
|
| 606 | - |
|
| 607 | - /** |
|
| 608 | - * Get transaction id of this subscription's parent invoice. |
|
| 609 | - * |
|
| 610 | - * @since 1.0.19 |
|
| 611 | - * @param string $value Bill times. |
|
| 612 | - */ |
|
| 613 | - public function set_transaction_id( $value ) { |
|
| 614 | - $this->set_prop( 'transaction_id', sanitize_text_field( $value ) ); |
|
| 615 | - } |
|
| 616 | - |
|
| 617 | - /** |
|
| 618 | - * Set date when this subscription started. |
|
| 619 | - * |
|
| 620 | - * @since 1.0.19 |
|
| 621 | - * @param string $value strtotime compliant date. |
|
| 622 | - */ |
|
| 623 | - public function set_created( $value ) { |
|
| 624 | - $date = strtotime( $value ); |
|
| 669 | + public function set_expiration( $value ) { |
|
| 670 | + $this->set_next_renewal_date( $value ); |
|
| 671 | + } |
|
| 625 | 672 | |
| 626 | - if ( $date && $value !== '0000-00-00 00:00:00' ) { |
|
| 627 | - $this->set_prop( 'created', date( 'Y-m-d H:i:s', $date ) ); |
|
| 673 | + /** |
|
| 674 | + * Set the subscription's trial period. |
|
| 675 | + * |
|
| 676 | + * @since 1.0.19 |
|
| 677 | + * @param string $value trial period e.g 1 year. |
|
| 678 | + */ |
|
| 679 | + public function set_trial_period( $value ) { |
|
| 680 | + $this->set_prop( 'trial_period', $value ); |
|
| 681 | + } |
|
| 682 | + |
|
| 683 | + /** |
|
| 684 | + * Set the subscription's status. |
|
| 685 | + * |
|
| 686 | + * @since 1.0.19 |
|
| 687 | + * @param string $new_status New subscription status. |
|
| 688 | + */ |
|
| 689 | + public function set_status( $new_status ) { |
|
| 690 | + |
|
| 691 | + // Abort if this is not a valid status; |
|
| 692 | + if ( ! array_key_exists( $new_status, getpaid_get_subscription_statuses() ) ) { |
|
| 628 | 693 | return; |
| 629 | 694 | } |
| 630 | 695 | |
| 631 | - $this->set_prop( 'created', '' ); |
|
| 632 | 696 | |
| 633 | - } |
|
| 697 | + $old_status = ! empty( $this->status_transition['from'] ) ? $this->status_transition['from'] : $this->get_status(); |
|
| 698 | + if ( true === $this->object_read && $old_status !== $new_status ) { |
|
| 699 | + $this->status_transition = array( |
|
| 700 | + 'from' => $old_status, |
|
| 701 | + 'to' => $new_status, |
|
| 702 | + ); |
|
| 703 | + } |
|
| 634 | 704 | |
| 635 | - /** |
|
| 636 | - * Alias for self::set_created(). |
|
| 637 | - * |
|
| 638 | - * @since 1.0.19 |
|
| 639 | - * @param string $value strtotime compliant date. |
|
| 640 | - */ |
|
| 641 | - public function set_date_created( $value ) { |
|
| 642 | - $this->set_created( $value ); |
|
| 705 | + $this->set_prop( 'status', $new_status ); |
|
| 643 | 706 | } |
| 644 | 707 | |
| 645 | - /** |
|
| 646 | - * Set the date that the subscription will renew. |
|
| 647 | - * |
|
| 648 | - * @since 1.0.19 |
|
| 649 | - * @param string $value strtotime compliant date. |
|
| 650 | - */ |
|
| 651 | - public function set_next_renewal_date( $value ) { |
|
| 652 | - $date = strtotime( $value ); |
|
| 708 | + /** |
|
| 709 | + * Set the subscription's (remote) profile id. |
|
| 710 | + * |
|
| 711 | + * @since 1.0.19 |
|
| 712 | + * @param string $value the remote profile id. |
|
| 713 | + */ |
|
| 714 | + public function set_profile_id( $value ) { |
|
| 715 | + $this->set_prop( 'profile_id', sanitize_text_field( $value ) ); |
|
| 716 | + } |
|
| 653 | 717 | |
| 654 | - if ( $date && $value !== '0000-00-00 00:00:00' ) { |
|
| 655 | - $this->set_prop( 'expiration', date( 'Y-m-d H:i:s', $date ) ); |
|
| 656 | - return; |
|
| 657 | - } |
|
| 658 | - |
|
| 659 | - $this->set_prop( 'expiration', '' ); |
|
| 660 | - |
|
| 661 | - } |
|
| 662 | - |
|
| 663 | - /** |
|
| 664 | - * Alias for self::set_next_renewal_date(). |
|
| 665 | - * |
|
| 666 | - * @since 1.0.19 |
|
| 667 | - * @param string $value strtotime compliant date. |
|
| 668 | - */ |
|
| 669 | - public function set_expiration( $value ) { |
|
| 670 | - $this->set_next_renewal_date( $value ); |
|
| 671 | - } |
|
| 672 | - |
|
| 673 | - /** |
|
| 674 | - * Set the subscription's trial period. |
|
| 675 | - * |
|
| 676 | - * @since 1.0.19 |
|
| 677 | - * @param string $value trial period e.g 1 year. |
|
| 678 | - */ |
|
| 679 | - public function set_trial_period( $value ) { |
|
| 680 | - $this->set_prop( 'trial_period', $value ); |
|
| 681 | - } |
|
| 682 | - |
|
| 683 | - /** |
|
| 684 | - * Set the subscription's status. |
|
| 685 | - * |
|
| 686 | - * @since 1.0.19 |
|
| 687 | - * @param string $new_status New subscription status. |
|
| 688 | - */ |
|
| 689 | - public function set_status( $new_status ) { |
|
| 690 | - |
|
| 691 | - // Abort if this is not a valid status; |
|
| 692 | - if ( ! array_key_exists( $new_status, getpaid_get_subscription_statuses() ) ) { |
|
| 693 | - return; |
|
| 694 | - } |
|
| 695 | - |
|
| 696 | - |
|
| 697 | - $old_status = ! empty( $this->status_transition['from'] ) ? $this->status_transition['from'] : $this->get_status(); |
|
| 698 | - if ( true === $this->object_read && $old_status !== $new_status ) { |
|
| 699 | - $this->status_transition = array( |
|
| 700 | - 'from' => $old_status, |
|
| 701 | - 'to' => $new_status, |
|
| 702 | - ); |
|
| 703 | - } |
|
| 704 | - |
|
| 705 | - $this->set_prop( 'status', $new_status ); |
|
| 706 | - } |
|
| 707 | - |
|
| 708 | - /** |
|
| 709 | - * Set the subscription's (remote) profile id. |
|
| 710 | - * |
|
| 711 | - * @since 1.0.19 |
|
| 712 | - * @param string $value the remote profile id. |
|
| 713 | - */ |
|
| 714 | - public function set_profile_id( $value ) { |
|
| 715 | - $this->set_prop( 'profile_id', sanitize_text_field( $value ) ); |
|
| 716 | - } |
|
| 717 | - |
|
| 718 | - /* |
|
| 718 | + /* |
|
| 719 | 719 | |-------------------------------------------------------------------------- |
| 720 | 720 | | Boolean methods |
| 721 | 721 | |-------------------------------------------------------------------------- |
@@ -724,55 +724,55 @@ discard block |
||
| 724 | 724 | | |
| 725 | 725 | */ |
| 726 | 726 | |
| 727 | - /** |
|
| 727 | + /** |
|
| 728 | 728 | * Checks if the subscription has a given status. |
| 729 | - * |
|
| 730 | - * @param string|array String or array of strings to check for. |
|
| 731 | - * @return bool |
|
| 729 | + * |
|
| 730 | + * @param string|array String or array of strings to check for. |
|
| 731 | + * @return bool |
|
| 732 | 732 | */ |
| 733 | 733 | public function has_status( $status ) { |
| 734 | 734 | return in_array( $this->get_status(), wpinv_clean( wpinv_parse_list( $status ) ) ); |
| 735 | - } |
|
| 735 | + } |
|
| 736 | 736 | |
| 737 | - /** |
|
| 737 | + /** |
|
| 738 | 738 | * Checks if the subscription has a trial period. |
| 739 | - * |
|
| 740 | - * @return bool |
|
| 739 | + * |
|
| 740 | + * @return bool |
|
| 741 | 741 | */ |
| 742 | 742 | public function has_trial_period() { |
| 743 | - $period = $this->get_trial_period(); |
|
| 743 | + $period = $this->get_trial_period(); |
|
| 744 | 744 | return ! empty( $period ); |
| 745 | - } |
|
| 746 | - |
|
| 747 | - /** |
|
| 748 | - * Is the subscription active? |
|
| 749 | - * |
|
| 750 | - * @return bool |
|
| 751 | - */ |
|
| 752 | - public function is_active() { |
|
| 753 | - return $this->has_status( 'active trialling' ) && ! $this->is_expired(); |
|
| 754 | - } |
|
| 755 | - |
|
| 756 | - /** |
|
| 757 | - * Is the subscription expired? |
|
| 758 | - * |
|
| 759 | - * @return bool |
|
| 760 | - */ |
|
| 761 | - public function is_expired() { |
|
| 762 | - return $this->has_status( 'expired' ) || ( $this->has_status( 'active cancelled trialling' ) && $this->get_expiration_time() < current_time( 'timestamp' ) ); |
|
| 763 | - } |
|
| 764 | - |
|
| 765 | - /** |
|
| 766 | - * Is this the last renewals? |
|
| 767 | - * |
|
| 768 | - * @return bool |
|
| 769 | - */ |
|
| 770 | - public function is_last_renewal() { |
|
| 771 | - $max_bills = $this->get_bill_times(); |
|
| 772 | - return ! empty( $max_bills ) && $max_bills <= $this->get_times_billed(); |
|
| 773 | - } |
|
| 774 | - |
|
| 775 | - /* |
|
| 745 | + } |
|
| 746 | + |
|
| 747 | + /** |
|
| 748 | + * Is the subscription active? |
|
| 749 | + * |
|
| 750 | + * @return bool |
|
| 751 | + */ |
|
| 752 | + public function is_active() { |
|
| 753 | + return $this->has_status( 'active trialling' ) && ! $this->is_expired(); |
|
| 754 | + } |
|
| 755 | + |
|
| 756 | + /** |
|
| 757 | + * Is the subscription expired? |
|
| 758 | + * |
|
| 759 | + * @return bool |
|
| 760 | + */ |
|
| 761 | + public function is_expired() { |
|
| 762 | + return $this->has_status( 'expired' ) || ( $this->has_status( 'active cancelled trialling' ) && $this->get_expiration_time() < current_time( 'timestamp' ) ); |
|
| 763 | + } |
|
| 764 | + |
|
| 765 | + /** |
|
| 766 | + * Is this the last renewals? |
|
| 767 | + * |
|
| 768 | + * @return bool |
|
| 769 | + */ |
|
| 770 | + public function is_last_renewal() { |
|
| 771 | + $max_bills = $this->get_bill_times(); |
|
| 772 | + return ! empty( $max_bills ) && $max_bills <= $this->get_times_billed(); |
|
| 773 | + } |
|
| 774 | + |
|
| 775 | + /* |
|
| 776 | 776 | |-------------------------------------------------------------------------- |
| 777 | 777 | | Additional methods |
| 778 | 778 | |-------------------------------------------------------------------------- |
@@ -781,27 +781,27 @@ discard block |
||
| 781 | 781 | | |
| 782 | 782 | */ |
| 783 | 783 | |
| 784 | - /** |
|
| 785 | - * Backwards compatibilty. |
|
| 786 | - */ |
|
| 787 | - public function create( $data = array() ) { |
|
| 784 | + /** |
|
| 785 | + * Backwards compatibilty. |
|
| 786 | + */ |
|
| 787 | + public function create( $data = array() ) { |
|
| 788 | 788 | |
| 789 | - // Set the properties. |
|
| 790 | - if ( is_array( $data ) ) { |
|
| 791 | - $this->set_props( $data ); |
|
| 792 | - } |
|
| 789 | + // Set the properties. |
|
| 790 | + if ( is_array( $data ) ) { |
|
| 791 | + $this->set_props( $data ); |
|
| 792 | + } |
|
| 793 | 793 | |
| 794 | - // Save the item. |
|
| 795 | - return $this->save(); |
|
| 794 | + // Save the item. |
|
| 795 | + return $this->save(); |
|
| 796 | 796 | |
| 797 | - } |
|
| 797 | + } |
|
| 798 | 798 | |
| 799 | - /** |
|
| 800 | - * Backwards compatibilty. |
|
| 801 | - */ |
|
| 802 | - public function update( $args = array() ) { |
|
| 803 | - return $this->create( $args ); |
|
| 804 | - } |
|
| 799 | + /** |
|
| 800 | + * Backwards compatibilty. |
|
| 801 | + */ |
|
| 802 | + public function update( $args = array() ) { |
|
| 803 | + return $this->create( $args ); |
|
| 804 | + } |
|
| 805 | 805 | |
| 806 | 806 | /** |
| 807 | 807 | * Retrieve renewal payments for a subscription |
@@ -811,22 +811,22 @@ discard block |
||
| 811 | 811 | */ |
| 812 | 812 | public function get_child_payments( $hide_pending = true ) { |
| 813 | 813 | |
| 814 | - $statuses = array( 'publish', 'wpi-processing', 'wpi-renewal' ); |
|
| 814 | + $statuses = array( 'publish', 'wpi-processing', 'wpi-renewal' ); |
|
| 815 | 815 | |
| 816 | - if ( ! $hide_pending ) { |
|
| 817 | - $statuses = array_keys( wpinv_get_invoice_statuses() ); |
|
| 818 | - } |
|
| 816 | + if ( ! $hide_pending ) { |
|
| 817 | + $statuses = array_keys( wpinv_get_invoice_statuses() ); |
|
| 818 | + } |
|
| 819 | 819 | |
| 820 | 820 | return get_posts( |
| 821 | - array( |
|
| 822 | - 'post_parent' => $this->get_parent_payment_id(), |
|
| 823 | - 'numberposts' => -1, |
|
| 824 | - 'post_status' => $statuses, |
|
| 825 | - 'orderby' => 'ID', |
|
| 826 | - 'order' => 'ASC', |
|
| 827 | - 'post_type' => 'wpi_invoice' |
|
| 828 | - ) |
|
| 829 | - ); |
|
| 821 | + array( |
|
| 822 | + 'post_parent' => $this->get_parent_payment_id(), |
|
| 823 | + 'numberposts' => -1, |
|
| 824 | + 'post_status' => $statuses, |
|
| 825 | + 'orderby' => 'ID', |
|
| 826 | + 'order' => 'ASC', |
|
| 827 | + 'post_type' => 'wpi_invoice' |
|
| 828 | + ) |
|
| 829 | + ); |
|
| 830 | 830 | } |
| 831 | 831 | |
| 832 | 832 | /** |
@@ -836,7 +836,7 @@ discard block |
||
| 836 | 836 | * @return int |
| 837 | 837 | */ |
| 838 | 838 | public function get_total_payments() { |
| 839 | - return getpaid_count_subscription_invoices( $this->get_parent_invoice_id(), $this->get_id() ); |
|
| 839 | + return getpaid_count_subscription_invoices( $this->get_parent_invoice_id(), $this->get_id() ); |
|
| 840 | 840 | } |
| 841 | 841 | |
| 842 | 842 | /** |
@@ -860,57 +860,57 @@ discard block |
||
| 860 | 860 | * |
| 861 | 861 | * @since 2.4 |
| 862 | 862 | * @param array $args Array of values for the payment, including amount and transaction ID |
| 863 | - * @param WPInv_Invoice $invoice If adding an existing invoice. |
|
| 863 | + * @param WPInv_Invoice $invoice If adding an existing invoice. |
|
| 864 | 864 | * @return bool |
| 865 | 865 | */ |
| 866 | 866 | public function add_payment( $args = array(), $invoice = false ) { |
| 867 | 867 | |
| 868 | - // Process each payment once. |
|
| 868 | + // Process each payment once. |
|
| 869 | 869 | if ( ! empty( $args['transaction_id'] ) && $this->payment_exists( $args['transaction_id'] ) ) { |
| 870 | 870 | return false; |
| 871 | 871 | } |
| 872 | 872 | |
| 873 | - // Are we creating a new invoice? |
|
| 874 | - if ( empty( $invoice ) ) { |
|
| 875 | - $invoice = $this->create_payment(); |
|
| 873 | + // Are we creating a new invoice? |
|
| 874 | + if ( empty( $invoice ) ) { |
|
| 875 | + $invoice = $this->create_payment(); |
|
| 876 | 876 | |
| 877 | - if ( empty( $invoice ) ) { |
|
| 878 | - return false; |
|
| 879 | - } |
|
| 877 | + if ( empty( $invoice ) ) { |
|
| 878 | + return false; |
|
| 879 | + } |
|
| 880 | 880 | |
| 881 | - } |
|
| 881 | + } |
|
| 882 | 882 | |
| 883 | - $invoice->set_status( 'wpi-renewal' ); |
|
| 883 | + $invoice->set_status( 'wpi-renewal' ); |
|
| 884 | 884 | |
| 885 | - // Maybe set a transaction id. |
|
| 886 | - if ( ! empty( $args['transaction_id'] ) ) { |
|
| 887 | - $invoice->set_transaction_id( $args['transaction_id'] ); |
|
| 888 | - } |
|
| 885 | + // Maybe set a transaction id. |
|
| 886 | + if ( ! empty( $args['transaction_id'] ) ) { |
|
| 887 | + $invoice->set_transaction_id( $args['transaction_id'] ); |
|
| 888 | + } |
|
| 889 | 889 | |
| 890 | - // Set the completed date. |
|
| 891 | - $invoice->set_completed_date( current_time( 'mysql' ) ); |
|
| 890 | + // Set the completed date. |
|
| 891 | + $invoice->set_completed_date( current_time( 'mysql' ) ); |
|
| 892 | 892 | |
| 893 | - // And the gateway. |
|
| 894 | - if ( ! empty( $args['gateway'] ) ) { |
|
| 895 | - $invoice->set_gateway( $args['gateway'] ); |
|
| 896 | - } |
|
| 893 | + // And the gateway. |
|
| 894 | + if ( ! empty( $args['gateway'] ) ) { |
|
| 895 | + $invoice->set_gateway( $args['gateway'] ); |
|
| 896 | + } |
|
| 897 | 897 | |
| 898 | - $invoice->save(); |
|
| 898 | + $invoice->save(); |
|
| 899 | 899 | |
| 900 | - if ( ! $invoice->exists() ) { |
|
| 901 | - return false; |
|
| 902 | - } |
|
| 900 | + if ( ! $invoice->exists() ) { |
|
| 901 | + return false; |
|
| 902 | + } |
|
| 903 | 903 | |
| 904 | - do_action( 'getpaid_after_create_subscription_renewal_invoice', $invoice, $this ); |
|
| 905 | - do_action( 'wpinv_recurring_add_subscription_payment', $invoice, $this ); |
|
| 904 | + do_action( 'getpaid_after_create_subscription_renewal_invoice', $invoice, $this ); |
|
| 905 | + do_action( 'wpinv_recurring_add_subscription_payment', $invoice, $this ); |
|
| 906 | 906 | do_action( 'wpinv_recurring_record_payment', $invoice->get_id(), $this->get_parent_invoice_id(), $invoice->get_recurring_total(), $invoice->get_transaction_id() ); |
| 907 | 907 | |
| 908 | 908 | update_post_meta( $invoice->get_id(), '_wpinv_subscription_id', $this->id ); |
| 909 | 909 | |
| 910 | 910 | return $invoice->get_id(); |
| 911 | - } |
|
| 911 | + } |
|
| 912 | 912 | |
| 913 | - /** |
|
| 913 | + /** |
|
| 914 | 914 | * Creates a new invoice and returns it. |
| 915 | 915 | * |
| 916 | 916 | * @since 1.0.19 |
@@ -918,124 +918,124 @@ discard block |
||
| 918 | 918 | */ |
| 919 | 919 | public function create_payment() { |
| 920 | 920 | |
| 921 | - $parent_invoice = $this->get_parent_payment(); |
|
| 922 | - |
|
| 923 | - if ( ! $parent_invoice->exists() ) { |
|
| 924 | - return false; |
|
| 925 | - } |
|
| 926 | - |
|
| 927 | - // Duplicate the parent invoice. |
|
| 928 | - $invoice = getpaid_duplicate_invoice( $parent_invoice ); |
|
| 929 | - $invoice->set_parent_id( $parent_invoice->get_id() ); |
|
| 930 | - $invoice->set_subscription_id( $this->get_id() ); |
|
| 931 | - $invoice->set_remote_subscription_id( $this->get_profile_id() ); |
|
| 932 | - |
|
| 933 | - // Set invoice items. |
|
| 934 | - $subscription_group = getpaid_get_invoice_subscription_group( $parent_invoice->get_id(), $this->get_id() ); |
|
| 935 | - $allowed_items = empty( $subscription_group ) ? array( $this->get_product_id() ) : array_keys( $subscription_group['items'] ); |
|
| 936 | - $invoice_items = array(); |
|
| 937 | - |
|
| 938 | - foreach ( $invoice->get_items() as $item ) { |
|
| 939 | - if ( in_array( $item->get_id(), $allowed_items ) ) { |
|
| 940 | - $invoice_items[] = $item; |
|
| 941 | - } |
|
| 942 | - } |
|
| 943 | - |
|
| 944 | - $invoice->set_items( $invoice_items ); |
|
| 945 | - |
|
| 946 | - if ( ! empty( $subscription_group['fees'] ) ) { |
|
| 947 | - $invoice->set_fees( $subscription_group['fees'] ); |
|
| 948 | - } |
|
| 949 | - |
|
| 950 | - // Maybe recalculate discount (Pre-GetPaid Fix). |
|
| 951 | - $discount = new WPInv_Discount( $invoice->get_discount_code() ); |
|
| 952 | - if ( $discount->exists() && $discount->is_recurring() && 0 == $invoice->get_total_discount() ) { |
|
| 953 | - $invoice->add_discount( getpaid_calculate_invoice_discount( $invoice, $discount ) ); |
|
| 954 | - } |
|
| 955 | - |
|
| 956 | - $invoice->recalculate_total(); |
|
| 957 | - $invoice->set_status( 'wpi-pending' ); |
|
| 958 | - $invoice->save(); |
|
| 959 | - |
|
| 960 | - return $invoice->exists() ? $invoice : false; |
|
| 961 | - } |
|
| 962 | - |
|
| 963 | - /** |
|
| 964 | - * Renews or completes a subscription |
|
| 965 | - * |
|
| 966 | - * @since 1.0.0 |
|
| 967 | - * @return int The subscription's id |
|
| 968 | - */ |
|
| 969 | - public function renew() { |
|
| 970 | - |
|
| 971 | - // Complete subscription if applicable |
|
| 972 | - if ( $this->is_last_renewal() ) { |
|
| 973 | - return $this->complete(); |
|
| 974 | - } |
|
| 975 | - |
|
| 976 | - // Calculate new expiration |
|
| 977 | - $frequency = $this->get_frequency(); |
|
| 978 | - $period = $this->get_period(); |
|
| 979 | - $new_expiration = strtotime( "+ $frequency $period", $this->get_expiration_time() ); |
|
| 980 | - |
|
| 981 | - $this->set_expiration( date( 'Y-m-d H:i:s',$new_expiration ) ); |
|
| 982 | - $this->set_status( 'active' ); |
|
| 983 | - $this->save(); |
|
| 984 | - |
|
| 985 | - do_action( 'getpaid_subscription_renewed', $this ); |
|
| 986 | - |
|
| 987 | - return $this->get_id(); |
|
| 988 | - } |
|
| 989 | - |
|
| 990 | - /** |
|
| 991 | - * Marks a subscription as completed |
|
| 992 | - * |
|
| 993 | - * Subscription is completed when the number of payments matches the billing_times field |
|
| 994 | - * |
|
| 995 | - * @since 1.0.0 |
|
| 996 | - * @return int|bool Subscription id or false if the subscription is cancelled. |
|
| 997 | - */ |
|
| 998 | - public function complete() { |
|
| 999 | - |
|
| 1000 | - // Only mark a subscription as complete if it's not already cancelled. |
|
| 1001 | - if ( $this->has_status( 'cancelled' ) ) { |
|
| 1002 | - return false; |
|
| 1003 | - } |
|
| 1004 | - |
|
| 1005 | - $this->set_status( 'completed' ); |
|
| 1006 | - return $this->save(); |
|
| 1007 | - |
|
| 1008 | - } |
|
| 1009 | - |
|
| 1010 | - /** |
|
| 1011 | - * Marks a subscription as expired |
|
| 1012 | - * |
|
| 1013 | - * @since 1.0.0 |
|
| 1014 | - * @param bool $check_expiration |
|
| 1015 | - * @return int|bool Subscription id or false if $check_expiration is true and expiration date is in the future. |
|
| 1016 | - */ |
|
| 1017 | - public function expire( $check_expiration = false ) { |
|
| 1018 | - |
|
| 1019 | - if ( $check_expiration && $this->get_expiration_time() > current_time( 'timestamp' ) ) { |
|
| 1020 | - // Do not mark as expired since real expiration date is in the future |
|
| 1021 | - return false; |
|
| 1022 | - } |
|
| 1023 | - |
|
| 1024 | - $this->set_status( 'expired' ); |
|
| 1025 | - return $this->save(); |
|
| 1026 | - |
|
| 1027 | - } |
|
| 1028 | - |
|
| 1029 | - /** |
|
| 1030 | - * Marks a subscription as failing |
|
| 1031 | - * |
|
| 1032 | - * @since 2.4.2 |
|
| 1033 | - * @return int Subscription id. |
|
| 1034 | - */ |
|
| 1035 | - public function failing() { |
|
| 1036 | - $this->set_status( 'failing' ); |
|
| 1037 | - return $this->save(); |
|
| 1038 | - } |
|
| 921 | + $parent_invoice = $this->get_parent_payment(); |
|
| 922 | + |
|
| 923 | + if ( ! $parent_invoice->exists() ) { |
|
| 924 | + return false; |
|
| 925 | + } |
|
| 926 | + |
|
| 927 | + // Duplicate the parent invoice. |
|
| 928 | + $invoice = getpaid_duplicate_invoice( $parent_invoice ); |
|
| 929 | + $invoice->set_parent_id( $parent_invoice->get_id() ); |
|
| 930 | + $invoice->set_subscription_id( $this->get_id() ); |
|
| 931 | + $invoice->set_remote_subscription_id( $this->get_profile_id() ); |
|
| 932 | + |
|
| 933 | + // Set invoice items. |
|
| 934 | + $subscription_group = getpaid_get_invoice_subscription_group( $parent_invoice->get_id(), $this->get_id() ); |
|
| 935 | + $allowed_items = empty( $subscription_group ) ? array( $this->get_product_id() ) : array_keys( $subscription_group['items'] ); |
|
| 936 | + $invoice_items = array(); |
|
| 937 | + |
|
| 938 | + foreach ( $invoice->get_items() as $item ) { |
|
| 939 | + if ( in_array( $item->get_id(), $allowed_items ) ) { |
|
| 940 | + $invoice_items[] = $item; |
|
| 941 | + } |
|
| 942 | + } |
|
| 943 | + |
|
| 944 | + $invoice->set_items( $invoice_items ); |
|
| 945 | + |
|
| 946 | + if ( ! empty( $subscription_group['fees'] ) ) { |
|
| 947 | + $invoice->set_fees( $subscription_group['fees'] ); |
|
| 948 | + } |
|
| 949 | + |
|
| 950 | + // Maybe recalculate discount (Pre-GetPaid Fix). |
|
| 951 | + $discount = new WPInv_Discount( $invoice->get_discount_code() ); |
|
| 952 | + if ( $discount->exists() && $discount->is_recurring() && 0 == $invoice->get_total_discount() ) { |
|
| 953 | + $invoice->add_discount( getpaid_calculate_invoice_discount( $invoice, $discount ) ); |
|
| 954 | + } |
|
| 955 | + |
|
| 956 | + $invoice->recalculate_total(); |
|
| 957 | + $invoice->set_status( 'wpi-pending' ); |
|
| 958 | + $invoice->save(); |
|
| 959 | + |
|
| 960 | + return $invoice->exists() ? $invoice : false; |
|
| 961 | + } |
|
| 962 | + |
|
| 963 | + /** |
|
| 964 | + * Renews or completes a subscription |
|
| 965 | + * |
|
| 966 | + * @since 1.0.0 |
|
| 967 | + * @return int The subscription's id |
|
| 968 | + */ |
|
| 969 | + public function renew() { |
|
| 970 | + |
|
| 971 | + // Complete subscription if applicable |
|
| 972 | + if ( $this->is_last_renewal() ) { |
|
| 973 | + return $this->complete(); |
|
| 974 | + } |
|
| 975 | + |
|
| 976 | + // Calculate new expiration |
|
| 977 | + $frequency = $this->get_frequency(); |
|
| 978 | + $period = $this->get_period(); |
|
| 979 | + $new_expiration = strtotime( "+ $frequency $period", $this->get_expiration_time() ); |
|
| 980 | + |
|
| 981 | + $this->set_expiration( date( 'Y-m-d H:i:s',$new_expiration ) ); |
|
| 982 | + $this->set_status( 'active' ); |
|
| 983 | + $this->save(); |
|
| 984 | + |
|
| 985 | + do_action( 'getpaid_subscription_renewed', $this ); |
|
| 986 | + |
|
| 987 | + return $this->get_id(); |
|
| 988 | + } |
|
| 989 | + |
|
| 990 | + /** |
|
| 991 | + * Marks a subscription as completed |
|
| 992 | + * |
|
| 993 | + * Subscription is completed when the number of payments matches the billing_times field |
|
| 994 | + * |
|
| 995 | + * @since 1.0.0 |
|
| 996 | + * @return int|bool Subscription id or false if the subscription is cancelled. |
|
| 997 | + */ |
|
| 998 | + public function complete() { |
|
| 999 | + |
|
| 1000 | + // Only mark a subscription as complete if it's not already cancelled. |
|
| 1001 | + if ( $this->has_status( 'cancelled' ) ) { |
|
| 1002 | + return false; |
|
| 1003 | + } |
|
| 1004 | + |
|
| 1005 | + $this->set_status( 'completed' ); |
|
| 1006 | + return $this->save(); |
|
| 1007 | + |
|
| 1008 | + } |
|
| 1009 | + |
|
| 1010 | + /** |
|
| 1011 | + * Marks a subscription as expired |
|
| 1012 | + * |
|
| 1013 | + * @since 1.0.0 |
|
| 1014 | + * @param bool $check_expiration |
|
| 1015 | + * @return int|bool Subscription id or false if $check_expiration is true and expiration date is in the future. |
|
| 1016 | + */ |
|
| 1017 | + public function expire( $check_expiration = false ) { |
|
| 1018 | + |
|
| 1019 | + if ( $check_expiration && $this->get_expiration_time() > current_time( 'timestamp' ) ) { |
|
| 1020 | + // Do not mark as expired since real expiration date is in the future |
|
| 1021 | + return false; |
|
| 1022 | + } |
|
| 1023 | + |
|
| 1024 | + $this->set_status( 'expired' ); |
|
| 1025 | + return $this->save(); |
|
| 1026 | + |
|
| 1027 | + } |
|
| 1028 | + |
|
| 1029 | + /** |
|
| 1030 | + * Marks a subscription as failing |
|
| 1031 | + * |
|
| 1032 | + * @since 2.4.2 |
|
| 1033 | + * @return int Subscription id. |
|
| 1034 | + */ |
|
| 1035 | + public function failing() { |
|
| 1036 | + $this->set_status( 'failing' ); |
|
| 1037 | + return $this->save(); |
|
| 1038 | + } |
|
| 1039 | 1039 | |
| 1040 | 1040 | /** |
| 1041 | 1041 | * Marks a subscription as cancelled |
@@ -1044,19 +1044,19 @@ discard block |
||
| 1044 | 1044 | * @return int Subscription id. |
| 1045 | 1045 | */ |
| 1046 | 1046 | public function cancel() { |
| 1047 | - $this->set_status( 'cancelled' ); |
|
| 1048 | - return $this->save(); |
|
| 1047 | + $this->set_status( 'cancelled' ); |
|
| 1048 | + return $this->save(); |
|
| 1049 | 1049 | } |
| 1050 | 1050 | |
| 1051 | - /** |
|
| 1052 | - * Determines if a subscription can be cancelled both locally and with a payment processor. |
|
| 1053 | - * |
|
| 1054 | - * @since 1.0.0 |
|
| 1055 | - * @return bool |
|
| 1056 | - */ |
|
| 1057 | - public function can_cancel() { |
|
| 1058 | - return apply_filters( 'wpinv_subscription_can_cancel', $this->has_status( $this->get_cancellable_statuses() ), $this ); |
|
| 1059 | - } |
|
| 1051 | + /** |
|
| 1052 | + * Determines if a subscription can be cancelled both locally and with a payment processor. |
|
| 1053 | + * |
|
| 1054 | + * @since 1.0.0 |
|
| 1055 | + * @return bool |
|
| 1056 | + */ |
|
| 1057 | + public function can_cancel() { |
|
| 1058 | + return apply_filters( 'wpinv_subscription_can_cancel', $this->has_status( $this->get_cancellable_statuses() ), $this ); |
|
| 1059 | + } |
|
| 1060 | 1060 | |
| 1061 | 1061 | /** |
| 1062 | 1062 | * Returns an array of subscription statuses that can be cancelled |
@@ -1069,96 +1069,96 @@ discard block |
||
| 1069 | 1069 | return apply_filters( 'wpinv_recurring_cancellable_statuses', array( 'active', 'trialling', 'failing' ) ); |
| 1070 | 1070 | } |
| 1071 | 1071 | |
| 1072 | - /** |
|
| 1073 | - * Retrieves the URL to cancel subscription |
|
| 1074 | - * |
|
| 1075 | - * @since 1.0.0 |
|
| 1076 | - * @return string |
|
| 1077 | - */ |
|
| 1078 | - public function get_cancel_url() { |
|
| 1079 | - $url = getpaid_get_authenticated_action_url( 'subscription_cancel', $this->get_view_url() ); |
|
| 1080 | - return apply_filters( 'wpinv_subscription_cancel_url', $url, $this ); |
|
| 1081 | - } |
|
| 1082 | - |
|
| 1083 | - /** |
|
| 1084 | - * Retrieves the URL to view a subscription |
|
| 1085 | - * |
|
| 1086 | - * @since 1.0.19 |
|
| 1087 | - * @return string |
|
| 1088 | - */ |
|
| 1089 | - public function get_view_url() { |
|
| 1090 | - |
|
| 1091 | - $url = getpaid_get_tab_url( 'gp-subscriptions', get_permalink( (int) wpinv_get_option( 'invoice_subscription_page' ) ) ); |
|
| 1092 | - $url = add_query_arg( 'subscription', $this->get_id(), $url ); |
|
| 1093 | - |
|
| 1094 | - return apply_filters( 'getpaid_get_subscription_view_url', $url, $this ); |
|
| 1095 | - } |
|
| 1096 | - |
|
| 1097 | - /** |
|
| 1098 | - * Determines if subscription can be manually renewed |
|
| 1099 | - * |
|
| 1100 | - * This method is filtered by payment gateways in order to return true on subscriptions |
|
| 1101 | - * that can be renewed manually |
|
| 1102 | - * |
|
| 1103 | - * @since 2.5 |
|
| 1104 | - * @return bool |
|
| 1105 | - */ |
|
| 1106 | - public function can_renew() { |
|
| 1107 | - return apply_filters( 'wpinv_subscription_can_renew', true, $this ); |
|
| 1108 | - } |
|
| 1109 | - |
|
| 1110 | - /** |
|
| 1111 | - * Retrieves the URL to renew a subscription |
|
| 1112 | - * |
|
| 1113 | - * @since 2.5 |
|
| 1114 | - * @return string |
|
| 1115 | - */ |
|
| 1116 | - public function get_renew_url() { |
|
| 1117 | - $url = wp_nonce_url( add_query_arg( array( 'getpaid-action' => 'renew_subscription', 'sub_id' => $this->get_id ) ), 'getpaid-nonce' ); |
|
| 1118 | - return apply_filters( 'wpinv_subscription_renew_url', $url, $this ); |
|
| 1119 | - } |
|
| 1120 | - |
|
| 1121 | - /** |
|
| 1122 | - * Determines if subscription can have their payment method updated |
|
| 1123 | - * |
|
| 1124 | - * @since 1.0.0 |
|
| 1125 | - * @return bool |
|
| 1126 | - */ |
|
| 1127 | - public function can_update() { |
|
| 1128 | - return apply_filters( 'wpinv_subscription_can_update', false, $this ); |
|
| 1129 | - } |
|
| 1130 | - |
|
| 1131 | - /** |
|
| 1132 | - * Retrieves the URL to update subscription |
|
| 1133 | - * |
|
| 1134 | - * @since 1.0.0 |
|
| 1135 | - * @return string |
|
| 1136 | - */ |
|
| 1137 | - public function get_update_url() { |
|
| 1138 | - $url = add_query_arg( array( 'action' => 'update', 'subscription_id' => $this->get_id() ) ); |
|
| 1139 | - return apply_filters( 'wpinv_subscription_update_url', $url, $this ); |
|
| 1140 | - } |
|
| 1141 | - |
|
| 1142 | - /** |
|
| 1143 | - * Retrieves the subscription status label |
|
| 1144 | - * |
|
| 1145 | - * @since 1.0.0 |
|
| 1146 | - * @return string |
|
| 1147 | - */ |
|
| 1148 | - public function get_status_label() { |
|
| 1149 | - return getpaid_get_subscription_status_label( $this->get_status() ); |
|
| 1150 | - } |
|
| 1151 | - |
|
| 1152 | - /** |
|
| 1153 | - * Retrieves the subscription status class |
|
| 1154 | - * |
|
| 1155 | - * @since 1.0.19 |
|
| 1156 | - * @return string |
|
| 1157 | - */ |
|
| 1158 | - public function get_status_class() { |
|
| 1159 | - $statuses = getpaid_get_subscription_status_classes(); |
|
| 1160 | - return isset( $statuses[ $this->get_status() ] ) ? $statuses[ $this->get_status() ] : 'badge-dark'; |
|
| 1161 | - } |
|
| 1072 | + /** |
|
| 1073 | + * Retrieves the URL to cancel subscription |
|
| 1074 | + * |
|
| 1075 | + * @since 1.0.0 |
|
| 1076 | + * @return string |
|
| 1077 | + */ |
|
| 1078 | + public function get_cancel_url() { |
|
| 1079 | + $url = getpaid_get_authenticated_action_url( 'subscription_cancel', $this->get_view_url() ); |
|
| 1080 | + return apply_filters( 'wpinv_subscription_cancel_url', $url, $this ); |
|
| 1081 | + } |
|
| 1082 | + |
|
| 1083 | + /** |
|
| 1084 | + * Retrieves the URL to view a subscription |
|
| 1085 | + * |
|
| 1086 | + * @since 1.0.19 |
|
| 1087 | + * @return string |
|
| 1088 | + */ |
|
| 1089 | + public function get_view_url() { |
|
| 1090 | + |
|
| 1091 | + $url = getpaid_get_tab_url( 'gp-subscriptions', get_permalink( (int) wpinv_get_option( 'invoice_subscription_page' ) ) ); |
|
| 1092 | + $url = add_query_arg( 'subscription', $this->get_id(), $url ); |
|
| 1093 | + |
|
| 1094 | + return apply_filters( 'getpaid_get_subscription_view_url', $url, $this ); |
|
| 1095 | + } |
|
| 1096 | + |
|
| 1097 | + /** |
|
| 1098 | + * Determines if subscription can be manually renewed |
|
| 1099 | + * |
|
| 1100 | + * This method is filtered by payment gateways in order to return true on subscriptions |
|
| 1101 | + * that can be renewed manually |
|
| 1102 | + * |
|
| 1103 | + * @since 2.5 |
|
| 1104 | + * @return bool |
|
| 1105 | + */ |
|
| 1106 | + public function can_renew() { |
|
| 1107 | + return apply_filters( 'wpinv_subscription_can_renew', true, $this ); |
|
| 1108 | + } |
|
| 1109 | + |
|
| 1110 | + /** |
|
| 1111 | + * Retrieves the URL to renew a subscription |
|
| 1112 | + * |
|
| 1113 | + * @since 2.5 |
|
| 1114 | + * @return string |
|
| 1115 | + */ |
|
| 1116 | + public function get_renew_url() { |
|
| 1117 | + $url = wp_nonce_url( add_query_arg( array( 'getpaid-action' => 'renew_subscription', 'sub_id' => $this->get_id ) ), 'getpaid-nonce' ); |
|
| 1118 | + return apply_filters( 'wpinv_subscription_renew_url', $url, $this ); |
|
| 1119 | + } |
|
| 1120 | + |
|
| 1121 | + /** |
|
| 1122 | + * Determines if subscription can have their payment method updated |
|
| 1123 | + * |
|
| 1124 | + * @since 1.0.0 |
|
| 1125 | + * @return bool |
|
| 1126 | + */ |
|
| 1127 | + public function can_update() { |
|
| 1128 | + return apply_filters( 'wpinv_subscription_can_update', false, $this ); |
|
| 1129 | + } |
|
| 1130 | + |
|
| 1131 | + /** |
|
| 1132 | + * Retrieves the URL to update subscription |
|
| 1133 | + * |
|
| 1134 | + * @since 1.0.0 |
|
| 1135 | + * @return string |
|
| 1136 | + */ |
|
| 1137 | + public function get_update_url() { |
|
| 1138 | + $url = add_query_arg( array( 'action' => 'update', 'subscription_id' => $this->get_id() ) ); |
|
| 1139 | + return apply_filters( 'wpinv_subscription_update_url', $url, $this ); |
|
| 1140 | + } |
|
| 1141 | + |
|
| 1142 | + /** |
|
| 1143 | + * Retrieves the subscription status label |
|
| 1144 | + * |
|
| 1145 | + * @since 1.0.0 |
|
| 1146 | + * @return string |
|
| 1147 | + */ |
|
| 1148 | + public function get_status_label() { |
|
| 1149 | + return getpaid_get_subscription_status_label( $this->get_status() ); |
|
| 1150 | + } |
|
| 1151 | + |
|
| 1152 | + /** |
|
| 1153 | + * Retrieves the subscription status class |
|
| 1154 | + * |
|
| 1155 | + * @since 1.0.19 |
|
| 1156 | + * @return string |
|
| 1157 | + */ |
|
| 1158 | + public function get_status_class() { |
|
| 1159 | + $statuses = getpaid_get_subscription_status_classes(); |
|
| 1160 | + return isset( $statuses[ $this->get_status() ] ) ? $statuses[ $this->get_status() ] : 'badge-dark'; |
|
| 1161 | + } |
|
| 1162 | 1162 | |
| 1163 | 1163 | /** |
| 1164 | 1164 | * Retrieves the subscription status label |
@@ -1168,11 +1168,11 @@ discard block |
||
| 1168 | 1168 | */ |
| 1169 | 1169 | public function get_status_label_html() { |
| 1170 | 1170 | |
| 1171 | - $status_label = sanitize_text_field( $this->get_status_label() ); |
|
| 1172 | - $class = esc_attr( $this->get_status_class() ); |
|
| 1173 | - $status = sanitize_html_class( $this->get_status() ); |
|
| 1171 | + $status_label = sanitize_text_field( $this->get_status_label() ); |
|
| 1172 | + $class = esc_attr( $this->get_status_class() ); |
|
| 1173 | + $status = sanitize_html_class( $this->get_status() ); |
|
| 1174 | 1174 | |
| 1175 | - return "<span class='bsui'><span class='badge $class $status'>$status_label</span></span>"; |
|
| 1175 | + return "<span class='bsui'><span class='badge $class $status'>$status_label</span></span>"; |
|
| 1176 | 1176 | } |
| 1177 | 1177 | |
| 1178 | 1178 | /** |
@@ -1183,75 +1183,75 @@ discard block |
||
| 1183 | 1183 | * @return bool |
| 1184 | 1184 | */ |
| 1185 | 1185 | public function payment_exists( $txn_id = '' ) { |
| 1186 | - $invoice_id = WPInv_Invoice::get_invoice_id_by_field( $txn_id, 'transaction_id' ); |
|
| 1186 | + $invoice_id = WPInv_Invoice::get_invoice_id_by_field( $txn_id, 'transaction_id' ); |
|
| 1187 | 1187 | return ! empty( $invoice_id ); |
| 1188 | - } |
|
| 1189 | - |
|
| 1190 | - /** |
|
| 1191 | - * Handle the status transition. |
|
| 1192 | - */ |
|
| 1193 | - protected function status_transition() { |
|
| 1194 | - $status_transition = $this->status_transition; |
|
| 1195 | - |
|
| 1196 | - // Reset status transition variable. |
|
| 1197 | - $this->status_transition = false; |
|
| 1198 | - |
|
| 1199 | - if ( $status_transition ) { |
|
| 1200 | - try { |
|
| 1201 | - |
|
| 1202 | - // Fire a hook for the status change. |
|
| 1203 | - do_action( 'wpinv_subscription_' . $status_transition['to'], $this->get_id(), $this, $status_transition ); |
|
| 1204 | - do_action( 'getpaid_subscription_' . $status_transition['to'], $this, $status_transition ); |
|
| 1205 | - |
|
| 1206 | - if ( ! empty( $status_transition['from'] ) ) { |
|
| 1207 | - |
|
| 1208 | - /* translators: 1: old subscription status 2: new subscription status */ |
|
| 1209 | - $transition_note = sprintf( __( 'Subscription status changed from %1$s to %2$s.', 'invoicing' ), getpaid_get_subscription_status_label( $status_transition['from'] ), getpaid_get_subscription_status_label( $status_transition['to'] ) ); |
|
| 1210 | - |
|
| 1211 | - // Note the transition occurred. |
|
| 1212 | - $this->get_parent_payment()->add_note( $transition_note, false, false, true ); |
|
| 1213 | - |
|
| 1214 | - // Fire another hook. |
|
| 1215 | - do_action( 'getpaid_subscription_status_' . $status_transition['from'] . '_to_' . $status_transition['to'], $this->get_id(), $this ); |
|
| 1216 | - do_action( 'getpaid_subscription_status_changed', $this, $status_transition['from'], $status_transition['to'] ); |
|
| 1217 | - |
|
| 1218 | - } else { |
|
| 1219 | - /* translators: %s: new invoice status */ |
|
| 1220 | - $transition_note = sprintf( __( 'Subscription status set to %s.', 'invoicing' ), getpaid_get_subscription_status_label( $status_transition['to'] ) ); |
|
| 1221 | - |
|
| 1222 | - // Note the transition occurred. |
|
| 1223 | - $this->get_parent_payment()->add_note( $transition_note, false, false, true ); |
|
| 1224 | - |
|
| 1225 | - } |
|
| 1226 | - } catch ( Exception $e ) { |
|
| 1227 | - $this->get_parent_payment()->add_note( __( 'Error during subscription status transition.', 'invoicing' ) . ' ' . $e->getMessage() ); |
|
| 1228 | - } |
|
| 1229 | - } |
|
| 1230 | - |
|
| 1231 | - } |
|
| 1232 | - |
|
| 1233 | - /** |
|
| 1234 | - * Save data to the database. |
|
| 1235 | - * |
|
| 1236 | - * @since 1.0.19 |
|
| 1237 | - * @return int subscription ID |
|
| 1238 | - */ |
|
| 1239 | - public function save() { |
|
| 1240 | - parent::save(); |
|
| 1241 | - $this->status_transition(); |
|
| 1242 | - return $this->get_id(); |
|
| 1243 | - } |
|
| 1244 | - |
|
| 1245 | - /** |
|
| 1246 | - * Activates a subscription. |
|
| 1247 | - * |
|
| 1248 | - * @since 1.0.19 |
|
| 1249 | - * @return int subscription ID |
|
| 1250 | - */ |
|
| 1251 | - public function activate() { |
|
| 1252 | - $status = 'trialling' == $this->get_status() ? 'trialling' : 'active'; |
|
| 1253 | - $this->set_status( $status ); |
|
| 1254 | - return $this->save(); |
|
| 1255 | - } |
|
| 1188 | + } |
|
| 1189 | + |
|
| 1190 | + /** |
|
| 1191 | + * Handle the status transition. |
|
| 1192 | + */ |
|
| 1193 | + protected function status_transition() { |
|
| 1194 | + $status_transition = $this->status_transition; |
|
| 1195 | + |
|
| 1196 | + // Reset status transition variable. |
|
| 1197 | + $this->status_transition = false; |
|
| 1198 | + |
|
| 1199 | + if ( $status_transition ) { |
|
| 1200 | + try { |
|
| 1201 | + |
|
| 1202 | + // Fire a hook for the status change. |
|
| 1203 | + do_action( 'wpinv_subscription_' . $status_transition['to'], $this->get_id(), $this, $status_transition ); |
|
| 1204 | + do_action( 'getpaid_subscription_' . $status_transition['to'], $this, $status_transition ); |
|
| 1205 | + |
|
| 1206 | + if ( ! empty( $status_transition['from'] ) ) { |
|
| 1207 | + |
|
| 1208 | + /* translators: 1: old subscription status 2: new subscription status */ |
|
| 1209 | + $transition_note = sprintf( __( 'Subscription status changed from %1$s to %2$s.', 'invoicing' ), getpaid_get_subscription_status_label( $status_transition['from'] ), getpaid_get_subscription_status_label( $status_transition['to'] ) ); |
|
| 1210 | + |
|
| 1211 | + // Note the transition occurred. |
|
| 1212 | + $this->get_parent_payment()->add_note( $transition_note, false, false, true ); |
|
| 1213 | + |
|
| 1214 | + // Fire another hook. |
|
| 1215 | + do_action( 'getpaid_subscription_status_' . $status_transition['from'] . '_to_' . $status_transition['to'], $this->get_id(), $this ); |
|
| 1216 | + do_action( 'getpaid_subscription_status_changed', $this, $status_transition['from'], $status_transition['to'] ); |
|
| 1217 | + |
|
| 1218 | + } else { |
|
| 1219 | + /* translators: %s: new invoice status */ |
|
| 1220 | + $transition_note = sprintf( __( 'Subscription status set to %s.', 'invoicing' ), getpaid_get_subscription_status_label( $status_transition['to'] ) ); |
|
| 1221 | + |
|
| 1222 | + // Note the transition occurred. |
|
| 1223 | + $this->get_parent_payment()->add_note( $transition_note, false, false, true ); |
|
| 1224 | + |
|
| 1225 | + } |
|
| 1226 | + } catch ( Exception $e ) { |
|
| 1227 | + $this->get_parent_payment()->add_note( __( 'Error during subscription status transition.', 'invoicing' ) . ' ' . $e->getMessage() ); |
|
| 1228 | + } |
|
| 1229 | + } |
|
| 1230 | + |
|
| 1231 | + } |
|
| 1232 | + |
|
| 1233 | + /** |
|
| 1234 | + * Save data to the database. |
|
| 1235 | + * |
|
| 1236 | + * @since 1.0.19 |
|
| 1237 | + * @return int subscription ID |
|
| 1238 | + */ |
|
| 1239 | + public function save() { |
|
| 1240 | + parent::save(); |
|
| 1241 | + $this->status_transition(); |
|
| 1242 | + return $this->get_id(); |
|
| 1243 | + } |
|
| 1244 | + |
|
| 1245 | + /** |
|
| 1246 | + * Activates a subscription. |
|
| 1247 | + * |
|
| 1248 | + * @since 1.0.19 |
|
| 1249 | + * @return int subscription ID |
|
| 1250 | + */ |
|
| 1251 | + public function activate() { |
|
| 1252 | + $status = 'trialling' == $this->get_status() ? 'trialling' : 'active'; |
|
| 1253 | + $this->set_status( $status ); |
|
| 1254 | + return $this->save(); |
|
| 1255 | + } |
|
| 1256 | 1256 | |
| 1257 | 1257 | } |