@@ -46,25 +46,25 @@ |
||
46 | 46 | parent::__construct( $options ); |
47 | 47 | } |
48 | 48 | |
49 | - /** |
|
50 | - * The Super block output function. |
|
51 | - * |
|
52 | - * @param array $args |
|
53 | - * @param array $widget_args |
|
54 | - * @param string $content |
|
55 | - * |
|
56 | - * @return mixed|string|bool |
|
57 | - */ |
|
49 | + /** |
|
50 | + * The Super block output function. |
|
51 | + * |
|
52 | + * @param array $args |
|
53 | + * @param array $widget_args |
|
54 | + * @param string $content |
|
55 | + * |
|
56 | + * @return mixed|string|bool |
|
57 | + */ |
|
58 | 58 | public function output( $args = array(), $widget_args = array(), $content = '' ) { |
59 | 59 | |
60 | - ob_start(); |
|
60 | + ob_start(); |
|
61 | 61 | |
62 | - do_action( 'wpinv_before_user_subscriptions' ); |
|
63 | - wpinv_get_template_part( 'wpinv-subscriptions-history' ); |
|
64 | - do_action( 'wpinv_after_user_subscriptions' ); |
|
62 | + do_action( 'wpinv_before_user_subscriptions' ); |
|
63 | + wpinv_get_template_part( 'wpinv-subscriptions-history' ); |
|
64 | + do_action( 'wpinv_after_user_subscriptions' ); |
|
65 | 65 | |
66 | - $output = ob_get_clean(); |
|
67 | - return trim($output); |
|
66 | + $output = ob_get_clean(); |
|
67 | + return trim($output); |
|
68 | 68 | |
69 | 69 | } |
70 | 70 |
@@ -44,25 +44,25 @@ |
||
44 | 44 | parent::__construct( $options ); |
45 | 45 | } |
46 | 46 | |
47 | - /** |
|
48 | - * The Super block output function. |
|
49 | - * |
|
50 | - * @param array $args |
|
51 | - * @param array $widget_args |
|
52 | - * @param string $content |
|
53 | - * |
|
54 | - * @return mixed|string|bool |
|
55 | - */ |
|
47 | + /** |
|
48 | + * The Super block output function. |
|
49 | + * |
|
50 | + * @param array $args |
|
51 | + * @param array $widget_args |
|
52 | + * @param string $content |
|
53 | + * |
|
54 | + * @return mixed|string|bool |
|
55 | + */ |
|
56 | 56 | public function output( $args = array(), $widget_args = array(), $content = '' ) { |
57 | 57 | |
58 | - ob_start(); |
|
58 | + ob_start(); |
|
59 | 59 | |
60 | - do_action( 'wpinv_success_content_before' ); |
|
61 | - echo wpinv_payment_receipt( $args ); |
|
62 | - do_action( 'wpinv_success_content_after' ); |
|
60 | + do_action( 'wpinv_success_content_before' ); |
|
61 | + echo wpinv_payment_receipt( $args ); |
|
62 | + do_action( 'wpinv_success_content_after' ); |
|
63 | 63 | |
64 | - $output = ob_get_clean(); |
|
65 | - return trim($output); |
|
64 | + $output = ob_get_clean(); |
|
65 | + return trim($output); |
|
66 | 66 | |
67 | 67 | } |
68 | 68 |
@@ -984,326 +984,326 @@ discard block |
||
984 | 984 | } |
985 | 985 | |
986 | 986 | function wpinv_get_pages( $with_slug = false, $default_label = NULL ) { |
987 | - $pages_options = array(); |
|
987 | + $pages_options = array(); |
|
988 | 988 | |
989 | - if( $default_label !== NULL && $default_label !== false ) { |
|
990 | - $pages_options = array( '' => $default_label ); // Blank option |
|
991 | - } |
|
989 | + if( $default_label !== NULL && $default_label !== false ) { |
|
990 | + $pages_options = array( '' => $default_label ); // Blank option |
|
991 | + } |
|
992 | 992 | |
993 | - $pages = get_pages(); |
|
994 | - if ( $pages ) { |
|
995 | - foreach ( $pages as $page ) { |
|
996 | - $title = $with_slug ? $page->post_title . ' (' . $page->post_name . ')' : $page->post_title; |
|
993 | + $pages = get_pages(); |
|
994 | + if ( $pages ) { |
|
995 | + foreach ( $pages as $page ) { |
|
996 | + $title = $with_slug ? $page->post_title . ' (' . $page->post_name . ')' : $page->post_title; |
|
997 | 997 | $pages_options[ $page->ID ] = $title; |
998 | - } |
|
999 | - } |
|
998 | + } |
|
999 | + } |
|
1000 | 1000 | |
1001 | - return $pages_options; |
|
1001 | + return $pages_options; |
|
1002 | 1002 | } |
1003 | 1003 | |
1004 | 1004 | function wpinv_header_callback( $args ) { |
1005 | - if ( !empty( $args['desc'] ) ) { |
|
1005 | + if ( !empty( $args['desc'] ) ) { |
|
1006 | 1006 | echo $args['desc']; |
1007 | 1007 | } |
1008 | 1008 | } |
1009 | 1009 | |
1010 | 1010 | function wpinv_hidden_callback( $args ) { |
1011 | - global $wpinv_options; |
|
1012 | - |
|
1013 | - if ( isset( $args['set_value'] ) ) { |
|
1014 | - $value = $args['set_value']; |
|
1015 | - } elseif ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
1016 | - $value = $wpinv_options[ $args['id'] ]; |
|
1017 | - } else { |
|
1018 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
1019 | - } |
|
1020 | - |
|
1021 | - if ( isset( $args['faux'] ) && true === $args['faux'] ) { |
|
1022 | - $args['readonly'] = true; |
|
1023 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
1024 | - $name = ''; |
|
1025 | - } else { |
|
1026 | - $name = 'name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"'; |
|
1027 | - } |
|
1028 | - |
|
1029 | - $html = '<input type="hidden" id="wpinv_settings[' . wpinv_sanitize_key( $args['id'] ) . ']" ' . $name . ' value="' . esc_attr( stripslashes( $value ) ) . '" />'; |
|
1011 | + global $wpinv_options; |
|
1012 | + |
|
1013 | + if ( isset( $args['set_value'] ) ) { |
|
1014 | + $value = $args['set_value']; |
|
1015 | + } elseif ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
1016 | + $value = $wpinv_options[ $args['id'] ]; |
|
1017 | + } else { |
|
1018 | + $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
1019 | + } |
|
1020 | + |
|
1021 | + if ( isset( $args['faux'] ) && true === $args['faux'] ) { |
|
1022 | + $args['readonly'] = true; |
|
1023 | + $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
1024 | + $name = ''; |
|
1025 | + } else { |
|
1026 | + $name = 'name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"'; |
|
1027 | + } |
|
1028 | + |
|
1029 | + $html = '<input type="hidden" id="wpinv_settings[' . wpinv_sanitize_key( $args['id'] ) . ']" ' . $name . ' value="' . esc_attr( stripslashes( $value ) ) . '" />'; |
|
1030 | 1030 | |
1031 | - echo $html; |
|
1031 | + echo $html; |
|
1032 | 1032 | } |
1033 | 1033 | |
1034 | 1034 | function wpinv_checkbox_callback( $args ) { |
1035 | - global $wpinv_options; |
|
1035 | + global $wpinv_options; |
|
1036 | 1036 | |
1037 | 1037 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
1038 | 1038 | |
1039 | - if ( isset( $args['faux'] ) && true === $args['faux'] ) { |
|
1040 | - $name = ''; |
|
1041 | - } else { |
|
1042 | - $name = 'name="wpinv_settings[' . $sanitize_id . ']"'; |
|
1043 | - } |
|
1039 | + if ( isset( $args['faux'] ) && true === $args['faux'] ) { |
|
1040 | + $name = ''; |
|
1041 | + } else { |
|
1042 | + $name = 'name="wpinv_settings[' . $sanitize_id . ']"'; |
|
1043 | + } |
|
1044 | 1044 | |
1045 | - $checked = isset( $wpinv_options[ $args['id'] ] ) ? checked( 1, $wpinv_options[ $args['id'] ], false ) : ''; |
|
1046 | - $html = '<input type="checkbox" id="wpinv_settings[' . $sanitize_id . ']"' . $name . ' value="1" ' . $checked . '/>'; |
|
1047 | - $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
1045 | + $checked = isset( $wpinv_options[ $args['id'] ] ) ? checked( 1, $wpinv_options[ $args['id'] ], false ) : ''; |
|
1046 | + $html = '<input type="checkbox" id="wpinv_settings[' . $sanitize_id . ']"' . $name . ' value="1" ' . $checked . '/>'; |
|
1047 | + $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
1048 | 1048 | |
1049 | - echo $html; |
|
1049 | + echo $html; |
|
1050 | 1050 | } |
1051 | 1051 | |
1052 | 1052 | function wpinv_multicheck_callback( $args ) { |
1053 | - global $wpinv_options; |
|
1053 | + global $wpinv_options; |
|
1054 | 1054 | |
1055 | - $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
|
1056 | - $class = !empty( $args['class'] ) ? ' ' . esc_attr( $args['class'] ) : ''; |
|
1055 | + $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
|
1056 | + $class = !empty( $args['class'] ) ? ' ' . esc_attr( $args['class'] ) : ''; |
|
1057 | 1057 | |
1058 | - if ( ! empty( $args['options'] ) ) { |
|
1059 | - echo '<div class="wpi-mcheck-rows wpi-mcheck-' . $sanitize_id . $class . '">'; |
|
1058 | + if ( ! empty( $args['options'] ) ) { |
|
1059 | + echo '<div class="wpi-mcheck-rows wpi-mcheck-' . $sanitize_id . $class . '">'; |
|
1060 | 1060 | foreach( $args['options'] as $key => $option ): |
1061 | - $sanitize_key = wpinv_sanitize_key( $key ); |
|
1062 | - if ( isset( $wpinv_options[$args['id']][$sanitize_key] ) ) { |
|
1063 | - $enabled = $sanitize_key; |
|
1064 | - } else { |
|
1065 | - $enabled = NULL; |
|
1066 | - } |
|
1067 | - echo '<div class="wpi-mcheck-row"><input name="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" id="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" type="checkbox" value="' . esc_attr( $sanitize_key ) . '" ' . checked( $sanitize_key, $enabled, false ) . '/> '; |
|
1068 | - echo '<label for="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']">' . wp_kses_post( $option ) . '</label></div>'; |
|
1069 | - endforeach; |
|
1070 | - echo '</div>'; |
|
1071 | - echo '<p class="description">' . $args['desc'] . '</p>'; |
|
1072 | - } |
|
1061 | + $sanitize_key = wpinv_sanitize_key( $key ); |
|
1062 | + if ( isset( $wpinv_options[$args['id']][$sanitize_key] ) ) { |
|
1063 | + $enabled = $sanitize_key; |
|
1064 | + } else { |
|
1065 | + $enabled = NULL; |
|
1066 | + } |
|
1067 | + echo '<div class="wpi-mcheck-row"><input name="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" id="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" type="checkbox" value="' . esc_attr( $sanitize_key ) . '" ' . checked( $sanitize_key, $enabled, false ) . '/> '; |
|
1068 | + echo '<label for="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']">' . wp_kses_post( $option ) . '</label></div>'; |
|
1069 | + endforeach; |
|
1070 | + echo '</div>'; |
|
1071 | + echo '<p class="description">' . $args['desc'] . '</p>'; |
|
1072 | + } |
|
1073 | 1073 | } |
1074 | 1074 | |
1075 | 1075 | function wpinv_payment_icons_callback( $args ) { |
1076 | - global $wpinv_options; |
|
1076 | + global $wpinv_options; |
|
1077 | 1077 | |
1078 | 1078 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
1079 | 1079 | |
1080 | - if ( ! empty( $args['options'] ) ) { |
|
1081 | - foreach( $args['options'] as $key => $option ) { |
|
1080 | + if ( ! empty( $args['options'] ) ) { |
|
1081 | + foreach( $args['options'] as $key => $option ) { |
|
1082 | 1082 | $sanitize_key = wpinv_sanitize_key( $key ); |
1083 | 1083 | |
1084 | - if( isset( $wpinv_options[$args['id']][$key] ) ) { |
|
1085 | - $enabled = $option; |
|
1086 | - } else { |
|
1087 | - $enabled = NULL; |
|
1088 | - } |
|
1089 | - |
|
1090 | - echo '<label for="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" style="margin-right:10px;line-height:16px;height:16px;display:inline-block;">'; |
|
1091 | - |
|
1092 | - echo '<input name="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" id="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" type="checkbox" value="' . esc_attr( $option ) . '" ' . checked( $option, $enabled, false ) . '/> '; |
|
1093 | - |
|
1094 | - if ( wpinv_string_is_image_url( $key ) ) { |
|
1095 | - echo '<img class="payment-icon" src="' . esc_url( $key ) . '" style="width:32px;height:24px;position:relative;top:6px;margin-right:5px;"/>'; |
|
1096 | - } else { |
|
1097 | - $card = strtolower( str_replace( ' ', '', $option ) ); |
|
1098 | - |
|
1099 | - if ( has_filter( 'wpinv_accepted_payment_' . $card . '_image' ) ) { |
|
1100 | - $image = apply_filters( 'wpinv_accepted_payment_' . $card . '_image', '' ); |
|
1101 | - } else { |
|
1102 | - $image = wpinv_locate_template( 'images' . DIRECTORY_SEPARATOR . 'icons' . DIRECTORY_SEPARATOR . $card . '.gif', false ); |
|
1103 | - $content_dir = WP_CONTENT_DIR; |
|
1104 | - |
|
1105 | - if ( function_exists( 'wp_normalize_path' ) ) { |
|
1106 | - // Replaces backslashes with forward slashes for Windows systems |
|
1107 | - $image = wp_normalize_path( $image ); |
|
1108 | - $content_dir = wp_normalize_path( $content_dir ); |
|
1109 | - } |
|
1110 | - |
|
1111 | - $image = str_replace( $content_dir, content_url(), $image ); |
|
1112 | - } |
|
1113 | - |
|
1114 | - echo '<img class="payment-icon" src="' . esc_url( $image ) . '" style="width:32px;height:24px;position:relative;top:6px;margin-right:5px;"/>'; |
|
1115 | - } |
|
1116 | - echo $option . '</label>'; |
|
1117 | - } |
|
1118 | - echo '<p class="description" style="margin-top:16px;">' . wp_kses_post( $args['desc'] ) . '</p>'; |
|
1119 | - } |
|
1084 | + if( isset( $wpinv_options[$args['id']][$key] ) ) { |
|
1085 | + $enabled = $option; |
|
1086 | + } else { |
|
1087 | + $enabled = NULL; |
|
1088 | + } |
|
1089 | + |
|
1090 | + echo '<label for="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" style="margin-right:10px;line-height:16px;height:16px;display:inline-block;">'; |
|
1091 | + |
|
1092 | + echo '<input name="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" id="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" type="checkbox" value="' . esc_attr( $option ) . '" ' . checked( $option, $enabled, false ) . '/> '; |
|
1093 | + |
|
1094 | + if ( wpinv_string_is_image_url( $key ) ) { |
|
1095 | + echo '<img class="payment-icon" src="' . esc_url( $key ) . '" style="width:32px;height:24px;position:relative;top:6px;margin-right:5px;"/>'; |
|
1096 | + } else { |
|
1097 | + $card = strtolower( str_replace( ' ', '', $option ) ); |
|
1098 | + |
|
1099 | + if ( has_filter( 'wpinv_accepted_payment_' . $card . '_image' ) ) { |
|
1100 | + $image = apply_filters( 'wpinv_accepted_payment_' . $card . '_image', '' ); |
|
1101 | + } else { |
|
1102 | + $image = wpinv_locate_template( 'images' . DIRECTORY_SEPARATOR . 'icons' . DIRECTORY_SEPARATOR . $card . '.gif', false ); |
|
1103 | + $content_dir = WP_CONTENT_DIR; |
|
1104 | + |
|
1105 | + if ( function_exists( 'wp_normalize_path' ) ) { |
|
1106 | + // Replaces backslashes with forward slashes for Windows systems |
|
1107 | + $image = wp_normalize_path( $image ); |
|
1108 | + $content_dir = wp_normalize_path( $content_dir ); |
|
1109 | + } |
|
1110 | + |
|
1111 | + $image = str_replace( $content_dir, content_url(), $image ); |
|
1112 | + } |
|
1113 | + |
|
1114 | + echo '<img class="payment-icon" src="' . esc_url( $image ) . '" style="width:32px;height:24px;position:relative;top:6px;margin-right:5px;"/>'; |
|
1115 | + } |
|
1116 | + echo $option . '</label>'; |
|
1117 | + } |
|
1118 | + echo '<p class="description" style="margin-top:16px;">' . wp_kses_post( $args['desc'] ) . '</p>'; |
|
1119 | + } |
|
1120 | 1120 | } |
1121 | 1121 | |
1122 | 1122 | function wpinv_radio_callback( $args ) { |
1123 | - global $wpinv_options; |
|
1123 | + global $wpinv_options; |
|
1124 | 1124 | |
1125 | 1125 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
1126 | 1126 | |
1127 | 1127 | foreach ( $args['options'] as $key => $option ) : |
1128 | - $sanitize_key = wpinv_sanitize_key( $key ); |
|
1128 | + $sanitize_key = wpinv_sanitize_key( $key ); |
|
1129 | 1129 | |
1130 | 1130 | $checked = false; |
1131 | 1131 | |
1132 | - if ( isset( $wpinv_options[ $args['id'] ] ) && $wpinv_options[ $args['id'] ] == $key ) |
|
1133 | - $checked = true; |
|
1134 | - elseif( isset( $args['std'] ) && $args['std'] == $key && ! isset( $wpinv_options[ $args['id'] ] ) ) |
|
1135 | - $checked = true; |
|
1132 | + if ( isset( $wpinv_options[ $args['id'] ] ) && $wpinv_options[ $args['id'] ] == $key ) |
|
1133 | + $checked = true; |
|
1134 | + elseif( isset( $args['std'] ) && $args['std'] == $key && ! isset( $wpinv_options[ $args['id'] ] ) ) |
|
1135 | + $checked = true; |
|
1136 | 1136 | |
1137 | - echo '<input name="wpinv_settings[' . $sanitize_id . ']" id="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" type="radio" value="' . $sanitize_key . '" ' . checked(true, $checked, false) . '/> '; |
|
1138 | - echo '<label for="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']">' . esc_html( $option ) . '</label><br/>'; |
|
1139 | - endforeach; |
|
1137 | + echo '<input name="wpinv_settings[' . $sanitize_id . ']" id="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" type="radio" value="' . $sanitize_key . '" ' . checked(true, $checked, false) . '/> '; |
|
1138 | + echo '<label for="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']">' . esc_html( $option ) . '</label><br/>'; |
|
1139 | + endforeach; |
|
1140 | 1140 | |
1141 | - echo '<p class="description">' . wp_kses_post( $args['desc'] ) . '</p>'; |
|
1141 | + echo '<p class="description">' . wp_kses_post( $args['desc'] ) . '</p>'; |
|
1142 | 1142 | } |
1143 | 1143 | |
1144 | 1144 | function wpinv_gateways_callback( $args ) { |
1145 | - global $wpinv_options; |
|
1145 | + global $wpinv_options; |
|
1146 | 1146 | |
1147 | 1147 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
1148 | 1148 | |
1149 | - foreach ( $args['options'] as $key => $option ) : |
|
1150 | - $sanitize_key = wpinv_sanitize_key( $key ); |
|
1149 | + foreach ( $args['options'] as $key => $option ) : |
|
1150 | + $sanitize_key = wpinv_sanitize_key( $key ); |
|
1151 | 1151 | |
1152 | 1152 | if ( isset( $wpinv_options['gateways'][ $key ] ) ) |
1153 | - $enabled = '1'; |
|
1154 | - else |
|
1155 | - $enabled = null; |
|
1153 | + $enabled = '1'; |
|
1154 | + else |
|
1155 | + $enabled = null; |
|
1156 | 1156 | |
1157 | - echo '<input name="wpinv_settings[' . esc_attr( $args['id'] ) . '][' . $sanitize_key . ']" id="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" type="checkbox" value="1" ' . checked('1', $enabled, false) . '/> '; |
|
1158 | - echo '<label for="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']">' . esc_html( $option['admin_label'] ) . '</label><br/>'; |
|
1159 | - endforeach; |
|
1157 | + echo '<input name="wpinv_settings[' . esc_attr( $args['id'] ) . '][' . $sanitize_key . ']" id="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']" type="checkbox" value="1" ' . checked('1', $enabled, false) . '/> '; |
|
1158 | + echo '<label for="wpinv_settings[' . $sanitize_id . '][' . $sanitize_key . ']">' . esc_html( $option['admin_label'] ) . '</label><br/>'; |
|
1159 | + endforeach; |
|
1160 | 1160 | } |
1161 | 1161 | |
1162 | 1162 | function wpinv_gateway_select_callback($args) { |
1163 | - global $wpinv_options; |
|
1163 | + global $wpinv_options; |
|
1164 | 1164 | |
1165 | 1165 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
1166 | 1166 | $class = !empty( $args['class'] ) ? ' ' . esc_attr( $args['class'] ) : ''; |
1167 | 1167 | |
1168 | - echo '<select name="wpinv_settings[' . $sanitize_id . ']"" id="wpinv_settings[' . $sanitize_id . ']" class="'.$class.'" >'; |
|
1168 | + echo '<select name="wpinv_settings[' . $sanitize_id . ']"" id="wpinv_settings[' . $sanitize_id . ']" class="'.$class.'" >'; |
|
1169 | 1169 | |
1170 | - foreach ( $args['options'] as $key => $option ) : |
|
1171 | - if ( isset( $args['selected'] ) && $args['selected'] !== null && $args['selected'] !== false ) { |
|
1170 | + foreach ( $args['options'] as $key => $option ) : |
|
1171 | + if ( isset( $args['selected'] ) && $args['selected'] !== null && $args['selected'] !== false ) { |
|
1172 | 1172 | $selected = selected( $key, $args['selected'], false ); |
1173 | 1173 | } else { |
1174 | 1174 | $selected = isset( $wpinv_options[ $args['id'] ] ) ? selected( $key, $wpinv_options[$args['id']], false ) : ''; |
1175 | 1175 | } |
1176 | - echo '<option value="' . wpinv_sanitize_key( $key ) . '"' . $selected . '>' . esc_html( $option['admin_label'] ) . '</option>'; |
|
1177 | - endforeach; |
|
1176 | + echo '<option value="' . wpinv_sanitize_key( $key ) . '"' . $selected . '>' . esc_html( $option['admin_label'] ) . '</option>'; |
|
1177 | + endforeach; |
|
1178 | 1178 | |
1179 | - echo '</select>'; |
|
1180 | - echo '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
1179 | + echo '</select>'; |
|
1180 | + echo '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
1181 | 1181 | } |
1182 | 1182 | |
1183 | 1183 | function wpinv_text_callback( $args ) { |
1184 | - global $wpinv_options; |
|
1184 | + global $wpinv_options; |
|
1185 | 1185 | |
1186 | 1186 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
1187 | 1187 | |
1188 | - if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
1189 | - $value = $wpinv_options[ $args['id'] ]; |
|
1190 | - } else { |
|
1191 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
1192 | - } |
|
1193 | - |
|
1194 | - if ( isset( $args['faux'] ) && true === $args['faux'] ) { |
|
1195 | - $args['readonly'] = true; |
|
1196 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
1197 | - $name = ''; |
|
1198 | - } else { |
|
1199 | - $name = 'name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"'; |
|
1200 | - } |
|
1201 | - $class = !empty( $args['class'] ) ? sanitize_html_class( $args['class'] ) : ''; |
|
1202 | - |
|
1203 | - $readonly = $args['readonly'] === true ? ' readonly="readonly"' : ''; |
|
1204 | - $size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular'; |
|
1205 | - $html = '<input type="text" class="' . sanitize_html_class( $size ) . '-text ' . $class . '" id="wpinv_settings[' . $sanitize_id . ']" ' . $name . ' value="' . esc_attr( stripslashes( $value ) ) . '"' . $readonly . '/>'; |
|
1206 | - $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
1207 | - |
|
1208 | - echo $html; |
|
1188 | + if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
1189 | + $value = $wpinv_options[ $args['id'] ]; |
|
1190 | + } else { |
|
1191 | + $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
1192 | + } |
|
1193 | + |
|
1194 | + if ( isset( $args['faux'] ) && true === $args['faux'] ) { |
|
1195 | + $args['readonly'] = true; |
|
1196 | + $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
1197 | + $name = ''; |
|
1198 | + } else { |
|
1199 | + $name = 'name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"'; |
|
1200 | + } |
|
1201 | + $class = !empty( $args['class'] ) ? sanitize_html_class( $args['class'] ) : ''; |
|
1202 | + |
|
1203 | + $readonly = $args['readonly'] === true ? ' readonly="readonly"' : ''; |
|
1204 | + $size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular'; |
|
1205 | + $html = '<input type="text" class="' . sanitize_html_class( $size ) . '-text ' . $class . '" id="wpinv_settings[' . $sanitize_id . ']" ' . $name . ' value="' . esc_attr( stripslashes( $value ) ) . '"' . $readonly . '/>'; |
|
1206 | + $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
1207 | + |
|
1208 | + echo $html; |
|
1209 | 1209 | } |
1210 | 1210 | |
1211 | 1211 | function wpinv_number_callback( $args ) { |
1212 | - global $wpinv_options; |
|
1212 | + global $wpinv_options; |
|
1213 | 1213 | |
1214 | 1214 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
1215 | 1215 | |
1216 | - if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
1217 | - $value = $wpinv_options[ $args['id'] ]; |
|
1218 | - } else { |
|
1219 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
1220 | - } |
|
1221 | - |
|
1222 | - if ( isset( $args['faux'] ) && true === $args['faux'] ) { |
|
1223 | - $args['readonly'] = true; |
|
1224 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
1225 | - $name = ''; |
|
1226 | - } else { |
|
1227 | - $name = 'name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"'; |
|
1228 | - } |
|
1229 | - |
|
1230 | - $max = isset( $args['max'] ) ? $args['max'] : 999999; |
|
1231 | - $min = isset( $args['min'] ) ? $args['min'] : 0; |
|
1232 | - $step = isset( $args['step'] ) ? $args['step'] : 1; |
|
1233 | - $class = !empty( $args['class'] ) ? sanitize_html_class( $args['class'] ) : ''; |
|
1234 | - |
|
1235 | - $size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular'; |
|
1236 | - $html = '<input type="number" step="' . esc_attr( $step ) . '" max="' . esc_attr( $max ) . '" min="' . esc_attr( $min ) . '" class="' . sanitize_html_class( $size ) . '-text ' . $class . '" id="wpinv_settings[' . $sanitize_id . ']" ' . $name . ' value="' . esc_attr( stripslashes( $value ) ) . '"/>'; |
|
1237 | - $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
1238 | - |
|
1239 | - echo $html; |
|
1216 | + if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
1217 | + $value = $wpinv_options[ $args['id'] ]; |
|
1218 | + } else { |
|
1219 | + $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
1220 | + } |
|
1221 | + |
|
1222 | + if ( isset( $args['faux'] ) && true === $args['faux'] ) { |
|
1223 | + $args['readonly'] = true; |
|
1224 | + $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
1225 | + $name = ''; |
|
1226 | + } else { |
|
1227 | + $name = 'name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"'; |
|
1228 | + } |
|
1229 | + |
|
1230 | + $max = isset( $args['max'] ) ? $args['max'] : 999999; |
|
1231 | + $min = isset( $args['min'] ) ? $args['min'] : 0; |
|
1232 | + $step = isset( $args['step'] ) ? $args['step'] : 1; |
|
1233 | + $class = !empty( $args['class'] ) ? sanitize_html_class( $args['class'] ) : ''; |
|
1234 | + |
|
1235 | + $size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular'; |
|
1236 | + $html = '<input type="number" step="' . esc_attr( $step ) . '" max="' . esc_attr( $max ) . '" min="' . esc_attr( $min ) . '" class="' . sanitize_html_class( $size ) . '-text ' . $class . '" id="wpinv_settings[' . $sanitize_id . ']" ' . $name . ' value="' . esc_attr( stripslashes( $value ) ) . '"/>'; |
|
1237 | + $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
1238 | + |
|
1239 | + echo $html; |
|
1240 | 1240 | } |
1241 | 1241 | |
1242 | 1242 | function wpinv_textarea_callback( $args ) { |
1243 | - global $wpinv_options; |
|
1243 | + global $wpinv_options; |
|
1244 | 1244 | |
1245 | 1245 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
1246 | 1246 | |
1247 | - if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
1248 | - $value = $wpinv_options[ $args['id'] ]; |
|
1249 | - } else { |
|
1250 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
1251 | - } |
|
1247 | + if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
1248 | + $value = $wpinv_options[ $args['id'] ]; |
|
1249 | + } else { |
|
1250 | + $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
1251 | + } |
|
1252 | 1252 | |
1253 | 1253 | $size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular'; |
1254 | 1254 | $class = ( isset( $args['class'] ) && ! is_null( $args['class'] ) ) ? $args['class'] : 'large-text'; |
1255 | 1255 | |
1256 | - $html = '<textarea class="' . sanitize_html_class( $class ) . ' txtarea-' . sanitize_html_class( $size ) . ' wpi-' . esc_attr( sanitize_html_class( $sanitize_id ) ) . ' " cols="' . $args['cols'] . '" rows="' . $args['rows'] . '" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']">' . esc_textarea( stripslashes( $value ) ) . '</textarea>'; |
|
1257 | - $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
1256 | + $html = '<textarea class="' . sanitize_html_class( $class ) . ' txtarea-' . sanitize_html_class( $size ) . ' wpi-' . esc_attr( sanitize_html_class( $sanitize_id ) ) . ' " cols="' . $args['cols'] . '" rows="' . $args['rows'] . '" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']">' . esc_textarea( stripslashes( $value ) ) . '</textarea>'; |
|
1257 | + $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
1258 | 1258 | |
1259 | - echo $html; |
|
1259 | + echo $html; |
|
1260 | 1260 | } |
1261 | 1261 | |
1262 | 1262 | function wpinv_password_callback( $args ) { |
1263 | - global $wpinv_options; |
|
1263 | + global $wpinv_options; |
|
1264 | 1264 | |
1265 | 1265 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
1266 | 1266 | |
1267 | - if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
1268 | - $value = $wpinv_options[ $args['id'] ]; |
|
1269 | - } else { |
|
1270 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
1271 | - } |
|
1267 | + if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
1268 | + $value = $wpinv_options[ $args['id'] ]; |
|
1269 | + } else { |
|
1270 | + $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
1271 | + } |
|
1272 | 1272 | |
1273 | - $size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular'; |
|
1274 | - $html = '<input type="password" class="' . sanitize_html_class( $size ) . '-text" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" value="' . esc_attr( $value ) . '"/>'; |
|
1275 | - $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
1273 | + $size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular'; |
|
1274 | + $html = '<input type="password" class="' . sanitize_html_class( $size ) . '-text" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" value="' . esc_attr( $value ) . '"/>'; |
|
1275 | + $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
1276 | 1276 | |
1277 | - echo $html; |
|
1277 | + echo $html; |
|
1278 | 1278 | } |
1279 | 1279 | |
1280 | 1280 | function wpinv_missing_callback($args) { |
1281 | - printf( |
|
1282 | - __( 'The callback function used for the %s setting is missing.', 'invoicing' ), |
|
1283 | - '<strong>' . $args['id'] . '</strong>' |
|
1284 | - ); |
|
1281 | + printf( |
|
1282 | + __( 'The callback function used for the %s setting is missing.', 'invoicing' ), |
|
1283 | + '<strong>' . $args['id'] . '</strong>' |
|
1284 | + ); |
|
1285 | 1285 | } |
1286 | 1286 | |
1287 | 1287 | function wpinv_select_callback($args) { |
1288 | - global $wpinv_options; |
|
1288 | + global $wpinv_options; |
|
1289 | 1289 | |
1290 | 1290 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
1291 | 1291 | |
1292 | - if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
1293 | - $value = $wpinv_options[ $args['id'] ]; |
|
1294 | - } else { |
|
1295 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
1296 | - } |
|
1292 | + if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
1293 | + $value = $wpinv_options[ $args['id'] ]; |
|
1294 | + } else { |
|
1295 | + $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
1296 | + } |
|
1297 | 1297 | |
1298 | 1298 | if ( isset( $args['selected'] ) && $args['selected'] !== null && $args['selected'] !== false ) { |
1299 | 1299 | $value = $args['selected']; |
1300 | 1300 | } |
1301 | 1301 | |
1302 | - if ( isset( $args['placeholder'] ) ) { |
|
1303 | - $placeholder = $args['placeholder']; |
|
1304 | - } else { |
|
1305 | - $placeholder = ''; |
|
1306 | - } |
|
1302 | + if ( isset( $args['placeholder'] ) ) { |
|
1303 | + $placeholder = $args['placeholder']; |
|
1304 | + } else { |
|
1305 | + $placeholder = ''; |
|
1306 | + } |
|
1307 | 1307 | |
1308 | 1308 | if( !empty( $args['onchange'] ) ) { |
1309 | 1309 | $onchange = ' onchange="' . esc_attr( $args['onchange'] ) . '"'; |
@@ -1313,142 +1313,142 @@ discard block |
||
1313 | 1313 | |
1314 | 1314 | $class = !empty( $args['class'] ) ? ' ' . esc_attr( $args['class'] ) : ''; |
1315 | 1315 | |
1316 | - $html = '<select id="wpinv_settings[' . $sanitize_id . ']" class="'.$class.'" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" data-placeholder="' . esc_html( $placeholder ) . '"' . $onchange . ' />'; |
|
1316 | + $html = '<select id="wpinv_settings[' . $sanitize_id . ']" class="'.$class.'" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" data-placeholder="' . esc_html( $placeholder ) . '"' . $onchange . ' />'; |
|
1317 | 1317 | |
1318 | - foreach ( $args['options'] as $option => $name ) { |
|
1319 | - $selected = selected( $option, $value, false ); |
|
1320 | - $html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( $name ) . '</option>'; |
|
1321 | - } |
|
1318 | + foreach ( $args['options'] as $option => $name ) { |
|
1319 | + $selected = selected( $option, $value, false ); |
|
1320 | + $html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( $name ) . '</option>'; |
|
1321 | + } |
|
1322 | 1322 | |
1323 | - $html .= '</select>'; |
|
1324 | - $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
1323 | + $html .= '</select>'; |
|
1324 | + $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
1325 | 1325 | |
1326 | - echo $html; |
|
1326 | + echo $html; |
|
1327 | 1327 | } |
1328 | 1328 | |
1329 | 1329 | function wpinv_color_select_callback( $args ) { |
1330 | - global $wpinv_options; |
|
1330 | + global $wpinv_options; |
|
1331 | 1331 | |
1332 | 1332 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
1333 | 1333 | |
1334 | - if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
1335 | - $value = $wpinv_options[ $args['id'] ]; |
|
1336 | - } else { |
|
1337 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
1338 | - } |
|
1334 | + if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
1335 | + $value = $wpinv_options[ $args['id'] ]; |
|
1336 | + } else { |
|
1337 | + $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
1338 | + } |
|
1339 | 1339 | |
1340 | - $html = '<select id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"/>'; |
|
1340 | + $html = '<select id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"/>'; |
|
1341 | 1341 | |
1342 | - foreach ( $args['options'] as $option => $color ) { |
|
1343 | - $selected = selected( $option, $value, false ); |
|
1344 | - $html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( $color['label'] ) . '</option>'; |
|
1345 | - } |
|
1342 | + foreach ( $args['options'] as $option => $color ) { |
|
1343 | + $selected = selected( $option, $value, false ); |
|
1344 | + $html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( $color['label'] ) . '</option>'; |
|
1345 | + } |
|
1346 | 1346 | |
1347 | - $html .= '</select>'; |
|
1348 | - $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
1347 | + $html .= '</select>'; |
|
1348 | + $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
1349 | 1349 | |
1350 | - echo $html; |
|
1350 | + echo $html; |
|
1351 | 1351 | } |
1352 | 1352 | |
1353 | 1353 | function wpinv_rich_editor_callback( $args ) { |
1354 | - global $wpinv_options, $wp_version; |
|
1354 | + global $wpinv_options, $wp_version; |
|
1355 | 1355 | |
1356 | 1356 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
1357 | 1357 | |
1358 | - if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
1359 | - $value = $wpinv_options[ $args['id'] ]; |
|
1358 | + if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
1359 | + $value = $wpinv_options[ $args['id'] ]; |
|
1360 | 1360 | |
1361 | - if( empty( $args['allow_blank'] ) && empty( $value ) ) { |
|
1362 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
1363 | - } |
|
1364 | - } else { |
|
1365 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
1366 | - } |
|
1361 | + if( empty( $args['allow_blank'] ) && empty( $value ) ) { |
|
1362 | + $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
1363 | + } |
|
1364 | + } else { |
|
1365 | + $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
1366 | + } |
|
1367 | 1367 | |
1368 | - $rows = isset( $args['size'] ) ? $args['size'] : 20; |
|
1368 | + $rows = isset( $args['size'] ) ? $args['size'] : 20; |
|
1369 | 1369 | |
1370 | - if ( $wp_version >= 3.3 && function_exists( 'wp_editor' ) ) { |
|
1371 | - ob_start(); |
|
1372 | - wp_editor( stripslashes( $value ), 'wpinv_settings_' . esc_attr( $args['id'] ), array( 'textarea_name' => 'wpinv_settings[' . esc_attr( $args['id'] ) . ']', 'textarea_rows' => absint( $rows ), 'media_buttons' => false ) ); |
|
1373 | - $html = ob_get_clean(); |
|
1374 | - } else { |
|
1375 | - $html = '<textarea class="large-text" rows="10" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" class="wpi-' . esc_attr( sanitize_html_class( $args['id'] ) ) . '">' . esc_textarea( stripslashes( $value ) ) . '</textarea>'; |
|
1376 | - } |
|
1370 | + if ( $wp_version >= 3.3 && function_exists( 'wp_editor' ) ) { |
|
1371 | + ob_start(); |
|
1372 | + wp_editor( stripslashes( $value ), 'wpinv_settings_' . esc_attr( $args['id'] ), array( 'textarea_name' => 'wpinv_settings[' . esc_attr( $args['id'] ) . ']', 'textarea_rows' => absint( $rows ), 'media_buttons' => false ) ); |
|
1373 | + $html = ob_get_clean(); |
|
1374 | + } else { |
|
1375 | + $html = '<textarea class="large-text" rows="10" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" class="wpi-' . esc_attr( sanitize_html_class( $args['id'] ) ) . '">' . esc_textarea( stripslashes( $value ) ) . '</textarea>'; |
|
1376 | + } |
|
1377 | 1377 | |
1378 | - $html .= '<br/><label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
1378 | + $html .= '<br/><label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
1379 | 1379 | |
1380 | - echo $html; |
|
1380 | + echo $html; |
|
1381 | 1381 | } |
1382 | 1382 | |
1383 | 1383 | function wpinv_upload_callback( $args ) { |
1384 | - global $wpinv_options; |
|
1384 | + global $wpinv_options; |
|
1385 | 1385 | |
1386 | 1386 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
1387 | 1387 | |
1388 | - if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
1389 | - $value = $wpinv_options[$args['id']]; |
|
1390 | - } else { |
|
1391 | - $value = isset($args['std']) ? $args['std'] : ''; |
|
1392 | - } |
|
1388 | + if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
1389 | + $value = $wpinv_options[$args['id']]; |
|
1390 | + } else { |
|
1391 | + $value = isset($args['std']) ? $args['std'] : ''; |
|
1392 | + } |
|
1393 | 1393 | |
1394 | - $size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular'; |
|
1395 | - $html = '<input type="text" class="' . sanitize_html_class( $size ) . '-text" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" value="' . esc_attr( stripslashes( $value ) ) . '"/>'; |
|
1396 | - $html .= '<span> <input type="button" class="wpinv_settings_upload_button button-secondary" value="' . __( 'Upload File', 'invoicing' ) . '"/></span>'; |
|
1397 | - $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
1394 | + $size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular'; |
|
1395 | + $html = '<input type="text" class="' . sanitize_html_class( $size ) . '-text" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" value="' . esc_attr( stripslashes( $value ) ) . '"/>'; |
|
1396 | + $html .= '<span> <input type="button" class="wpinv_settings_upload_button button-secondary" value="' . __( 'Upload File', 'invoicing' ) . '"/></span>'; |
|
1397 | + $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
1398 | 1398 | |
1399 | - echo $html; |
|
1399 | + echo $html; |
|
1400 | 1400 | } |
1401 | 1401 | |
1402 | 1402 | function wpinv_color_callback( $args ) { |
1403 | - global $wpinv_options; |
|
1403 | + global $wpinv_options; |
|
1404 | 1404 | |
1405 | 1405 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
1406 | 1406 | |
1407 | - if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
1408 | - $value = $wpinv_options[ $args['id'] ]; |
|
1409 | - } else { |
|
1410 | - $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
1411 | - } |
|
1407 | + if ( isset( $wpinv_options[ $args['id'] ] ) ) { |
|
1408 | + $value = $wpinv_options[ $args['id'] ]; |
|
1409 | + } else { |
|
1410 | + $value = isset( $args['std'] ) ? $args['std'] : ''; |
|
1411 | + } |
|
1412 | 1412 | |
1413 | - $default = isset( $args['std'] ) ? $args['std'] : ''; |
|
1413 | + $default = isset( $args['std'] ) ? $args['std'] : ''; |
|
1414 | 1414 | |
1415 | - $html = '<input type="text" class="wpinv-color-picker" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" value="' . esc_attr( $value ) . '" data-default-color="' . esc_attr( $default ) . '" />'; |
|
1416 | - $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
1415 | + $html = '<input type="text" class="wpinv-color-picker" id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']" value="' . esc_attr( $value ) . '" data-default-color="' . esc_attr( $default ) . '" />'; |
|
1416 | + $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
1417 | 1417 | |
1418 | - echo $html; |
|
1418 | + echo $html; |
|
1419 | 1419 | } |
1420 | 1420 | |
1421 | 1421 | function wpinv_country_states_callback($args) { |
1422 | - global $wpinv_options; |
|
1422 | + global $wpinv_options; |
|
1423 | 1423 | |
1424 | 1424 | $sanitize_id = wpinv_sanitize_key( $args['id'] ); |
1425 | 1425 | |
1426 | - if ( isset( $args['placeholder'] ) ) { |
|
1427 | - $placeholder = $args['placeholder']; |
|
1428 | - } else { |
|
1429 | - $placeholder = ''; |
|
1430 | - } |
|
1426 | + if ( isset( $args['placeholder'] ) ) { |
|
1427 | + $placeholder = $args['placeholder']; |
|
1428 | + } else { |
|
1429 | + $placeholder = ''; |
|
1430 | + } |
|
1431 | 1431 | |
1432 | - $states = wpinv_get_country_states(); |
|
1432 | + $states = wpinv_get_country_states(); |
|
1433 | 1433 | |
1434 | - $class = empty( $states ) ? ' class="wpinv-no-states"' : ' class="wpi_select2"'; |
|
1435 | - $html = '<select id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"' . $class . 'data-placeholder="' . esc_html( $placeholder ) . '"/>'; |
|
1434 | + $class = empty( $states ) ? ' class="wpinv-no-states"' : ' class="wpi_select2"'; |
|
1435 | + $html = '<select id="wpinv_settings[' . $sanitize_id . ']" name="wpinv_settings[' . esc_attr( $args['id'] ) . ']"' . $class . 'data-placeholder="' . esc_html( $placeholder ) . '"/>'; |
|
1436 | 1436 | |
1437 | - foreach ( $states as $option => $name ) { |
|
1438 | - $selected = isset( $wpinv_options[ $args['id'] ] ) ? selected( $option, $wpinv_options[$args['id']], false ) : ''; |
|
1439 | - $html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( $name ) . '</option>'; |
|
1440 | - } |
|
1437 | + foreach ( $states as $option => $name ) { |
|
1438 | + $selected = isset( $wpinv_options[ $args['id'] ] ) ? selected( $option, $wpinv_options[$args['id']], false ) : ''; |
|
1439 | + $html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( $name ) . '</option>'; |
|
1440 | + } |
|
1441 | 1441 | |
1442 | - $html .= '</select>'; |
|
1443 | - $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
1442 | + $html .= '</select>'; |
|
1443 | + $html .= '<label for="wpinv_settings[' . $sanitize_id . ']"> ' . wp_kses_post( $args['desc'] ) . '</label>'; |
|
1444 | 1444 | |
1445 | - echo $html; |
|
1445 | + echo $html; |
|
1446 | 1446 | } |
1447 | 1447 | |
1448 | 1448 | function wpinv_tax_rates_callback($args) { |
1449 | - global $wpinv_options; |
|
1450 | - $rates = wpinv_get_tax_rates(); |
|
1451 | - ob_start(); ?> |
|
1449 | + global $wpinv_options; |
|
1450 | + $rates = wpinv_get_tax_rates(); |
|
1451 | + ob_start(); ?> |
|
1452 | 1452 | </td><tr> |
1453 | 1453 | <td colspan="2" class="wpinv_tax_tdbox"> |
1454 | 1454 | <p><?php echo $args['desc']; ?></p> |
@@ -1472,40 +1472,40 @@ discard block |
||
1472 | 1472 | <tr> |
1473 | 1473 | <td class="wpinv_tax_country"> |
1474 | 1474 | <?php |
1475 | - echo wpinv_html_select( array( |
|
1476 | - 'options' => wpinv_get_country_list( true ), |
|
1477 | - 'name' => 'tax_rates[' . $sanitized_key . '][country]', |
|
1475 | + echo wpinv_html_select( array( |
|
1476 | + 'options' => wpinv_get_country_list( true ), |
|
1477 | + 'name' => 'tax_rates[' . $sanitized_key . '][country]', |
|
1478 | 1478 | 'id' => 'tax_rates[' . $sanitized_key . '][country]', |
1479 | - 'selected' => $rate['country'], |
|
1480 | - 'show_option_all' => false, |
|
1481 | - 'show_option_none' => false, |
|
1482 | - 'class' => 'wpinv-tax-country wpi_select2', |
|
1483 | - 'placeholder' => __( 'Choose a country', 'invoicing' ) |
|
1484 | - ) ); |
|
1485 | - ?> |
|
1479 | + 'selected' => $rate['country'], |
|
1480 | + 'show_option_all' => false, |
|
1481 | + 'show_option_none' => false, |
|
1482 | + 'class' => 'wpinv-tax-country wpi_select2', |
|
1483 | + 'placeholder' => __( 'Choose a country', 'invoicing' ) |
|
1484 | + ) ); |
|
1485 | + ?> |
|
1486 | 1486 | </td> |
1487 | 1487 | <td class="wpinv_tax_state"> |
1488 | 1488 | <?php |
1489 | - $states = wpinv_get_country_states( $rate['country'] ); |
|
1490 | - if( !empty( $states ) ) { |
|
1491 | - echo wpinv_html_select( array( |
|
1492 | - 'options' => array_merge( array( '' => '' ), $states ), |
|
1493 | - 'name' => 'tax_rates[' . $sanitized_key . '][state]', |
|
1489 | + $states = wpinv_get_country_states( $rate['country'] ); |
|
1490 | + if( !empty( $states ) ) { |
|
1491 | + echo wpinv_html_select( array( |
|
1492 | + 'options' => array_merge( array( '' => '' ), $states ), |
|
1493 | + 'name' => 'tax_rates[' . $sanitized_key . '][state]', |
|
1494 | 1494 | 'id' => 'tax_rates[' . $sanitized_key . '][state]', |
1495 | - 'selected' => $rate['state'], |
|
1496 | - 'show_option_all' => false, |
|
1497 | - 'show_option_none' => false, |
|
1495 | + 'selected' => $rate['state'], |
|
1496 | + 'show_option_all' => false, |
|
1497 | + 'show_option_none' => false, |
|
1498 | 1498 | 'class' => 'wpi_select2', |
1499 | - 'placeholder' => __( 'Choose a state', 'invoicing' ) |
|
1500 | - ) ); |
|
1501 | - } else { |
|
1502 | - echo wpinv_html_text( array( |
|
1503 | - 'name' => 'tax_rates[' . $sanitized_key . '][state]', $rate['state'], |
|
1504 | - 'value' => ! empty( $rate['state'] ) ? $rate['state'] : '', |
|
1499 | + 'placeholder' => __( 'Choose a state', 'invoicing' ) |
|
1500 | + ) ); |
|
1501 | + } else { |
|
1502 | + echo wpinv_html_text( array( |
|
1503 | + 'name' => 'tax_rates[' . $sanitized_key . '][state]', $rate['state'], |
|
1504 | + 'value' => ! empty( $rate['state'] ) ? $rate['state'] : '', |
|
1505 | 1505 | 'id' => 'tax_rates[' . $sanitized_key . '][state]', |
1506 | - ) ); |
|
1507 | - } |
|
1508 | - ?> |
|
1506 | + ) ); |
|
1507 | + } |
|
1508 | + ?> |
|
1509 | 1509 | </td> |
1510 | 1510 | <td class="wpinv_tax_global"> |
1511 | 1511 | <input type="checkbox" name="tax_rates[<?php echo $sanitized_key; ?>][global]" id="tax_rates[<?php echo $sanitized_key; ?>][global]" value="1"<?php checked( true, ! empty( $rate['global'] ) ); ?>/> |
@@ -1520,19 +1520,19 @@ discard block |
||
1520 | 1520 | <tr> |
1521 | 1521 | <td class="wpinv_tax_country"> |
1522 | 1522 | <?php |
1523 | - echo wpinv_html_select( array( |
|
1524 | - 'options' => wpinv_get_country_list( true ), |
|
1525 | - 'name' => 'tax_rates[0][country]', |
|
1526 | - 'show_option_all' => false, |
|
1527 | - 'show_option_none' => false, |
|
1528 | - 'class' => 'wpinv-tax-country wpi_select2', |
|
1529 | - 'placeholder' => __( 'Choose a country', 'invoicing' ) |
|
1530 | - ) ); ?> |
|
1523 | + echo wpinv_html_select( array( |
|
1524 | + 'options' => wpinv_get_country_list( true ), |
|
1525 | + 'name' => 'tax_rates[0][country]', |
|
1526 | + 'show_option_all' => false, |
|
1527 | + 'show_option_none' => false, |
|
1528 | + 'class' => 'wpinv-tax-country wpi_select2', |
|
1529 | + 'placeholder' => __( 'Choose a country', 'invoicing' ) |
|
1530 | + ) ); ?> |
|
1531 | 1531 | </td> |
1532 | 1532 | <td class="wpinv_tax_state"> |
1533 | 1533 | <?php echo wpinv_html_text( array( |
1534 | - 'name' => 'tax_rates[0][state]' |
|
1535 | - ) ); ?> |
|
1534 | + 'name' => 'tax_rates[0][state]' |
|
1535 | + ) ); ?> |
|
1536 | 1536 | </td> |
1537 | 1537 | <td class="wpinv_tax_global"> |
1538 | 1538 | <input type="checkbox" name="tax_rates[0][global]" id="tax_rates[0][global]" value="1"/> |
@@ -1547,7 +1547,7 @@ discard block |
||
1547 | 1547 | <tfoot><tr><td colspan="5"></td><td class="wpinv_tax_action"><span class="button-secondary" id="wpinv_add_tax_rate"><?php _e( 'Add Tax Rate', 'invoicing' ); ?></span></td></tr></tfoot> |
1548 | 1548 | </table> |
1549 | 1549 | <?php |
1550 | - echo ob_get_clean(); |
|
1550 | + echo ob_get_clean(); |
|
1551 | 1551 | } |
1552 | 1552 | |
1553 | 1553 | function wpinv_tools_callback($args) { |
@@ -1575,15 +1575,15 @@ discard block |
||
1575 | 1575 | } |
1576 | 1576 | |
1577 | 1577 | function wpinv_descriptive_text_callback( $args ) { |
1578 | - echo wp_kses_post( $args['desc'] ); |
|
1578 | + echo wp_kses_post( $args['desc'] ); |
|
1579 | 1579 | } |
1580 | 1580 | |
1581 | 1581 | function wpinv_hook_callback( $args ) { |
1582 | - do_action( 'wpinv_' . $args['id'], $args ); |
|
1582 | + do_action( 'wpinv_' . $args['id'], $args ); |
|
1583 | 1583 | } |
1584 | 1584 | |
1585 | 1585 | function wpinv_set_settings_cap() { |
1586 | - return wpinv_get_capability(); |
|
1586 | + return wpinv_get_capability(); |
|
1587 | 1587 | } |
1588 | 1588 | add_filter( 'option_page_capability_wpinv_settings', 'wpinv_set_settings_cap' ); |
1589 | 1589 |
@@ -101,9 +101,9 @@ discard block |
||
101 | 101 | case 'status' : |
102 | 102 | $value = $wpi_invoice->get_status( true ) . ( $wpi_invoice->is_recurring() && $wpi_invoice->is_parent() ? ' <span class="wpi-suffix">' . __( '(r)', 'invoicing' ) . '</span>' : '' ); |
103 | 103 | $is_viewed = wpinv_is_invoice_viewed( $wpi_invoice->ID ); |
104 | - $gateway_title = wpinv_get_gateway_admin_label( $wpi_invoice->get_gateway() ); |
|
105 | - $offline_gateways = apply_filters('wpinv_offline_payments', array('bank_transfer', 'cheque', 'cod')); |
|
106 | - $is_offline_payment = in_array($wpi_invoice->get_gateway(), $offline_gateways) ? true : false; |
|
104 | + $gateway_title = wpinv_get_gateway_admin_label( $wpi_invoice->get_gateway() ); |
|
105 | + $offline_gateways = apply_filters('wpinv_offline_payments', array('bank_transfer', 'cheque', 'cod')); |
|
106 | + $is_offline_payment = in_array($wpi_invoice->get_gateway(), $offline_gateways) ? true : false; |
|
107 | 107 | |
108 | 108 | if ( 1 == $is_viewed ) { |
109 | 109 | $value .= ' <i class="fa fa-eye" title="'.__( 'Viewed by Customer', 'invoicing' ).'"></i>'; |
@@ -174,69 +174,69 @@ discard block |
||
174 | 174 | } |
175 | 175 | |
176 | 176 | function wpinv_admin_messages() { |
177 | - global $wpinv_options, $pagenow, $post; |
|
177 | + global $wpinv_options, $pagenow, $post; |
|
178 | 178 | |
179 | - if ( isset( $_GET['wpinv-message'] ) && 'discount_added' == $_GET['wpinv-message'] && wpinv_current_user_can_manage_invoicing() ) { |
|
180 | - add_settings_error( 'wpinv-notices', 'wpinv-discount-added', __( 'Discount code added.', 'invoicing' ), 'updated' ); |
|
181 | - } |
|
179 | + if ( isset( $_GET['wpinv-message'] ) && 'discount_added' == $_GET['wpinv-message'] && wpinv_current_user_can_manage_invoicing() ) { |
|
180 | + add_settings_error( 'wpinv-notices', 'wpinv-discount-added', __( 'Discount code added.', 'invoicing' ), 'updated' ); |
|
181 | + } |
|
182 | 182 | |
183 | - if ( isset( $_GET['wpinv-message'] ) && 'discount_add_failed' == $_GET['wpinv-message'] && wpinv_current_user_can_manage_invoicing() ) { |
|
184 | - add_settings_error( 'wpinv-notices', 'wpinv-discount-add-fail', __( 'There was a problem adding your discount code, please try again.', 'invoicing' ), 'error' ); |
|
185 | - } |
|
183 | + if ( isset( $_GET['wpinv-message'] ) && 'discount_add_failed' == $_GET['wpinv-message'] && wpinv_current_user_can_manage_invoicing() ) { |
|
184 | + add_settings_error( 'wpinv-notices', 'wpinv-discount-add-fail', __( 'There was a problem adding your discount code, please try again.', 'invoicing' ), 'error' ); |
|
185 | + } |
|
186 | 186 | |
187 | - if ( isset( $_GET['wpinv-message'] ) && 'discount_exists' == $_GET['wpinv-message'] && wpinv_current_user_can_manage_invoicing() ) { |
|
188 | - add_settings_error( 'wpinv-notices', 'wpinv-discount-exists', __( 'A discount with that code already exists, please use a different code.', 'invoicing' ), 'error' ); |
|
189 | - } |
|
187 | + if ( isset( $_GET['wpinv-message'] ) && 'discount_exists' == $_GET['wpinv-message'] && wpinv_current_user_can_manage_invoicing() ) { |
|
188 | + add_settings_error( 'wpinv-notices', 'wpinv-discount-exists', __( 'A discount with that code already exists, please use a different code.', 'invoicing' ), 'error' ); |
|
189 | + } |
|
190 | 190 | |
191 | - if ( isset( $_GET['wpinv-message'] ) && 'discount_updated' == $_GET['wpinv-message'] && wpinv_current_user_can_manage_invoicing() ) { |
|
192 | - add_settings_error( 'wpinv-notices', 'wpinv-discount-updated', __( 'Discount code updated.', 'invoicing' ), 'updated' ); |
|
193 | - } |
|
191 | + if ( isset( $_GET['wpinv-message'] ) && 'discount_updated' == $_GET['wpinv-message'] && wpinv_current_user_can_manage_invoicing() ) { |
|
192 | + add_settings_error( 'wpinv-notices', 'wpinv-discount-updated', __( 'Discount code updated.', 'invoicing' ), 'updated' ); |
|
193 | + } |
|
194 | 194 | |
195 | - if ( isset( $_GET['wpinv-message'] ) && 'discount_update_failed' == $_GET['wpinv-message'] && wpinv_current_user_can_manage_invoicing() ) { |
|
196 | - add_settings_error( 'wpinv-notices', 'wpinv-discount-updated-fail', __( 'There was a problem updating your discount code, please try again.', 'invoicing' ), 'error' ); |
|
197 | - } |
|
195 | + if ( isset( $_GET['wpinv-message'] ) && 'discount_update_failed' == $_GET['wpinv-message'] && wpinv_current_user_can_manage_invoicing() ) { |
|
196 | + add_settings_error( 'wpinv-notices', 'wpinv-discount-updated-fail', __( 'There was a problem updating your discount code, please try again.', 'invoicing' ), 'error' ); |
|
197 | + } |
|
198 | 198 | |
199 | - if ( isset( $_GET['wpinv-message'] ) && 'invoice_deleted' == $_GET['wpinv-message'] && wpinv_current_user_can_manage_invoicing() ) { |
|
200 | - add_settings_error( 'wpinv-notices', 'wpinv-deleted', __( 'The invoice has been deleted.', 'invoicing' ), 'updated' ); |
|
201 | - } |
|
199 | + if ( isset( $_GET['wpinv-message'] ) && 'invoice_deleted' == $_GET['wpinv-message'] && wpinv_current_user_can_manage_invoicing() ) { |
|
200 | + add_settings_error( 'wpinv-notices', 'wpinv-deleted', __( 'The invoice has been deleted.', 'invoicing' ), 'updated' ); |
|
201 | + } |
|
202 | 202 | |
203 | - if ( isset( $_GET['wpinv-message'] ) && 'email_disabled' == $_GET['wpinv-message'] && wpinv_current_user_can_manage_invoicing() ) { |
|
204 | - add_settings_error( 'wpinv-notices', 'wpinv-sent-fail', __( 'Email notification is disabled. Please check settings.', 'invoicing' ), 'error' ); |
|
205 | - } |
|
203 | + if ( isset( $_GET['wpinv-message'] ) && 'email_disabled' == $_GET['wpinv-message'] && wpinv_current_user_can_manage_invoicing() ) { |
|
204 | + add_settings_error( 'wpinv-notices', 'wpinv-sent-fail', __( 'Email notification is disabled. Please check settings.', 'invoicing' ), 'error' ); |
|
205 | + } |
|
206 | 206 | |
207 | - if ( isset( $_GET['wpinv-message'] ) && 'email_sent' == $_GET['wpinv-message'] && wpinv_current_user_can_manage_invoicing() ) { |
|
208 | - add_settings_error( 'wpinv-notices', 'wpinv-sent', __( 'The email has been sent to customer.', 'invoicing' ), 'updated' ); |
|
207 | + if ( isset( $_GET['wpinv-message'] ) && 'email_sent' == $_GET['wpinv-message'] && wpinv_current_user_can_manage_invoicing() ) { |
|
208 | + add_settings_error( 'wpinv-notices', 'wpinv-sent', __( 'The email has been sent to customer.', 'invoicing' ), 'updated' ); |
|
209 | 209 | } |
210 | 210 | |
211 | 211 | if ( isset( $_GET['wpinv-message'] ) && 'email_fail' == $_GET['wpinv-message'] && wpinv_current_user_can_manage_invoicing() ) { |
212 | - add_settings_error( 'wpinv-notices', 'wpinv-sent-fail', __( 'Fail to send email to the customer.', 'invoicing' ), 'error' ); |
|
212 | + add_settings_error( 'wpinv-notices', 'wpinv-sent-fail', __( 'Fail to send email to the customer.', 'invoicing' ), 'error' ); |
|
213 | 213 | } |
214 | 214 | |
215 | 215 | if ( isset( $_GET['wpinv-message'] ) && 'invoice-note-deleted' == $_GET['wpinv-message'] && wpinv_current_user_can_manage_invoicing() ) { |
216 | 216 | add_settings_error( 'wpinv-notices', 'wpinv-note-deleted', __( 'The invoice note has been deleted.', 'invoicing' ), 'updated' ); |
217 | 217 | } |
218 | 218 | |
219 | - if ( isset( $_GET['wpinv-message'] ) && 'settings-imported' == $_GET['wpinv-message'] && wpinv_current_user_can_manage_invoicing() ) { |
|
220 | - add_settings_error( 'wpinv-notices', 'wpinv-settings-imported', __( 'The settings have been imported.', 'invoicing' ), 'updated' ); |
|
221 | - } |
|
219 | + if ( isset( $_GET['wpinv-message'] ) && 'settings-imported' == $_GET['wpinv-message'] && wpinv_current_user_can_manage_invoicing() ) { |
|
220 | + add_settings_error( 'wpinv-notices', 'wpinv-settings-imported', __( 'The settings have been imported.', 'invoicing' ), 'updated' ); |
|
221 | + } |
|
222 | 222 | |
223 | - if ( isset( $_GET['wpinv-message'] ) && 'note-added' == $_GET['wpinv-message'] && wpinv_current_user_can_manage_invoicing() ) { |
|
224 | - add_settings_error( 'wpinv-notices', 'wpinv-note-added', __( 'The invoice note has been added successfully.', 'invoicing' ), 'updated' ); |
|
225 | - } |
|
223 | + if ( isset( $_GET['wpinv-message'] ) && 'note-added' == $_GET['wpinv-message'] && wpinv_current_user_can_manage_invoicing() ) { |
|
224 | + add_settings_error( 'wpinv-notices', 'wpinv-note-added', __( 'The invoice note has been added successfully.', 'invoicing' ), 'updated' ); |
|
225 | + } |
|
226 | 226 | |
227 | - if ( isset( $_GET['wpinv-message'] ) && 'invoice-updated' == $_GET['wpinv-message'] && wpinv_current_user_can_manage_invoicing() ) { |
|
228 | - add_settings_error( 'wpinv-notices', 'wpinv-updated', __( 'The invoice has been successfully updated.', 'invoicing' ), 'updated' ); |
|
229 | - } |
|
227 | + if ( isset( $_GET['wpinv-message'] ) && 'invoice-updated' == $_GET['wpinv-message'] && wpinv_current_user_can_manage_invoicing() ) { |
|
228 | + add_settings_error( 'wpinv-notices', 'wpinv-updated', __( 'The invoice has been successfully updated.', 'invoicing' ), 'updated' ); |
|
229 | + } |
|
230 | 230 | |
231 | - if ( $pagenow == 'post.php' && !empty( $post->post_type ) && $post->post_type == 'wpi_item' && !wpinv_item_is_editable( $post ) ) { |
|
232 | - $message = apply_filters( 'wpinv_item_non_editable_message', __( 'This item in not editable.', 'invoicing' ), $post->ID ); |
|
231 | + if ( $pagenow == 'post.php' && !empty( $post->post_type ) && $post->post_type == 'wpi_item' && !wpinv_item_is_editable( $post ) ) { |
|
232 | + $message = apply_filters( 'wpinv_item_non_editable_message', __( 'This item in not editable.', 'invoicing' ), $post->ID ); |
|
233 | 233 | |
234 | - if ( !empty( $message ) ) { |
|
235 | - add_settings_error( 'wpinv-notices', 'wpinv-edit-n', $message, 'updated' ); |
|
236 | - } |
|
237 | - } |
|
234 | + if ( !empty( $message ) ) { |
|
235 | + add_settings_error( 'wpinv-notices', 'wpinv-edit-n', $message, 'updated' ); |
|
236 | + } |
|
237 | + } |
|
238 | 238 | |
239 | - settings_errors( 'wpinv-notices' ); |
|
239 | + settings_errors( 'wpinv-notices' ); |
|
240 | 240 | } |
241 | 241 | add_action( 'admin_notices', 'wpinv_admin_messages' ); |
242 | 242 | |
@@ -326,7 +326,7 @@ discard block |
||
326 | 326 | break; |
327 | 327 | case 'id' : |
328 | 328 | echo $post->ID; |
329 | - echo '<div class="hidden" id="wpinv_inline-' . $post->ID . '"> |
|
329 | + echo '<div class="hidden" id="wpinv_inline-' . $post->ID . '"> |
|
330 | 330 | <div class="price">' . wpinv_get_item_price( $post->ID ) . '</div>'; |
331 | 331 | if ( $wpinv_euvat->allow_vat_rules() ) { |
332 | 332 | echo '<div class="vat_rule">' . $wpinv_euvat->get_item_rule( $post->ID ) . '</div>'; |
@@ -159,11 +159,11 @@ |
||
159 | 159 | */ |
160 | 160 | function wpinv_get_capability( $capalibilty = 'manage_invoicing' ) { |
161 | 161 | |
162 | - if ( current_user_can( 'manage_options' ) ) { |
|
163 | - return 'manage_options'; |
|
164 | - }; |
|
162 | + if ( current_user_can( 'manage_options' ) ) { |
|
163 | + return 'manage_options'; |
|
164 | + }; |
|
165 | 165 | |
166 | - return $capalibilty; |
|
166 | + return $capalibilty; |
|
167 | 167 | } |
168 | 168 | |
169 | 169 | /** |
@@ -20,91 +20,91 @@ discard block |
||
20 | 20 | class WPInv_REST_Items_Controller extends WP_REST_Posts_Controller { |
21 | 21 | |
22 | 22 | /** |
23 | - * Post type. |
|
24 | - * |
|
25 | - * @var string |
|
26 | - */ |
|
27 | - protected $post_type = 'wpi_item'; |
|
23 | + * Post type. |
|
24 | + * |
|
25 | + * @var string |
|
26 | + */ |
|
27 | + protected $post_type = 'wpi_item'; |
|
28 | 28 | |
29 | - /** |
|
30 | - * Cached results of get_item_schema. |
|
31 | - * |
|
32 | - * @since 1.0.13 |
|
33 | - * @var array |
|
34 | - */ |
|
35 | - protected $schema; |
|
29 | + /** |
|
30 | + * Cached results of get_item_schema. |
|
31 | + * |
|
32 | + * @since 1.0.13 |
|
33 | + * @var array |
|
34 | + */ |
|
35 | + protected $schema; |
|
36 | 36 | |
37 | 37 | /** |
38 | - * Constructor. |
|
39 | - * |
|
40 | - * @since 1.0.13 |
|
41 | - * |
|
42 | - * @param string $namespace Api Namespace |
|
43 | - */ |
|
44 | - public function __construct( $namespace ) { |
|
38 | + * Constructor. |
|
39 | + * |
|
40 | + * @since 1.0.13 |
|
41 | + * |
|
42 | + * @param string $namespace Api Namespace |
|
43 | + */ |
|
44 | + public function __construct( $namespace ) { |
|
45 | 45 | |
46 | 46 | // Set api namespace... |
47 | - $this->namespace = $namespace; |
|
47 | + $this->namespace = $namespace; |
|
48 | 48 | |
49 | 49 | // ... and the rest base |
50 | 50 | $this->rest_base = 'items'; |
51 | 51 | |
52 | 52 | } |
53 | 53 | |
54 | - /** |
|
55 | - * Registers the routes for the objects of the controller. |
|
56 | - * |
|
57 | - * @since 1.0.13 |
|
58 | - * |
|
59 | - * @see register_rest_route() |
|
60 | - */ |
|
61 | - public function register_routes() { |
|
62 | - |
|
63 | - parent::register_routes(); |
|
64 | - |
|
65 | - register_rest_route( |
|
66 | - $this->namespace, |
|
67 | - '/' . $this->rest_base . '/item-types', |
|
68 | - array( |
|
69 | - array( |
|
70 | - 'methods' => WP_REST_Server::READABLE, |
|
71 | - 'callback' => array( $this, 'get_item_types' ), |
|
72 | - ), |
|
73 | - ) |
|
74 | - ); |
|
75 | - |
|
76 | - } |
|
54 | + /** |
|
55 | + * Registers the routes for the objects of the controller. |
|
56 | + * |
|
57 | + * @since 1.0.13 |
|
58 | + * |
|
59 | + * @see register_rest_route() |
|
60 | + */ |
|
61 | + public function register_routes() { |
|
62 | + |
|
63 | + parent::register_routes(); |
|
64 | + |
|
65 | + register_rest_route( |
|
66 | + $this->namespace, |
|
67 | + '/' . $this->rest_base . '/item-types', |
|
68 | + array( |
|
69 | + array( |
|
70 | + 'methods' => WP_REST_Server::READABLE, |
|
71 | + 'callback' => array( $this, 'get_item_types' ), |
|
72 | + ), |
|
73 | + ) |
|
74 | + ); |
|
75 | + |
|
76 | + } |
|
77 | 77 | |
78 | 78 | /** |
79 | - * Checks if a given request has access to read items. |
|
79 | + * Checks if a given request has access to read items. |
|
80 | 80 | * |
81 | - * |
|
82 | - * @since 1.0.13 |
|
83 | - * |
|
84 | - * @param WP_REST_Request $request Full details about the request. |
|
85 | - * @return true|WP_Error True if the request has read access, WP_Error object otherwise. |
|
86 | - */ |
|
87 | - public function get_items_permissions_check( $request ) { |
|
81 | + * |
|
82 | + * @since 1.0.13 |
|
83 | + * |
|
84 | + * @param WP_REST_Request $request Full details about the request. |
|
85 | + * @return true|WP_Error True if the request has read access, WP_Error object otherwise. |
|
86 | + */ |
|
87 | + public function get_items_permissions_check( $request ) { |
|
88 | 88 | |
89 | - if ( current_user_can( 'manage_options' ) || current_user_can( 'manage_invoicing' ) ) { |
|
90 | - return true; |
|
91 | - } |
|
89 | + if ( current_user_can( 'manage_options' ) || current_user_can( 'manage_invoicing' ) ) { |
|
90 | + return true; |
|
91 | + } |
|
92 | 92 | |
93 | - return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to view invoice items.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
93 | + return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to view invoice items.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
94 | 94 | |
95 | 95 | } |
96 | 96 | |
97 | 97 | /** |
98 | - * Retrieves a collection of invoice items. |
|
99 | - * |
|
100 | - * @since 1.0.13 |
|
101 | - * |
|
102 | - * @param WP_REST_Request $request Full details about the request. |
|
103 | - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
104 | - */ |
|
105 | - public function get_items( $request ) { |
|
98 | + * Retrieves a collection of invoice items. |
|
99 | + * |
|
100 | + * @since 1.0.13 |
|
101 | + * |
|
102 | + * @param WP_REST_Request $request Full details about the request. |
|
103 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
104 | + */ |
|
105 | + public function get_items( $request ) { |
|
106 | 106 | |
107 | - // Retrieve the list of registered item query parameters. |
|
107 | + // Retrieve the list of registered item query parameters. |
|
108 | 108 | $registered = $this->get_collection_params(); |
109 | 109 | |
110 | 110 | $args = array(); |
@@ -117,54 +117,54 @@ discard block |
||
117 | 117 | |
118 | 118 | } |
119 | 119 | |
120 | - /** |
|
121 | - * Filters the wpinv_get_items arguments for items rest requests. |
|
122 | - * |
|
123 | - * |
|
124 | - * @since 1.0.13 |
|
125 | - * |
|
126 | - * |
|
127 | - * @param array $args Key value array of query var to query value. |
|
128 | - * @param WP_REST_Request $request The request used. |
|
129 | - */ |
|
120 | + /** |
|
121 | + * Filters the wpinv_get_items arguments for items rest requests. |
|
122 | + * |
|
123 | + * |
|
124 | + * @since 1.0.13 |
|
125 | + * |
|
126 | + * |
|
127 | + * @param array $args Key value array of query var to query value. |
|
128 | + * @param WP_REST_Request $request The request used. |
|
129 | + */ |
|
130 | 130 | $args = apply_filters( "wpinv_rest_get_items_arguments", $args, $request, $this ); |
131 | 131 | |
132 | - // Special args |
|
133 | - $args[ 'return' ] = 'objects'; |
|
134 | - $args[ 'paginate' ] = true; |
|
132 | + // Special args |
|
133 | + $args[ 'return' ] = 'objects'; |
|
134 | + $args[ 'paginate' ] = true; |
|
135 | 135 | |
136 | 136 | // Run the query. |
137 | - $query = wpinv_get_all_items( $args ); |
|
137 | + $query = wpinv_get_all_items( $args ); |
|
138 | 138 | |
139 | - // Prepare the retrieved items |
|
140 | - $items = array(); |
|
141 | - foreach( $query->items as $item ) { |
|
142 | - |
|
143 | - if ( ! $this->check_read_permission( $item ) ) { |
|
144 | - continue; |
|
145 | - } |
|
146 | - |
|
147 | - $data = $this->prepare_item_for_response( $item, $request ); |
|
148 | - $items[] = $this->prepare_response_for_collection( $data ); |
|
149 | - |
|
150 | - } |
|
151 | - |
|
152 | - // Prepare the response. |
|
153 | - $response = rest_ensure_response( $items ); |
|
154 | - $response->header( 'X-WP-Total', (int) $query->total ); |
|
155 | - $response->header( 'X-WP-TotalPages', (int) $query->max_num_pages ); |
|
156 | - |
|
157 | - /** |
|
158 | - * Filters the responses for item requests. |
|
159 | - * |
|
160 | - * |
|
161 | - * @since 1.0.13 |
|
162 | - * |
|
163 | - * |
|
164 | - * @param arrWP_REST_Response $response Response object. |
|
165 | - * @param WP_REST_Request $request The request used. |
|
139 | + // Prepare the retrieved items |
|
140 | + $items = array(); |
|
141 | + foreach( $query->items as $item ) { |
|
142 | + |
|
143 | + if ( ! $this->check_read_permission( $item ) ) { |
|
144 | + continue; |
|
145 | + } |
|
146 | + |
|
147 | + $data = $this->prepare_item_for_response( $item, $request ); |
|
148 | + $items[] = $this->prepare_response_for_collection( $data ); |
|
149 | + |
|
150 | + } |
|
151 | + |
|
152 | + // Prepare the response. |
|
153 | + $response = rest_ensure_response( $items ); |
|
154 | + $response->header( 'X-WP-Total', (int) $query->total ); |
|
155 | + $response->header( 'X-WP-TotalPages', (int) $query->max_num_pages ); |
|
156 | + |
|
157 | + /** |
|
158 | + * Filters the responses for item requests. |
|
159 | + * |
|
160 | + * |
|
161 | + * @since 1.0.13 |
|
162 | + * |
|
163 | + * |
|
164 | + * @param arrWP_REST_Response $response Response object. |
|
165 | + * @param WP_REST_Request $request The request used. |
|
166 | 166 | * @param array $args Array of args used to retrieve the items |
167 | - */ |
|
167 | + */ |
|
168 | 168 | $response = apply_filters( "wpinv_rest_items_response", $response, $request, $args ); |
169 | 169 | |
170 | 170 | return rest_ensure_response( $response ); |
@@ -172,25 +172,25 @@ discard block |
||
172 | 172 | } |
173 | 173 | |
174 | 174 | /** |
175 | - * Get the post, if the ID is valid. |
|
176 | - * |
|
177 | - * @since 1.0.13 |
|
178 | - * |
|
179 | - * @param int $item_id Supplied ID. |
|
180 | - * @return WPInv_Item|WP_Error Item object if ID is valid, WP_Error otherwise. |
|
181 | - */ |
|
182 | - protected function get_post( $item_id ) { |
|
175 | + * Get the post, if the ID is valid. |
|
176 | + * |
|
177 | + * @since 1.0.13 |
|
178 | + * |
|
179 | + * @param int $item_id Supplied ID. |
|
180 | + * @return WPInv_Item|WP_Error Item object if ID is valid, WP_Error otherwise. |
|
181 | + */ |
|
182 | + protected function get_post( $item_id ) { |
|
183 | 183 | |
184 | - $error = new WP_Error( 'rest_item_invalid_id', __( 'Invalid item ID.', 'invoicing' ), array( 'status' => 404 ) ); |
|
184 | + $error = new WP_Error( 'rest_item_invalid_id', __( 'Invalid item ID.', 'invoicing' ), array( 'status' => 404 ) ); |
|
185 | 185 | |
186 | 186 | // Ids start from 1 |
187 | 187 | if ( (int) $item_id <= 0 ) { |
188 | - return $error; |
|
189 | - } |
|
188 | + return $error; |
|
189 | + } |
|
190 | 190 | |
191 | - $item = wpinv_get_item_by( 'id', (int) $item_id ); |
|
192 | - if ( empty( $item ) ) { |
|
193 | - return $error; |
|
191 | + $item = wpinv_get_item_by( 'id', (int) $item_id ); |
|
192 | + if ( empty( $item ) ) { |
|
193 | + return $error; |
|
194 | 194 | } |
195 | 195 | |
196 | 196 | return $item; |
@@ -198,27 +198,27 @@ discard block |
||
198 | 198 | } |
199 | 199 | |
200 | 200 | /** |
201 | - * Checks if a given request has access to read an invoice item. |
|
202 | - * |
|
203 | - * @since 1.0.13 |
|
204 | - * |
|
205 | - * @param WP_REST_Request $request Full details about the request. |
|
206 | - * @return bool|WP_Error True if the request has read access for the invoice item, WP_Error object otherwise. |
|
207 | - */ |
|
208 | - public function get_item_permissions_check( $request ) { |
|
201 | + * Checks if a given request has access to read an invoice item. |
|
202 | + * |
|
203 | + * @since 1.0.13 |
|
204 | + * |
|
205 | + * @param WP_REST_Request $request Full details about the request. |
|
206 | + * @return bool|WP_Error True if the request has read access for the invoice item, WP_Error object otherwise. |
|
207 | + */ |
|
208 | + public function get_item_permissions_check( $request ) { |
|
209 | 209 | |
210 | 210 | // Retrieve the item object. |
211 | 211 | $item = $this->get_post( $request['id'] ); |
212 | 212 | |
213 | 213 | // Ensure it is valid. |
214 | - if ( is_wp_error( $item ) ) { |
|
215 | - return $item; |
|
216 | - } |
|
214 | + if ( is_wp_error( $item ) ) { |
|
215 | + return $item; |
|
216 | + } |
|
217 | 217 | |
218 | - $post_type = get_post_type_object( $this->post_type ); |
|
218 | + $post_type = get_post_type_object( $this->post_type ); |
|
219 | 219 | |
220 | - if ( ! current_user_can( $post_type->cap->read_post, $item->ID ) ) { |
|
221 | - return new WP_Error( |
|
220 | + if ( ! current_user_can( $post_type->cap->read_post, $item->ID ) ) { |
|
221 | + return new WP_Error( |
|
222 | 222 | 'rest_cannot_edit', |
223 | 223 | __( 'Sorry, you are not allowed to view this item.', 'invoicing' ), |
224 | 224 | array( |
@@ -227,61 +227,61 @@ discard block |
||
227 | 227 | ); |
228 | 228 | } |
229 | 229 | |
230 | - return $this->check_read_permission( $item ); |
|
230 | + return $this->check_read_permission( $item ); |
|
231 | 231 | } |
232 | 232 | |
233 | 233 | /** |
234 | - * Checks if an item can be read. |
|
235 | - * |
|
236 | - * An item can be read by site admins. |
|
237 | - * |
|
238 | - * |
|
239 | - * @since 1.0.13 |
|
240 | - * |
|
241 | - * @param WPInv_Item $item WPInv_Item object. |
|
242 | - * @return bool Whether the post can be read. |
|
243 | - */ |
|
244 | - public function check_read_permission( $item ) { |
|
245 | - |
|
246 | - // An item can be read by an admin... |
|
247 | - if ( current_user_can( 'manage_options' ) || current_user_can( 'manage_invoicing' ) ) { |
|
248 | - return true; |
|
249 | - } |
|
250 | - |
|
251 | - return false; |
|
234 | + * Checks if an item can be read. |
|
235 | + * |
|
236 | + * An item can be read by site admins. |
|
237 | + * |
|
238 | + * |
|
239 | + * @since 1.0.13 |
|
240 | + * |
|
241 | + * @param WPInv_Item $item WPInv_Item object. |
|
242 | + * @return bool Whether the post can be read. |
|
243 | + */ |
|
244 | + public function check_read_permission( $item ) { |
|
245 | + |
|
246 | + // An item can be read by an admin... |
|
247 | + if ( current_user_can( 'manage_options' ) || current_user_can( 'manage_invoicing' ) ) { |
|
248 | + return true; |
|
249 | + } |
|
250 | + |
|
251 | + return false; |
|
252 | 252 | } |
253 | 253 | |
254 | 254 | /** |
255 | - * Retrieves a single invoice item. |
|
256 | - * |
|
257 | - * @since 1.0.13 |
|
258 | - * |
|
259 | - * @param WP_REST_Request $request Full details about the request. |
|
260 | - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
261 | - */ |
|
262 | - public function get_item( $request ) { |
|
255 | + * Retrieves a single invoice item. |
|
256 | + * |
|
257 | + * @since 1.0.13 |
|
258 | + * |
|
259 | + * @param WP_REST_Request $request Full details about the request. |
|
260 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
261 | + */ |
|
262 | + public function get_item( $request ) { |
|
263 | 263 | |
264 | 264 | // Fetch the item. |
265 | 265 | $item = $this->get_post( $request['id'] ); |
266 | 266 | |
267 | 267 | // Abort early if it does not exist |
268 | - if ( is_wp_error( $item ) ) { |
|
269 | - return $item; |
|
270 | - } |
|
271 | - |
|
272 | - // Prepare the response |
|
273 | - $response = $this->prepare_item_for_response( $item, $request ); |
|
274 | - |
|
275 | - /** |
|
276 | - * Filters the responses for single invoice item requests. |
|
277 | - * |
|
278 | - * |
|
279 | - * @since 1.0.13 |
|
280 | - * @var WP_HTTP_Response |
|
281 | - * |
|
282 | - * @param WP_HTTP_Response $response Response. |
|
283 | - * @param WP_REST_Request $request The request used. |
|
284 | - */ |
|
268 | + if ( is_wp_error( $item ) ) { |
|
269 | + return $item; |
|
270 | + } |
|
271 | + |
|
272 | + // Prepare the response |
|
273 | + $response = $this->prepare_item_for_response( $item, $request ); |
|
274 | + |
|
275 | + /** |
|
276 | + * Filters the responses for single invoice item requests. |
|
277 | + * |
|
278 | + * |
|
279 | + * @since 1.0.13 |
|
280 | + * @var WP_HTTP_Response |
|
281 | + * |
|
282 | + * @param WP_HTTP_Response $response Response. |
|
283 | + * @param WP_REST_Request $request The request used. |
|
284 | + */ |
|
285 | 285 | $response = apply_filters( "wpinv_rest_get_item_response", $response, $request ); |
286 | 286 | |
287 | 287 | return rest_ensure_response( $response ); |
@@ -289,26 +289,26 @@ discard block |
||
289 | 289 | } |
290 | 290 | |
291 | 291 | /** |
292 | - * Checks if a given request has access to create an invoice item. |
|
293 | - * |
|
294 | - * @since 1.0.13 |
|
295 | - * |
|
296 | - * @param WP_REST_Request $request Full details about the request. |
|
297 | - * @return true|WP_Error True if the request has access to create items, WP_Error object otherwise. |
|
298 | - */ |
|
299 | - public function create_item_permissions_check( $request ) { |
|
292 | + * Checks if a given request has access to create an invoice item. |
|
293 | + * |
|
294 | + * @since 1.0.13 |
|
295 | + * |
|
296 | + * @param WP_REST_Request $request Full details about the request. |
|
297 | + * @return true|WP_Error True if the request has access to create items, WP_Error object otherwise. |
|
298 | + */ |
|
299 | + public function create_item_permissions_check( $request ) { |
|
300 | 300 | |
301 | - if ( ! empty( $request['id'] ) ) { |
|
302 | - return new WP_Error( 'rest_item_exists', __( 'Cannot create existing item.', 'invoicing' ), array( 'status' => 400 ) ); |
|
303 | - } |
|
301 | + if ( ! empty( $request['id'] ) ) { |
|
302 | + return new WP_Error( 'rest_item_exists', __( 'Cannot create existing item.', 'invoicing' ), array( 'status' => 400 ) ); |
|
303 | + } |
|
304 | 304 | |
305 | - if ( current_user_can( 'manage_options' ) || current_user_can( 'manage_invoicing' ) ) { |
|
306 | - return true; |
|
307 | - } |
|
305 | + if ( current_user_can( 'manage_options' ) || current_user_can( 'manage_invoicing' ) ) { |
|
306 | + return true; |
|
307 | + } |
|
308 | 308 | |
309 | - $post_type = get_post_type_object( $this->post_type ); |
|
310 | - if ( ! current_user_can( $post_type->cap->create_posts ) ) { |
|
311 | - return new WP_Error( |
|
309 | + $post_type = get_post_type_object( $this->post_type ); |
|
310 | + if ( ! current_user_can( $post_type->cap->create_posts ) ) { |
|
311 | + return new WP_Error( |
|
312 | 312 | 'rest_cannot_create', |
313 | 313 | __( 'Sorry, you are not allowed to create invoice items as this user.', 'invoicing' ), |
314 | 314 | array( |
@@ -317,253 +317,253 @@ discard block |
||
317 | 317 | ); |
318 | 318 | } |
319 | 319 | |
320 | - return true; |
|
320 | + return true; |
|
321 | 321 | } |
322 | 322 | |
323 | 323 | /** |
324 | - * Creates a single invoice item. |
|
325 | - * |
|
326 | - * @since 1.0.13 |
|
327 | - * |
|
328 | - * @param WP_REST_Request $request Full details about the request. |
|
329 | - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
330 | - */ |
|
331 | - public function create_item( $request ) { |
|
332 | - |
|
333 | - if ( ! empty( $request['id'] ) ) { |
|
334 | - return new WP_Error( 'rest_item_exists', __( 'Cannot create existing invoice item.', 'invoicing' ), array( 'status' => 400 ) ); |
|
335 | - } |
|
324 | + * Creates a single invoice item. |
|
325 | + * |
|
326 | + * @since 1.0.13 |
|
327 | + * |
|
328 | + * @param WP_REST_Request $request Full details about the request. |
|
329 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
330 | + */ |
|
331 | + public function create_item( $request ) { |
|
332 | + |
|
333 | + if ( ! empty( $request['id'] ) ) { |
|
334 | + return new WP_Error( 'rest_item_exists', __( 'Cannot create existing invoice item.', 'invoicing' ), array( 'status' => 400 ) ); |
|
335 | + } |
|
336 | 336 | |
337 | - $request->set_param( 'context', 'edit' ); |
|
337 | + $request->set_param( 'context', 'edit' ); |
|
338 | 338 | |
339 | - // Prepare the updated data. |
|
340 | - $item_data = $this->prepare_item_for_database( $request ); |
|
339 | + // Prepare the updated data. |
|
340 | + $item_data = $this->prepare_item_for_database( $request ); |
|
341 | 341 | |
342 | - if ( is_wp_error( $item_data ) ) { |
|
343 | - return $item_data; |
|
344 | - } |
|
342 | + if ( is_wp_error( $item_data ) ) { |
|
343 | + return $item_data; |
|
344 | + } |
|
345 | 345 | |
346 | - // Try creating the item. |
|
346 | + // Try creating the item. |
|
347 | 347 | $item = wpinv_create_item( $item_data, true ); |
348 | 348 | |
349 | - if ( is_wp_error( $item ) ) { |
|
349 | + if ( is_wp_error( $item ) ) { |
|
350 | 350 | return $item; |
351 | - } |
|
352 | - |
|
353 | - // Prepare the response |
|
354 | - $response = $this->prepare_item_for_response( $item, $request ); |
|
355 | - |
|
356 | - /** |
|
357 | - * Fires after a single invoice item is created or updated via the REST API. |
|
358 | - * |
|
359 | - * @since 1.0.13 |
|
360 | - * |
|
361 | - * @param WPinv_Item $item Inserted or updated item object. |
|
362 | - * @param WP_REST_Request $request Request object. |
|
363 | - * @param bool $creating True when creating a post, false when updating. |
|
364 | - */ |
|
365 | - do_action( "wpinv_rest_insert_item", $item, $request, true ); |
|
366 | - |
|
367 | - /** |
|
368 | - * Filters the responses for creating single item requests. |
|
369 | - * |
|
370 | - * |
|
371 | - * @since 1.0.13 |
|
372 | - * |
|
373 | - * |
|
374 | - * @param array $item_data Invoice properties. |
|
375 | - * @param WP_REST_Request $request The request used. |
|
376 | - */ |
|
351 | + } |
|
352 | + |
|
353 | + // Prepare the response |
|
354 | + $response = $this->prepare_item_for_response( $item, $request ); |
|
355 | + |
|
356 | + /** |
|
357 | + * Fires after a single invoice item is created or updated via the REST API. |
|
358 | + * |
|
359 | + * @since 1.0.13 |
|
360 | + * |
|
361 | + * @param WPinv_Item $item Inserted or updated item object. |
|
362 | + * @param WP_REST_Request $request Request object. |
|
363 | + * @param bool $creating True when creating a post, false when updating. |
|
364 | + */ |
|
365 | + do_action( "wpinv_rest_insert_item", $item, $request, true ); |
|
366 | + |
|
367 | + /** |
|
368 | + * Filters the responses for creating single item requests. |
|
369 | + * |
|
370 | + * |
|
371 | + * @since 1.0.13 |
|
372 | + * |
|
373 | + * |
|
374 | + * @param array $item_data Invoice properties. |
|
375 | + * @param WP_REST_Request $request The request used. |
|
376 | + */ |
|
377 | 377 | $response = apply_filters( "wpinv_rest_create_item_response", $response, $request ); |
378 | 378 | |
379 | 379 | return rest_ensure_response( $response ); |
380 | - } |
|
381 | - |
|
382 | - /** |
|
383 | - * Checks if a given request has access to update an item. |
|
384 | - * |
|
385 | - * @since 1.0.13 |
|
386 | - * |
|
387 | - * @param WP_REST_Request $request Full details about the request. |
|
388 | - * @return true|WP_Error True if the request has access to update the item, WP_Error object otherwise. |
|
389 | - */ |
|
390 | - public function update_item_permissions_check( $request ) { |
|
391 | - |
|
392 | - // Retrieve the item. |
|
393 | - $item = $this->get_post( $request['id'] ); |
|
394 | - if ( is_wp_error( $item ) ) { |
|
395 | - return $item; |
|
396 | - } |
|
397 | - |
|
398 | - if ( wpinv_current_user_can_manage_invoicing() ) { |
|
399 | - return true; |
|
400 | - } |
|
401 | - |
|
402 | - return new WP_Error( |
|
403 | - 'rest_cannot_edit', |
|
404 | - __( 'Sorry, you are not allowed to update this item.', 'invoicing' ), |
|
405 | - array( |
|
406 | - 'status' => rest_authorization_required_code(), |
|
407 | - ) |
|
408 | - ); |
|
409 | - |
|
410 | - } |
|
411 | - |
|
412 | - /** |
|
413 | - * Updates a single item. |
|
414 | - * |
|
415 | - * @since 1.0.13 |
|
416 | - * |
|
417 | - * @param WP_REST_Request $request Full details about the request. |
|
418 | - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
419 | - */ |
|
420 | - public function update_item( $request ) { |
|
380 | + } |
|
381 | + |
|
382 | + /** |
|
383 | + * Checks if a given request has access to update an item. |
|
384 | + * |
|
385 | + * @since 1.0.13 |
|
386 | + * |
|
387 | + * @param WP_REST_Request $request Full details about the request. |
|
388 | + * @return true|WP_Error True if the request has access to update the item, WP_Error object otherwise. |
|
389 | + */ |
|
390 | + public function update_item_permissions_check( $request ) { |
|
391 | + |
|
392 | + // Retrieve the item. |
|
393 | + $item = $this->get_post( $request['id'] ); |
|
394 | + if ( is_wp_error( $item ) ) { |
|
395 | + return $item; |
|
396 | + } |
|
397 | + |
|
398 | + if ( wpinv_current_user_can_manage_invoicing() ) { |
|
399 | + return true; |
|
400 | + } |
|
401 | + |
|
402 | + return new WP_Error( |
|
403 | + 'rest_cannot_edit', |
|
404 | + __( 'Sorry, you are not allowed to update this item.', 'invoicing' ), |
|
405 | + array( |
|
406 | + 'status' => rest_authorization_required_code(), |
|
407 | + ) |
|
408 | + ); |
|
409 | + |
|
410 | + } |
|
411 | + |
|
412 | + /** |
|
413 | + * Updates a single item. |
|
414 | + * |
|
415 | + * @since 1.0.13 |
|
416 | + * |
|
417 | + * @param WP_REST_Request $request Full details about the request. |
|
418 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
419 | + */ |
|
420 | + public function update_item( $request ) { |
|
421 | 421 | |
422 | - // Ensure the item exists. |
|
422 | + // Ensure the item exists. |
|
423 | 423 | $valid_check = $this->get_post( $request['id'] ); |
424 | 424 | |
425 | 425 | // Abort early if it does not exist |
426 | - if ( is_wp_error( $valid_check ) ) { |
|
427 | - return $valid_check; |
|
428 | - } |
|
426 | + if ( is_wp_error( $valid_check ) ) { |
|
427 | + return $valid_check; |
|
428 | + } |
|
429 | 429 | |
430 | - $request->set_param( 'context', 'edit' ); |
|
430 | + $request->set_param( 'context', 'edit' ); |
|
431 | 431 | |
432 | - // Prepare the updated data. |
|
433 | - $data_to_update = $this->prepare_item_for_database( $request ); |
|
432 | + // Prepare the updated data. |
|
433 | + $data_to_update = $this->prepare_item_for_database( $request ); |
|
434 | 434 | |
435 | - if ( is_wp_error( $data_to_update ) ) { |
|
436 | - return $data_to_update; |
|
437 | - } |
|
435 | + if ( is_wp_error( $data_to_update ) ) { |
|
436 | + return $data_to_update; |
|
437 | + } |
|
438 | 438 | |
439 | - // Abort if no item data is provided |
|
439 | + // Abort if no item data is provided |
|
440 | 440 | if( empty( $data_to_update ) ) { |
441 | 441 | return new WP_Error( 'missing_data', __( 'An update request cannot be empty.', 'invoicing' ) ); |
442 | 442 | } |
443 | 443 | |
444 | - // Include the item ID |
|
445 | - $data_to_update['ID'] = $request['id']; |
|
446 | - |
|
447 | - // Update the item |
|
448 | - $updated_item = wpinv_update_item( $data_to_update, true ); |
|
449 | - |
|
450 | - // Incase the update operation failed... |
|
451 | - if ( is_wp_error( $updated_item ) ) { |
|
452 | - return $updated_item; |
|
453 | - } |
|
454 | - |
|
455 | - // Prepare the response |
|
456 | - $response = $this->prepare_item_for_response( $updated_item, $request ); |
|
457 | - |
|
458 | - /** This action is documented in includes/class-wpinv-rest-item-controller.php */ |
|
459 | - do_action( "wpinv_rest_insert_item", $updated_item, $request, false ); |
|
460 | - |
|
461 | - /** |
|
462 | - * Filters the responses for updating single item requests. |
|
463 | - * |
|
464 | - * |
|
465 | - * @since 1.0.13 |
|
466 | - * |
|
467 | - * |
|
468 | - * @param array $data_to_update Item properties. |
|
469 | - * @param WP_REST_Request $request The request used. |
|
470 | - */ |
|
444 | + // Include the item ID |
|
445 | + $data_to_update['ID'] = $request['id']; |
|
446 | + |
|
447 | + // Update the item |
|
448 | + $updated_item = wpinv_update_item( $data_to_update, true ); |
|
449 | + |
|
450 | + // Incase the update operation failed... |
|
451 | + if ( is_wp_error( $updated_item ) ) { |
|
452 | + return $updated_item; |
|
453 | + } |
|
454 | + |
|
455 | + // Prepare the response |
|
456 | + $response = $this->prepare_item_for_response( $updated_item, $request ); |
|
457 | + |
|
458 | + /** This action is documented in includes/class-wpinv-rest-item-controller.php */ |
|
459 | + do_action( "wpinv_rest_insert_item", $updated_item, $request, false ); |
|
460 | + |
|
461 | + /** |
|
462 | + * Filters the responses for updating single item requests. |
|
463 | + * |
|
464 | + * |
|
465 | + * @since 1.0.13 |
|
466 | + * |
|
467 | + * |
|
468 | + * @param array $data_to_update Item properties. |
|
469 | + * @param WP_REST_Request $request The request used. |
|
470 | + */ |
|
471 | 471 | $response = apply_filters( "wpinv_rest_update_item_response", $response, $data_to_update, $request ); |
472 | 472 | |
473 | 473 | return rest_ensure_response( $response ); |
474 | - } |
|
475 | - |
|
476 | - /** |
|
477 | - * Checks if a given request has access to delete an item. |
|
478 | - * |
|
479 | - * @since 1.0.13 |
|
480 | - * |
|
481 | - * @param WP_REST_Request $request Full details about the request. |
|
482 | - * @return true|WP_Error True if the request has access to delete the item, WP_Error object otherwise. |
|
483 | - */ |
|
484 | - public function delete_item_permissions_check( $request ) { |
|
485 | - |
|
486 | - // Retrieve the item. |
|
487 | - $item = $this->get_post( $request['id'] ); |
|
488 | - if ( is_wp_error( $item ) ) { |
|
489 | - return $item; |
|
490 | - } |
|
491 | - |
|
492 | - // |
|
493 | - |
|
494 | - // Ensure the current user can delete the item |
|
495 | - if (! wpinv_can_delete_item( $request['id'] ) ) { |
|
496 | - return new WP_Error( |
|
474 | + } |
|
475 | + |
|
476 | + /** |
|
477 | + * Checks if a given request has access to delete an item. |
|
478 | + * |
|
479 | + * @since 1.0.13 |
|
480 | + * |
|
481 | + * @param WP_REST_Request $request Full details about the request. |
|
482 | + * @return true|WP_Error True if the request has access to delete the item, WP_Error object otherwise. |
|
483 | + */ |
|
484 | + public function delete_item_permissions_check( $request ) { |
|
485 | + |
|
486 | + // Retrieve the item. |
|
487 | + $item = $this->get_post( $request['id'] ); |
|
488 | + if ( is_wp_error( $item ) ) { |
|
489 | + return $item; |
|
490 | + } |
|
491 | + |
|
492 | + // |
|
493 | + |
|
494 | + // Ensure the current user can delete the item |
|
495 | + if (! wpinv_can_delete_item( $request['id'] ) ) { |
|
496 | + return new WP_Error( |
|
497 | 497 | 'rest_cannot_delete', |
498 | 498 | __( 'Sorry, you are not allowed to delete this item.', 'invoicing' ), |
499 | 499 | array( |
500 | 500 | 'status' => rest_authorization_required_code(), |
501 | 501 | ) |
502 | 502 | ); |
503 | - } |
|
504 | - |
|
505 | - return true; |
|
506 | - } |
|
507 | - |
|
508 | - /** |
|
509 | - * Deletes a single item. |
|
510 | - * |
|
511 | - * @since 1.0.13 |
|
512 | - * |
|
513 | - * @param WP_REST_Request $request Full details about the request. |
|
514 | - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
515 | - */ |
|
516 | - public function delete_item( $request ) { |
|
503 | + } |
|
504 | + |
|
505 | + return true; |
|
506 | + } |
|
507 | + |
|
508 | + /** |
|
509 | + * Deletes a single item. |
|
510 | + * |
|
511 | + * @since 1.0.13 |
|
512 | + * |
|
513 | + * @param WP_REST_Request $request Full details about the request. |
|
514 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
515 | + */ |
|
516 | + public function delete_item( $request ) { |
|
517 | 517 | |
518 | - // Retrieve the item. |
|
519 | - $item = $this->get_post( $request['id'] ); |
|
520 | - if ( is_wp_error( $item ) ) { |
|
521 | - return $item; |
|
522 | - } |
|
518 | + // Retrieve the item. |
|
519 | + $item = $this->get_post( $request['id'] ); |
|
520 | + if ( is_wp_error( $item ) ) { |
|
521 | + return $item; |
|
522 | + } |
|
523 | 523 | |
524 | - $request->set_param( 'context', 'edit' ); |
|
524 | + $request->set_param( 'context', 'edit' ); |
|
525 | 525 | |
526 | - // Prepare the item id |
|
527 | - $id = $item->ID; |
|
526 | + // Prepare the item id |
|
527 | + $id = $item->ID; |
|
528 | 528 | |
529 | - // Prepare the response |
|
530 | - $response = $this->prepare_item_for_response( $item, $request ); |
|
529 | + // Prepare the response |
|
530 | + $response = $this->prepare_item_for_response( $item, $request ); |
|
531 | 531 | |
532 | - // Check if the user wants to bypass the trash... |
|
533 | - $force_delete = (bool) $request['force']; |
|
532 | + // Check if the user wants to bypass the trash... |
|
533 | + $force_delete = (bool) $request['force']; |
|
534 | 534 | |
535 | - // Try deleting the item. |
|
536 | - $deleted = wp_delete_post( $id, $force_delete ); |
|
535 | + // Try deleting the item. |
|
536 | + $deleted = wp_delete_post( $id, $force_delete ); |
|
537 | 537 | |
538 | - // Abort early if we can't delete the item. |
|
539 | - if ( ! $deleted ) { |
|
540 | - return new WP_Error( 'rest_cannot_delete', __( 'The item cannot be deleted.', 'invoicing' ), array( 'status' => 500 ) ); |
|
541 | - } |
|
538 | + // Abort early if we can't delete the item. |
|
539 | + if ( ! $deleted ) { |
|
540 | + return new WP_Error( 'rest_cannot_delete', __( 'The item cannot be deleted.', 'invoicing' ), array( 'status' => 500 ) ); |
|
541 | + } |
|
542 | 542 | |
543 | - /** |
|
544 | - * Fires immediately after a single item is deleted or trashed via the REST API. |
|
545 | - * |
|
546 | - * |
|
547 | - * @since 1.0.13 |
|
548 | - * |
|
549 | - * @param WPInv_Item $item The deleted or trashed item. |
|
550 | - * @param WP_REST_Request $request The request sent to the API. |
|
551 | - */ |
|
552 | - do_action( "wpinv_rest_delete_item", $item, $request ); |
|
543 | + /** |
|
544 | + * Fires immediately after a single item is deleted or trashed via the REST API. |
|
545 | + * |
|
546 | + * |
|
547 | + * @since 1.0.13 |
|
548 | + * |
|
549 | + * @param WPInv_Item $item The deleted or trashed item. |
|
550 | + * @param WP_REST_Request $request The request sent to the API. |
|
551 | + */ |
|
552 | + do_action( "wpinv_rest_delete_item", $item, $request ); |
|
553 | 553 | |
554 | - return $response; |
|
554 | + return $response; |
|
555 | 555 | |
556 | - } |
|
556 | + } |
|
557 | 557 | |
558 | 558 | |
559 | 559 | /** |
560 | - * Retrieves the query params for the items collection. |
|
561 | - * |
|
562 | - * @since 1.0.13 |
|
563 | - * |
|
564 | - * @return array Collection parameters. |
|
565 | - */ |
|
566 | - public function get_collection_params() { |
|
560 | + * Retrieves the query params for the items collection. |
|
561 | + * |
|
562 | + * @since 1.0.13 |
|
563 | + * |
|
564 | + * @return array Collection parameters. |
|
565 | + */ |
|
566 | + public function get_collection_params() { |
|
567 | 567 | |
568 | 568 | $query_params = array( |
569 | 569 | |
@@ -577,27 +577,27 @@ discard block |
||
577 | 577 | |
578 | 578 | // Item types |
579 | 579 | 'type' => array( |
580 | - 'description' => __( 'Type of items to fetch.', 'invoicing' ), |
|
581 | - 'type' => 'array', |
|
582 | - 'default' => wpinv_item_types(), |
|
583 | - 'items' => array( |
|
580 | + 'description' => __( 'Type of items to fetch.', 'invoicing' ), |
|
581 | + 'type' => 'array', |
|
582 | + 'default' => wpinv_item_types(), |
|
583 | + 'items' => array( |
|
584 | 584 | 'enum' => wpinv_item_types(), |
585 | 585 | 'type' => 'string', |
586 | 586 | ), |
587 | - ), |
|
587 | + ), |
|
588 | 588 | |
589 | - // Number of results per page |
|
589 | + // Number of results per page |
|
590 | 590 | 'limit' => array( |
591 | - 'description' => __( 'Number of items to fetch.', 'invoicing' ), |
|
592 | - 'type' => 'integer', |
|
593 | - 'default' => (int) get_option( 'posts_per_page' ), |
|
591 | + 'description' => __( 'Number of items to fetch.', 'invoicing' ), |
|
592 | + 'type' => 'integer', |
|
593 | + 'default' => (int) get_option( 'posts_per_page' ), |
|
594 | 594 | ), |
595 | 595 | |
596 | 596 | // Pagination |
597 | 597 | 'page' => array( |
598 | - 'description' => __( 'Current page to fetch.', 'invoicing' ), |
|
599 | - 'type' => 'integer', |
|
600 | - 'default' => 1, |
|
598 | + 'description' => __( 'Current page to fetch.', 'invoicing' ), |
|
599 | + 'type' => 'integer', |
|
600 | + 'default' => 1, |
|
601 | 601 | ), |
602 | 602 | |
603 | 603 | // Exclude certain items |
@@ -620,9 +620,9 @@ discard block |
||
620 | 620 | 'date', |
621 | 621 | 'ID', |
622 | 622 | 'modified', |
623 | - 'title', |
|
624 | - 'relevance', |
|
625 | - 'rand' |
|
623 | + 'title', |
|
624 | + 'relevance', |
|
625 | + 'rand' |
|
626 | 626 | ), |
627 | 627 | ), |
628 | 628 | |
@@ -632,617 +632,617 @@ discard block |
||
632 | 632 | 'type' => 'string', |
633 | 633 | 'default' => 'DESC', |
634 | 634 | 'enum' => array( 'ASC', 'DESC' ), |
635 | - ), |
|
635 | + ), |
|
636 | 636 | |
637 | - // Search term |
|
637 | + // Search term |
|
638 | 638 | 'search' => array( |
639 | - 'description' => __( 'Return items that match the search term.', 'invoicing' ), |
|
640 | - 'type' => 'string', |
|
639 | + 'description' => __( 'Return items that match the search term.', 'invoicing' ), |
|
640 | + 'type' => 'string', |
|
641 | 641 | ), |
642 | 642 | ); |
643 | 643 | |
644 | - /** |
|
645 | - * Filter collection parameters for the items controller. |
|
646 | - * |
|
647 | - * |
|
648 | - * @since 1.0.13 |
|
649 | - * |
|
650 | - * @param array $query_params JSON Schema-formatted collection parameters. |
|
651 | - */ |
|
652 | - return apply_filters( "wpinv_rest_items_collection_params", $query_params ); |
|
644 | + /** |
|
645 | + * Filter collection parameters for the items controller. |
|
646 | + * |
|
647 | + * |
|
648 | + * @since 1.0.13 |
|
649 | + * |
|
650 | + * @param array $query_params JSON Schema-formatted collection parameters. |
|
651 | + */ |
|
652 | + return apply_filters( "wpinv_rest_items_collection_params", $query_params ); |
|
653 | 653 | } |
654 | 654 | |
655 | 655 | /** |
656 | - * Checks if a given post type can be viewed or managed. |
|
657 | - * |
|
658 | - * @since 1.0.13 |
|
659 | - * |
|
660 | - * @param object|string $post_type Post type name or object. |
|
661 | - * @return bool Whether the post type is allowed in REST. |
|
662 | - */ |
|
663 | - protected function check_is_post_type_allowed( $post_type ) { |
|
664 | - return true; |
|
665 | - } |
|
666 | - |
|
667 | - /** |
|
668 | - * Prepares a single item for create or update. |
|
669 | - * |
|
670 | - * @since 1.0.13 |
|
671 | - * |
|
672 | - * @param WP_REST_Request $request Request object. |
|
673 | - * @return array|WP_Error Invoice Properties or WP_Error. |
|
674 | - */ |
|
675 | - protected function prepare_item_for_database( $request ) { |
|
676 | - $prepared_item = new stdClass(); |
|
677 | - |
|
678 | - // Post ID. |
|
679 | - if ( isset( $request['id'] ) ) { |
|
680 | - $existing_item = $this->get_post( $request['id'] ); |
|
681 | - if ( is_wp_error( $existing_item ) ) { |
|
682 | - return $existing_item; |
|
683 | - } |
|
684 | - |
|
685 | - $prepared_item->ID = $existing_item->ID; |
|
686 | - } |
|
687 | - |
|
688 | - $schema = $this->get_item_schema(); |
|
689 | - |
|
690 | - // item title. |
|
691 | - if ( ! empty( $schema['properties']['name'] ) && isset( $request['name'] ) ) { |
|
692 | - $prepared_item->title = sanitize_text_field( $request['name'] ); |
|
693 | - } |
|
694 | - |
|
695 | - // item summary. |
|
696 | - if ( ! empty( $schema['properties']['summary'] ) && isset( $request['summary'] ) ) { |
|
697 | - $prepared_item->excerpt = wp_kses_post( $request['summary'] ); |
|
698 | - } |
|
699 | - |
|
700 | - // item price. |
|
701 | - if ( ! empty( $schema['properties']['price'] ) && isset( $request['price'] ) ) { |
|
702 | - $prepared_item->price = floatval( $request['price'] ); |
|
703 | - } |
|
704 | - |
|
705 | - // minimum price (for dynamc items). |
|
706 | - if ( ! empty( $schema['properties']['minimum_price'] ) && isset( $request['minimum_price'] ) ) { |
|
707 | - $prepared_item->minimum_price = floatval( $request['minimum_price'] ); |
|
708 | - } |
|
709 | - |
|
710 | - // item status. |
|
711 | - if ( ! empty( $schema['properties']['status'] ) && isset( $request['status'] ) ) { |
|
712 | - $prepared_item->status = 'publish' === $request['status'] ? 'publish' : 'pending'; |
|
713 | - } |
|
714 | - |
|
715 | - // item type. |
|
716 | - if ( ! empty( $schema['properties']['type'] ) && isset( $request['type'] ) ) { |
|
717 | - $prepared_item->type = in_array( $request['type'], wpinv_item_types() ) ? trim( strtolower( $request['type'] ) ) : 'custom'; |
|
718 | - } |
|
719 | - |
|
720 | - // VAT rule. |
|
721 | - if ( ! empty( $schema['properties']['vat_rule'] ) && isset( $request['vat_rule'] ) ) { |
|
722 | - $prepared_item->vat_rule = 'digital' === $request['vat_rule'] ? 'digital' : 'physical'; |
|
723 | - } |
|
724 | - |
|
725 | - // Simple strings. |
|
726 | - foreach( array( 'custom_id', 'custom_name', 'custom_singular_name' ) as $property ) { |
|
727 | - |
|
728 | - if ( ! empty( $schema['properties'][$property] ) && isset( $request[$property] ) ) { |
|
729 | - $prepared_item->$property = sanitize_text_field( $request[$property] ); |
|
730 | - } |
|
731 | - |
|
732 | - } |
|
733 | - |
|
734 | - // Simple integers. |
|
735 | - foreach( array( 'is_recurring', 'recurring_interval', 'recurring_limit', 'free_trial', 'trial_interval', 'dynamic_pricing', 'editable' ) as $property ) { |
|
736 | - |
|
737 | - if ( ! empty( $schema['properties'][$property] ) && isset( $request[$property] ) ) { |
|
738 | - $prepared_item->$property = intval( $request[$property] ); |
|
739 | - } |
|
740 | - |
|
741 | - } |
|
742 | - |
|
743 | - // Time periods. |
|
744 | - foreach( array( 'recurring_period', 'trial_period' ) as $property ) { |
|
745 | - |
|
746 | - if ( ! empty( $schema['properties'][$property] ) && isset( $request[$property] ) ) { |
|
747 | - $prepared_item->$property = in_array( $request[$property], array( 'D', 'W', 'M', 'Y' ) ) ? trim( strtoupper( $request[$property] ) ) : 'D'; |
|
748 | - } |
|
749 | - |
|
750 | - } |
|
751 | - |
|
752 | - $item_data = (array) wp_unslash( $prepared_item ); |
|
753 | - |
|
754 | - /** |
|
755 | - * Filters an item before it is inserted via the REST API. |
|
756 | - * |
|
757 | - * @since 1.0.13 |
|
758 | - * |
|
759 | - * @param array $item_data An array of item data |
|
760 | - * @param WP_REST_Request $request Request object. |
|
761 | - */ |
|
762 | - return apply_filters( "wpinv_rest_pre_insert_item", $item_data, $request ); |
|
763 | - |
|
764 | - } |
|
765 | - |
|
766 | - /** |
|
767 | - * Prepares a single item output for response. |
|
768 | - * |
|
769 | - * @since 1.0.13 |
|
770 | - * |
|
771 | - * @param WPInv_Item $item item object. |
|
772 | - * @param WP_REST_Request $request Request object. |
|
773 | - * @return WP_REST_Response Response object. |
|
774 | - */ |
|
775 | - public function prepare_item_for_response( $item, $request ) { |
|
656 | + * Checks if a given post type can be viewed or managed. |
|
657 | + * |
|
658 | + * @since 1.0.13 |
|
659 | + * |
|
660 | + * @param object|string $post_type Post type name or object. |
|
661 | + * @return bool Whether the post type is allowed in REST. |
|
662 | + */ |
|
663 | + protected function check_is_post_type_allowed( $post_type ) { |
|
664 | + return true; |
|
665 | + } |
|
776 | 666 | |
777 | - $GLOBALS['post'] = get_post( $item->get_ID() ); |
|
667 | + /** |
|
668 | + * Prepares a single item for create or update. |
|
669 | + * |
|
670 | + * @since 1.0.13 |
|
671 | + * |
|
672 | + * @param WP_REST_Request $request Request object. |
|
673 | + * @return array|WP_Error Invoice Properties or WP_Error. |
|
674 | + */ |
|
675 | + protected function prepare_item_for_database( $request ) { |
|
676 | + $prepared_item = new stdClass(); |
|
677 | + |
|
678 | + // Post ID. |
|
679 | + if ( isset( $request['id'] ) ) { |
|
680 | + $existing_item = $this->get_post( $request['id'] ); |
|
681 | + if ( is_wp_error( $existing_item ) ) { |
|
682 | + return $existing_item; |
|
683 | + } |
|
778 | 684 | |
779 | - setup_postdata( $item->get_ID() ); |
|
685 | + $prepared_item->ID = $existing_item->ID; |
|
686 | + } |
|
780 | 687 | |
781 | - // Fetch the fields to include in this response. |
|
782 | - $fields = $this->get_fields_for_response( $request ); |
|
688 | + $schema = $this->get_item_schema(); |
|
783 | 689 | |
784 | - // Base fields for every item. |
|
785 | - $data = array(); |
|
690 | + // item title. |
|
691 | + if ( ! empty( $schema['properties']['name'] ) && isset( $request['name'] ) ) { |
|
692 | + $prepared_item->title = sanitize_text_field( $request['name'] ); |
|
693 | + } |
|
786 | 694 | |
787 | - // Set up ID |
|
788 | - if ( rest_is_field_included( 'id', $fields ) ) { |
|
789 | - $data['id'] = $item->get_ID(); |
|
790 | - } |
|
695 | + // item summary. |
|
696 | + if ( ! empty( $schema['properties']['summary'] ) && isset( $request['summary'] ) ) { |
|
697 | + $prepared_item->excerpt = wp_kses_post( $request['summary'] ); |
|
698 | + } |
|
791 | 699 | |
700 | + // item price. |
|
701 | + if ( ! empty( $schema['properties']['price'] ) && isset( $request['price'] ) ) { |
|
702 | + $prepared_item->price = floatval( $request['price'] ); |
|
703 | + } |
|
792 | 704 | |
793 | - // Item properties |
|
794 | - $item_properties = array( |
|
795 | - 'name', 'summary', 'price', 'status', 'type', |
|
796 | - 'vat_rule', 'vat_class', |
|
797 | - 'custom_id', 'custom_name', 'custom_singular_name', |
|
798 | - 'editable' |
|
799 | - ); |
|
705 | + // minimum price (for dynamc items). |
|
706 | + if ( ! empty( $schema['properties']['minimum_price'] ) && isset( $request['minimum_price'] ) ) { |
|
707 | + $prepared_item->minimum_price = floatval( $request['minimum_price'] ); |
|
708 | + } |
|
709 | + |
|
710 | + // item status. |
|
711 | + if ( ! empty( $schema['properties']['status'] ) && isset( $request['status'] ) ) { |
|
712 | + $prepared_item->status = 'publish' === $request['status'] ? 'publish' : 'pending'; |
|
713 | + } |
|
800 | 714 | |
801 | - foreach( $item_properties as $property ) { |
|
715 | + // item type. |
|
716 | + if ( ! empty( $schema['properties']['type'] ) && isset( $request['type'] ) ) { |
|
717 | + $prepared_item->type = in_array( $request['type'], wpinv_item_types() ) ? trim( strtolower( $request['type'] ) ) : 'custom'; |
|
718 | + } |
|
802 | 719 | |
803 | - if ( rest_is_field_included( $property, $fields ) && method_exists( $item, 'get_' . $property ) ) { |
|
804 | - $data[$property] = call_user_func( array( $item, 'get_' . $property ) ); |
|
805 | - } |
|
720 | + // VAT rule. |
|
721 | + if ( ! empty( $schema['properties']['vat_rule'] ) && isset( $request['vat_rule'] ) ) { |
|
722 | + $prepared_item->vat_rule = 'digital' === $request['vat_rule'] ? 'digital' : 'physical'; |
|
723 | + } |
|
806 | 724 | |
807 | - } |
|
725 | + // Simple strings. |
|
726 | + foreach( array( 'custom_id', 'custom_name', 'custom_singular_name' ) as $property ) { |
|
808 | 727 | |
809 | - // Dynamic pricing. |
|
810 | - if( $item->supports_dynamic_pricing() ) { |
|
728 | + if ( ! empty( $schema['properties'][$property] ) && isset( $request[$property] ) ) { |
|
729 | + $prepared_item->$property = sanitize_text_field( $request[$property] ); |
|
730 | + } |
|
811 | 731 | |
812 | - if( rest_is_field_included( 'dynamic_pricing', $fields ) ) { |
|
813 | - $data['dynamic_pricing'] = $item->get_is_dynamic_pricing(); |
|
814 | - } |
|
732 | + } |
|
815 | 733 | |
816 | - if( rest_is_field_included( 'minimum_price', $fields ) ) { |
|
817 | - $data['minimum_price'] = $item->get_minimum_price(); |
|
818 | - } |
|
819 | - } |
|
734 | + // Simple integers. |
|
735 | + foreach( array( 'is_recurring', 'recurring_interval', 'recurring_limit', 'free_trial', 'trial_interval', 'dynamic_pricing', 'editable' ) as $property ) { |
|
820 | 736 | |
821 | - // Subscriptions. |
|
822 | - if( rest_is_field_included( 'is_recurring', $fields ) ) { |
|
823 | - $data['is_recurring'] = $item->get_is_recurring(); |
|
824 | - } |
|
737 | + if ( ! empty( $schema['properties'][$property] ) && isset( $request[$property] ) ) { |
|
738 | + $prepared_item->$property = intval( $request[$property] ); |
|
739 | + } |
|
825 | 740 | |
826 | - if( $item->is_recurring() ) { |
|
741 | + } |
|
827 | 742 | |
828 | - $recurring_fields = array( 'is_recurring', 'recurring_period', 'recurring_interval', 'recurring_limit', 'free_trial' ); |
|
829 | - foreach( $recurring_fields as $field ) { |
|
743 | + // Time periods. |
|
744 | + foreach( array( 'recurring_period', 'trial_period' ) as $property ) { |
|
830 | 745 | |
831 | - if ( rest_is_field_included( $field, $fields ) && method_exists( $item, 'get_' . $field ) ) { |
|
832 | - $data[$field] = call_user_func( array( $item, 'get_' . $field ) ); |
|
833 | - } |
|
746 | + if ( ! empty( $schema['properties'][$property] ) && isset( $request[$property] ) ) { |
|
747 | + $prepared_item->$property = in_array( $request[$property], array( 'D', 'W', 'M', 'Y' ) ) ? trim( strtoupper( $request[$property] ) ) : 'D'; |
|
748 | + } |
|
749 | + |
|
750 | + } |
|
751 | + |
|
752 | + $item_data = (array) wp_unslash( $prepared_item ); |
|
753 | + |
|
754 | + /** |
|
755 | + * Filters an item before it is inserted via the REST API. |
|
756 | + * |
|
757 | + * @since 1.0.13 |
|
758 | + * |
|
759 | + * @param array $item_data An array of item data |
|
760 | + * @param WP_REST_Request $request Request object. |
|
761 | + */ |
|
762 | + return apply_filters( "wpinv_rest_pre_insert_item", $item_data, $request ); |
|
763 | + |
|
764 | + } |
|
765 | + |
|
766 | + /** |
|
767 | + * Prepares a single item output for response. |
|
768 | + * |
|
769 | + * @since 1.0.13 |
|
770 | + * |
|
771 | + * @param WPInv_Item $item item object. |
|
772 | + * @param WP_REST_Request $request Request object. |
|
773 | + * @return WP_REST_Response Response object. |
|
774 | + */ |
|
775 | + public function prepare_item_for_response( $item, $request ) { |
|
776 | + |
|
777 | + $GLOBALS['post'] = get_post( $item->get_ID() ); |
|
778 | + |
|
779 | + setup_postdata( $item->get_ID() ); |
|
780 | + |
|
781 | + // Fetch the fields to include in this response. |
|
782 | + $fields = $this->get_fields_for_response( $request ); |
|
783 | + |
|
784 | + // Base fields for every item. |
|
785 | + $data = array(); |
|
786 | + |
|
787 | + // Set up ID |
|
788 | + if ( rest_is_field_included( 'id', $fields ) ) { |
|
789 | + $data['id'] = $item->get_ID(); |
|
790 | + } |
|
791 | + |
|
792 | + |
|
793 | + // Item properties |
|
794 | + $item_properties = array( |
|
795 | + 'name', 'summary', 'price', 'status', 'type', |
|
796 | + 'vat_rule', 'vat_class', |
|
797 | + 'custom_id', 'custom_name', 'custom_singular_name', |
|
798 | + 'editable' |
|
799 | + ); |
|
800 | + |
|
801 | + foreach( $item_properties as $property ) { |
|
802 | + |
|
803 | + if ( rest_is_field_included( $property, $fields ) && method_exists( $item, 'get_' . $property ) ) { |
|
804 | + $data[$property] = call_user_func( array( $item, 'get_' . $property ) ); |
|
805 | + } |
|
806 | + |
|
807 | + } |
|
808 | + |
|
809 | + // Dynamic pricing. |
|
810 | + if( $item->supports_dynamic_pricing() ) { |
|
811 | + |
|
812 | + if( rest_is_field_included( 'dynamic_pricing', $fields ) ) { |
|
813 | + $data['dynamic_pricing'] = $item->get_is_dynamic_pricing(); |
|
814 | + } |
|
815 | + |
|
816 | + if( rest_is_field_included( 'minimum_price', $fields ) ) { |
|
817 | + $data['minimum_price'] = $item->get_minimum_price(); |
|
818 | + } |
|
819 | + } |
|
820 | + |
|
821 | + // Subscriptions. |
|
822 | + if( rest_is_field_included( 'is_recurring', $fields ) ) { |
|
823 | + $data['is_recurring'] = $item->get_is_recurring(); |
|
824 | + } |
|
825 | + |
|
826 | + if( $item->is_recurring() ) { |
|
827 | + |
|
828 | + $recurring_fields = array( 'is_recurring', 'recurring_period', 'recurring_interval', 'recurring_limit', 'free_trial' ); |
|
829 | + foreach( $recurring_fields as $field ) { |
|
830 | + |
|
831 | + if ( rest_is_field_included( $field, $fields ) && method_exists( $item, 'get_' . $field ) ) { |
|
832 | + $data[$field] = call_user_func( array( $item, 'get_' . $field ) ); |
|
833 | + } |
|
834 | 834 | |
835 | - } |
|
835 | + } |
|
836 | 836 | |
837 | - if( $item->has_free_trial() ) { |
|
837 | + if( $item->has_free_trial() ) { |
|
838 | 838 | |
839 | - $trial_fields = array( 'trial_period', 'trial_interval' ); |
|
840 | - foreach( $trial_fields as $field ) { |
|
839 | + $trial_fields = array( 'trial_period', 'trial_interval' ); |
|
840 | + foreach( $trial_fields as $field ) { |
|
841 | 841 | |
842 | - if ( rest_is_field_included( $field, $fields ) && method_exists( $item, 'get_' . $field ) ) { |
|
843 | - $data[$field] = call_user_func( array( $item, 'get_' . $field ) ); |
|
844 | - } |
|
842 | + if ( rest_is_field_included( $field, $fields ) && method_exists( $item, 'get_' . $field ) ) { |
|
843 | + $data[$field] = call_user_func( array( $item, 'get_' . $field ) ); |
|
844 | + } |
|
845 | 845 | |
846 | - } |
|
847 | - |
|
848 | - } |
|
849 | - |
|
850 | - } |
|
851 | - |
|
852 | - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
853 | - $data = $this->add_additional_fields_to_object( $data, $request ); |
|
854 | - $data = $this->filter_response_by_context( $data, $context ); |
|
855 | - |
|
856 | - // Wrap the data in a response object. |
|
857 | - $response = rest_ensure_response( $data ); |
|
858 | - |
|
859 | - $links = $this->prepare_links( $item ); |
|
860 | - $response->add_links( $links ); |
|
861 | - |
|
862 | - if ( ! empty( $links['self']['href'] ) ) { |
|
863 | - $actions = $this->get_available_actions( $item, $request ); |
|
864 | - |
|
865 | - $self = $links['self']['href']; |
|
866 | - |
|
867 | - foreach ( $actions as $rel ) { |
|
868 | - $response->add_link( $rel, $self ); |
|
869 | - } |
|
870 | - } |
|
871 | - |
|
872 | - /** |
|
873 | - * Filters the item data for a response. |
|
874 | - * |
|
875 | - * @since 1.0.13 |
|
876 | - * |
|
877 | - * @param WP_REST_Response $response The response object. |
|
878 | - * @param WPInv_Item $item The item object. |
|
879 | - * @param WP_REST_Request $request Request object. |
|
880 | - */ |
|
881 | - return apply_filters( "wpinv_rest_prepare_item", $response, $item, $request ); |
|
882 | - } |
|
883 | - |
|
884 | - /** |
|
885 | - * Gets an array of fields to be included on the response. |
|
886 | - * |
|
887 | - * Included fields are based on item schema and `_fields=` request argument. |
|
888 | - * |
|
889 | - * @since 1.0.13 |
|
890 | - * |
|
891 | - * @param WP_REST_Request $request Full details about the request. |
|
892 | - * @return array Fields to be included in the response. |
|
893 | - */ |
|
894 | - public function get_fields_for_response( $request ) { |
|
895 | - $schema = $this->get_item_schema(); |
|
896 | - $properties = isset( $schema['properties'] ) ? $schema['properties'] : array(); |
|
897 | - |
|
898 | - $additional_fields = $this->get_additional_fields(); |
|
899 | - foreach ( $additional_fields as $field_name => $field_options ) { |
|
900 | - // For back-compat, include any field with an empty schema |
|
901 | - // because it won't be present in $this->get_item_schema(). |
|
902 | - if ( is_null( $field_options['schema'] ) ) { |
|
903 | - $properties[ $field_name ] = $field_options; |
|
904 | - } |
|
905 | - } |
|
906 | - |
|
907 | - // Exclude fields that specify a different context than the request context. |
|
908 | - $context = $request['context']; |
|
909 | - if ( $context ) { |
|
910 | - foreach ( $properties as $name => $options ) { |
|
911 | - if ( ! empty( $options['context'] ) && ! in_array( $context, $options['context'], true ) ) { |
|
912 | - unset( $properties[ $name ] ); |
|
913 | - } |
|
914 | - } |
|
915 | - } |
|
916 | - |
|
917 | - $fields = array_keys( $properties ); |
|
918 | - |
|
919 | - if ( ! isset( $request['_fields'] ) ) { |
|
920 | - return $fields; |
|
921 | - } |
|
922 | - $requested_fields = wpinv_parse_list( $request['_fields'] ); |
|
923 | - if ( 0 === count( $requested_fields ) ) { |
|
924 | - return $fields; |
|
925 | - } |
|
926 | - // Trim off outside whitespace from the comma delimited list. |
|
927 | - $requested_fields = array_map( 'trim', $requested_fields ); |
|
928 | - // Always persist 'id', because it can be needed for add_additional_fields_to_object(). |
|
929 | - if ( in_array( 'id', $fields, true ) ) { |
|
930 | - $requested_fields[] = 'id'; |
|
931 | - } |
|
932 | - // Return the list of all requested fields which appear in the schema. |
|
933 | - return array_reduce( |
|
934 | - $requested_fields, |
|
935 | - function( $response_fields, $field ) use ( $fields ) { |
|
936 | - if ( in_array( $field, $fields, true ) ) { |
|
937 | - $response_fields[] = $field; |
|
938 | - return $response_fields; |
|
939 | - } |
|
940 | - // Check for nested fields if $field is not a direct match. |
|
941 | - $nested_fields = explode( '.', $field ); |
|
942 | - // A nested field is included so long as its top-level property is |
|
943 | - // present in the schema. |
|
944 | - if ( in_array( $nested_fields[0], $fields, true ) ) { |
|
945 | - $response_fields[] = $field; |
|
946 | - } |
|
947 | - return $response_fields; |
|
948 | - }, |
|
949 | - array() |
|
950 | - ); |
|
951 | - } |
|
952 | - |
|
953 | - /** |
|
954 | - * Retrieves the item's schema, conforming to JSON Schema. |
|
955 | - * |
|
956 | - * @since 1.0.13 |
|
957 | - * |
|
958 | - * @return array Item schema data. |
|
959 | - */ |
|
960 | - public function get_item_schema() { |
|
961 | - |
|
962 | - // Maybe retrieve the schema from cache. |
|
963 | - if ( $this->schema ) { |
|
964 | - return $this->add_additional_fields_schema( $this->schema ); |
|
965 | - } |
|
966 | - |
|
967 | - $schema = array( |
|
968 | - '$schema' => 'http://json-schema.org/draft-04/schema#', |
|
969 | - 'title' => $this->post_type, |
|
970 | - 'type' => 'object', |
|
971 | - |
|
972 | - // Base properties for every Item. |
|
973 | - 'properties' => array( |
|
974 | - |
|
975 | - 'id' => array( |
|
976 | - 'description' => __( 'Unique identifier for the item.', 'invoicing' ), |
|
977 | - 'type' => 'integer', |
|
978 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
979 | - 'readonly' => true, |
|
980 | - ), |
|
981 | - |
|
982 | - 'name' => array( |
|
983 | - 'description' => __( 'The name for the item.', 'invoicing' ), |
|
984 | - 'type' => 'string', |
|
985 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
986 | - ), |
|
987 | - |
|
988 | - 'summary' => array( |
|
989 | - 'description' => __( 'A summary for the item.', 'invoicing' ), |
|
990 | - 'type' => 'string', |
|
991 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
992 | - ), |
|
993 | - |
|
994 | - 'price' => array( |
|
995 | - 'description' => __( 'The price for the item.', 'invoicing' ), |
|
996 | - 'type' => 'number', |
|
997 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
998 | - ), |
|
999 | - |
|
1000 | - 'status' => array( |
|
1001 | - 'description' => __( 'A named status for the item.', 'invoicing' ), |
|
1002 | - 'type' => 'string', |
|
1003 | - 'enum' => array_keys( get_post_stati( array( 'internal' => false ) ) ), |
|
1004 | - 'context' => array( 'view', 'edit' ), |
|
1005 | - ), |
|
1006 | - |
|
1007 | - 'type' => array( |
|
1008 | - 'description' => __( 'The item type.', 'invoicing' ), |
|
1009 | - 'type' => 'string', |
|
1010 | - 'enum' => wpinv_item_types(), |
|
1011 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1012 | - ), |
|
1013 | - |
|
1014 | - 'vat_rule' => array( |
|
1015 | - 'description' => __( 'VAT rule applied to the item.', 'invoicing' ), |
|
1016 | - 'type' => 'string', |
|
1017 | - 'enum' => array( 'digital', 'physical' ), |
|
1018 | - 'context' => array( 'view', 'edit' ), |
|
1019 | - ), |
|
1020 | - |
|
1021 | - 'vat_class' => array( |
|
1022 | - 'description' => __( 'VAT class for the item.', 'invoicing' ), |
|
1023 | - 'type' => 'string', |
|
1024 | - 'context' => array( 'view', 'edit' ), |
|
1025 | - 'readonly' => true, |
|
1026 | - ), |
|
1027 | - |
|
1028 | - 'custom_id' => array( |
|
1029 | - 'description' => __( 'Custom id for the item.', 'invoicing' ), |
|
1030 | - 'type' => 'string', |
|
1031 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1032 | - ), |
|
846 | + } |
|
847 | + |
|
848 | + } |
|
849 | + |
|
850 | + } |
|
851 | + |
|
852 | + $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
853 | + $data = $this->add_additional_fields_to_object( $data, $request ); |
|
854 | + $data = $this->filter_response_by_context( $data, $context ); |
|
855 | + |
|
856 | + // Wrap the data in a response object. |
|
857 | + $response = rest_ensure_response( $data ); |
|
858 | + |
|
859 | + $links = $this->prepare_links( $item ); |
|
860 | + $response->add_links( $links ); |
|
861 | + |
|
862 | + if ( ! empty( $links['self']['href'] ) ) { |
|
863 | + $actions = $this->get_available_actions( $item, $request ); |
|
864 | + |
|
865 | + $self = $links['self']['href']; |
|
866 | + |
|
867 | + foreach ( $actions as $rel ) { |
|
868 | + $response->add_link( $rel, $self ); |
|
869 | + } |
|
870 | + } |
|
871 | + |
|
872 | + /** |
|
873 | + * Filters the item data for a response. |
|
874 | + * |
|
875 | + * @since 1.0.13 |
|
876 | + * |
|
877 | + * @param WP_REST_Response $response The response object. |
|
878 | + * @param WPInv_Item $item The item object. |
|
879 | + * @param WP_REST_Request $request Request object. |
|
880 | + */ |
|
881 | + return apply_filters( "wpinv_rest_prepare_item", $response, $item, $request ); |
|
882 | + } |
|
883 | + |
|
884 | + /** |
|
885 | + * Gets an array of fields to be included on the response. |
|
886 | + * |
|
887 | + * Included fields are based on item schema and `_fields=` request argument. |
|
888 | + * |
|
889 | + * @since 1.0.13 |
|
890 | + * |
|
891 | + * @param WP_REST_Request $request Full details about the request. |
|
892 | + * @return array Fields to be included in the response. |
|
893 | + */ |
|
894 | + public function get_fields_for_response( $request ) { |
|
895 | + $schema = $this->get_item_schema(); |
|
896 | + $properties = isset( $schema['properties'] ) ? $schema['properties'] : array(); |
|
897 | + |
|
898 | + $additional_fields = $this->get_additional_fields(); |
|
899 | + foreach ( $additional_fields as $field_name => $field_options ) { |
|
900 | + // For back-compat, include any field with an empty schema |
|
901 | + // because it won't be present in $this->get_item_schema(). |
|
902 | + if ( is_null( $field_options['schema'] ) ) { |
|
903 | + $properties[ $field_name ] = $field_options; |
|
904 | + } |
|
905 | + } |
|
906 | + |
|
907 | + // Exclude fields that specify a different context than the request context. |
|
908 | + $context = $request['context']; |
|
909 | + if ( $context ) { |
|
910 | + foreach ( $properties as $name => $options ) { |
|
911 | + if ( ! empty( $options['context'] ) && ! in_array( $context, $options['context'], true ) ) { |
|
912 | + unset( $properties[ $name ] ); |
|
913 | + } |
|
914 | + } |
|
915 | + } |
|
916 | + |
|
917 | + $fields = array_keys( $properties ); |
|
918 | + |
|
919 | + if ( ! isset( $request['_fields'] ) ) { |
|
920 | + return $fields; |
|
921 | + } |
|
922 | + $requested_fields = wpinv_parse_list( $request['_fields'] ); |
|
923 | + if ( 0 === count( $requested_fields ) ) { |
|
924 | + return $fields; |
|
925 | + } |
|
926 | + // Trim off outside whitespace from the comma delimited list. |
|
927 | + $requested_fields = array_map( 'trim', $requested_fields ); |
|
928 | + // Always persist 'id', because it can be needed for add_additional_fields_to_object(). |
|
929 | + if ( in_array( 'id', $fields, true ) ) { |
|
930 | + $requested_fields[] = 'id'; |
|
931 | + } |
|
932 | + // Return the list of all requested fields which appear in the schema. |
|
933 | + return array_reduce( |
|
934 | + $requested_fields, |
|
935 | + function( $response_fields, $field ) use ( $fields ) { |
|
936 | + if ( in_array( $field, $fields, true ) ) { |
|
937 | + $response_fields[] = $field; |
|
938 | + return $response_fields; |
|
939 | + } |
|
940 | + // Check for nested fields if $field is not a direct match. |
|
941 | + $nested_fields = explode( '.', $field ); |
|
942 | + // A nested field is included so long as its top-level property is |
|
943 | + // present in the schema. |
|
944 | + if ( in_array( $nested_fields[0], $fields, true ) ) { |
|
945 | + $response_fields[] = $field; |
|
946 | + } |
|
947 | + return $response_fields; |
|
948 | + }, |
|
949 | + array() |
|
950 | + ); |
|
951 | + } |
|
952 | + |
|
953 | + /** |
|
954 | + * Retrieves the item's schema, conforming to JSON Schema. |
|
955 | + * |
|
956 | + * @since 1.0.13 |
|
957 | + * |
|
958 | + * @return array Item schema data. |
|
959 | + */ |
|
960 | + public function get_item_schema() { |
|
961 | + |
|
962 | + // Maybe retrieve the schema from cache. |
|
963 | + if ( $this->schema ) { |
|
964 | + return $this->add_additional_fields_schema( $this->schema ); |
|
965 | + } |
|
966 | + |
|
967 | + $schema = array( |
|
968 | + '$schema' => 'http://json-schema.org/draft-04/schema#', |
|
969 | + 'title' => $this->post_type, |
|
970 | + 'type' => 'object', |
|
971 | + |
|
972 | + // Base properties for every Item. |
|
973 | + 'properties' => array( |
|
974 | + |
|
975 | + 'id' => array( |
|
976 | + 'description' => __( 'Unique identifier for the item.', 'invoicing' ), |
|
977 | + 'type' => 'integer', |
|
978 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
979 | + 'readonly' => true, |
|
980 | + ), |
|
981 | + |
|
982 | + 'name' => array( |
|
983 | + 'description' => __( 'The name for the item.', 'invoicing' ), |
|
984 | + 'type' => 'string', |
|
985 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
986 | + ), |
|
987 | + |
|
988 | + 'summary' => array( |
|
989 | + 'description' => __( 'A summary for the item.', 'invoicing' ), |
|
990 | + 'type' => 'string', |
|
991 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
992 | + ), |
|
993 | + |
|
994 | + 'price' => array( |
|
995 | + 'description' => __( 'The price for the item.', 'invoicing' ), |
|
996 | + 'type' => 'number', |
|
997 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
998 | + ), |
|
999 | + |
|
1000 | + 'status' => array( |
|
1001 | + 'description' => __( 'A named status for the item.', 'invoicing' ), |
|
1002 | + 'type' => 'string', |
|
1003 | + 'enum' => array_keys( get_post_stati( array( 'internal' => false ) ) ), |
|
1004 | + 'context' => array( 'view', 'edit' ), |
|
1005 | + ), |
|
1006 | + |
|
1007 | + 'type' => array( |
|
1008 | + 'description' => __( 'The item type.', 'invoicing' ), |
|
1009 | + 'type' => 'string', |
|
1010 | + 'enum' => wpinv_item_types(), |
|
1011 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1012 | + ), |
|
1013 | + |
|
1014 | + 'vat_rule' => array( |
|
1015 | + 'description' => __( 'VAT rule applied to the item.', 'invoicing' ), |
|
1016 | + 'type' => 'string', |
|
1017 | + 'enum' => array( 'digital', 'physical' ), |
|
1018 | + 'context' => array( 'view', 'edit' ), |
|
1019 | + ), |
|
1020 | + |
|
1021 | + 'vat_class' => array( |
|
1022 | + 'description' => __( 'VAT class for the item.', 'invoicing' ), |
|
1023 | + 'type' => 'string', |
|
1024 | + 'context' => array( 'view', 'edit' ), |
|
1025 | + 'readonly' => true, |
|
1026 | + ), |
|
1027 | + |
|
1028 | + 'custom_id' => array( |
|
1029 | + 'description' => __( 'Custom id for the item.', 'invoicing' ), |
|
1030 | + 'type' => 'string', |
|
1031 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1032 | + ), |
|
1033 | 1033 | |
1034 | - 'custom_name' => array( |
|
1035 | - 'description' => __( 'Custom name for the item.', 'invoicing' ), |
|
1036 | - 'type' => 'string', |
|
1037 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1038 | - ), |
|
1039 | - |
|
1040 | - 'custom_singular_name' => array( |
|
1041 | - 'description' => __( 'Custom singular name for the item.', 'invoicing' ), |
|
1042 | - 'type' => 'string', |
|
1043 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1044 | - ), |
|
1045 | - |
|
1046 | - 'dynamic_pricing' => array( |
|
1047 | - 'description' => __( 'Whether the item allows a user to set their own price.', 'invoicing' ), |
|
1048 | - 'type' => 'integer', |
|
1049 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1050 | - ), |
|
1051 | - |
|
1052 | - 'minimum_price' => array( |
|
1053 | - 'description' => __( 'For dynamic prices, this is the minimum price that a user can set.', 'invoicing' ), |
|
1054 | - 'type' => 'number', |
|
1055 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1056 | - ), |
|
1057 | - |
|
1058 | - 'is_recurring' => array( |
|
1059 | - 'description' => __( 'Whether the item is a subscription item.', 'invoicing' ), |
|
1060 | - 'type' => 'integer', |
|
1061 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1062 | - ), |
|
1063 | - |
|
1064 | - 'recurring_period' => array( |
|
1065 | - 'description' => __( 'The recurring period for a recurring item.', 'invoicing' ), |
|
1066 | - 'type' => 'string', |
|
1067 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1068 | - 'enum' => array( 'D', 'W', 'M', 'Y' ), |
|
1069 | - ), |
|
1070 | - |
|
1071 | - 'recurring_interval' => array( |
|
1072 | - 'description' => __( 'The recurring interval for a subscription item.', 'invoicing' ), |
|
1073 | - 'type' => 'integer', |
|
1074 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1075 | - ), |
|
1076 | - |
|
1077 | - 'recurring_limit' => array( |
|
1078 | - 'description' => __( 'The maximum number of renewals for a subscription item.', 'invoicing' ), |
|
1079 | - 'type' => 'integer', |
|
1080 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1081 | - ), |
|
1082 | - |
|
1083 | - 'free_trial' => array( |
|
1084 | - 'description' => __( 'Whether the item has a free trial period.', 'invoicing' ), |
|
1085 | - 'type' => 'integer', |
|
1086 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1087 | - ), |
|
1088 | - |
|
1089 | - 'trial_period' => array( |
|
1090 | - 'description' => __( 'The trial period of a recurring item.', 'invoicing' ), |
|
1091 | - 'type' => 'string', |
|
1092 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1093 | - 'enum' => array( 'D', 'W', 'M', 'Y' ), |
|
1094 | - ), |
|
1095 | - |
|
1096 | - 'trial_interval' => array( |
|
1097 | - 'description' => __( 'The trial interval for a subscription item.', 'invoicing' ), |
|
1098 | - 'type' => 'integer', |
|
1099 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1100 | - ), |
|
1101 | - |
|
1102 | - 'editable' => array( |
|
1103 | - 'description' => __( 'Whether or not the item is editable.', 'invoicing' ), |
|
1104 | - 'type' => 'integer', |
|
1105 | - 'context' => array( 'view', 'edit' ), |
|
1106 | - ), |
|
1107 | - |
|
1108 | - ), |
|
1109 | - ); |
|
1110 | - |
|
1111 | - // Add helpful links to the item schem. |
|
1112 | - $schema['links'] = $this->get_schema_links(); |
|
1113 | - |
|
1114 | - /** |
|
1115 | - * Filters the item schema for the REST API. |
|
1116 | - * |
|
1117 | - * Enables adding extra properties to items. |
|
1118 | - * |
|
1119 | - * @since 1.0.13 |
|
1120 | - * |
|
1121 | - * @param array $schema The item schema. |
|
1122 | - */ |
|
1034 | + 'custom_name' => array( |
|
1035 | + 'description' => __( 'Custom name for the item.', 'invoicing' ), |
|
1036 | + 'type' => 'string', |
|
1037 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1038 | + ), |
|
1039 | + |
|
1040 | + 'custom_singular_name' => array( |
|
1041 | + 'description' => __( 'Custom singular name for the item.', 'invoicing' ), |
|
1042 | + 'type' => 'string', |
|
1043 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1044 | + ), |
|
1045 | + |
|
1046 | + 'dynamic_pricing' => array( |
|
1047 | + 'description' => __( 'Whether the item allows a user to set their own price.', 'invoicing' ), |
|
1048 | + 'type' => 'integer', |
|
1049 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1050 | + ), |
|
1051 | + |
|
1052 | + 'minimum_price' => array( |
|
1053 | + 'description' => __( 'For dynamic prices, this is the minimum price that a user can set.', 'invoicing' ), |
|
1054 | + 'type' => 'number', |
|
1055 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1056 | + ), |
|
1057 | + |
|
1058 | + 'is_recurring' => array( |
|
1059 | + 'description' => __( 'Whether the item is a subscription item.', 'invoicing' ), |
|
1060 | + 'type' => 'integer', |
|
1061 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1062 | + ), |
|
1063 | + |
|
1064 | + 'recurring_period' => array( |
|
1065 | + 'description' => __( 'The recurring period for a recurring item.', 'invoicing' ), |
|
1066 | + 'type' => 'string', |
|
1067 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1068 | + 'enum' => array( 'D', 'W', 'M', 'Y' ), |
|
1069 | + ), |
|
1070 | + |
|
1071 | + 'recurring_interval' => array( |
|
1072 | + 'description' => __( 'The recurring interval for a subscription item.', 'invoicing' ), |
|
1073 | + 'type' => 'integer', |
|
1074 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1075 | + ), |
|
1076 | + |
|
1077 | + 'recurring_limit' => array( |
|
1078 | + 'description' => __( 'The maximum number of renewals for a subscription item.', 'invoicing' ), |
|
1079 | + 'type' => 'integer', |
|
1080 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1081 | + ), |
|
1082 | + |
|
1083 | + 'free_trial' => array( |
|
1084 | + 'description' => __( 'Whether the item has a free trial period.', 'invoicing' ), |
|
1085 | + 'type' => 'integer', |
|
1086 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1087 | + ), |
|
1088 | + |
|
1089 | + 'trial_period' => array( |
|
1090 | + 'description' => __( 'The trial period of a recurring item.', 'invoicing' ), |
|
1091 | + 'type' => 'string', |
|
1092 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1093 | + 'enum' => array( 'D', 'W', 'M', 'Y' ), |
|
1094 | + ), |
|
1095 | + |
|
1096 | + 'trial_interval' => array( |
|
1097 | + 'description' => __( 'The trial interval for a subscription item.', 'invoicing' ), |
|
1098 | + 'type' => 'integer', |
|
1099 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1100 | + ), |
|
1101 | + |
|
1102 | + 'editable' => array( |
|
1103 | + 'description' => __( 'Whether or not the item is editable.', 'invoicing' ), |
|
1104 | + 'type' => 'integer', |
|
1105 | + 'context' => array( 'view', 'edit' ), |
|
1106 | + ), |
|
1107 | + |
|
1108 | + ), |
|
1109 | + ); |
|
1110 | + |
|
1111 | + // Add helpful links to the item schem. |
|
1112 | + $schema['links'] = $this->get_schema_links(); |
|
1113 | + |
|
1114 | + /** |
|
1115 | + * Filters the item schema for the REST API. |
|
1116 | + * |
|
1117 | + * Enables adding extra properties to items. |
|
1118 | + * |
|
1119 | + * @since 1.0.13 |
|
1120 | + * |
|
1121 | + * @param array $schema The item schema. |
|
1122 | + */ |
|
1123 | 1123 | $schema = apply_filters( "wpinv_rest_item_schema", $schema ); |
1124 | 1124 | |
1125 | - // Cache the item schema. |
|
1126 | - $this->schema = $schema; |
|
1125 | + // Cache the item schema. |
|
1126 | + $this->schema = $schema; |
|
1127 | 1127 | |
1128 | - return $this->add_additional_fields_schema( $this->schema ); |
|
1129 | - } |
|
1130 | - |
|
1131 | - /** |
|
1132 | - * Retrieve Link Description Objects that should be added to the Schema for the invoices collection. |
|
1133 | - * |
|
1134 | - * @since 1.0.13 |
|
1135 | - * |
|
1136 | - * @return array |
|
1137 | - */ |
|
1138 | - protected function get_schema_links() { |
|
1139 | - |
|
1140 | - $href = rest_url( "{$this->namespace}/{$this->rest_base}/{id}" ); |
|
1141 | - |
|
1142 | - $links = array(); |
|
1143 | - |
|
1144 | - $links[] = array( |
|
1145 | - 'rel' => 'https://api.w.org/action-publish', |
|
1146 | - 'title' => __( 'The current user can publish this item.' ), |
|
1147 | - 'href' => $href, |
|
1148 | - 'targetSchema' => array( |
|
1149 | - 'type' => 'object', |
|
1150 | - 'properties' => array( |
|
1151 | - 'status' => array( |
|
1152 | - 'type' => 'string', |
|
1153 | - 'enum' => array( 'publish', 'future' ), |
|
1154 | - ), |
|
1155 | - ), |
|
1156 | - ), |
|
1157 | - ); |
|
1158 | - |
|
1159 | - return $links; |
|
1160 | - } |
|
1161 | - |
|
1162 | - /** |
|
1163 | - * Prepares links for the request. |
|
1164 | - * |
|
1165 | - * @since 1.0.13 |
|
1166 | - * |
|
1167 | - * @param WPInv_Item $item Item Object. |
|
1168 | - * @return array Links for the given item. |
|
1169 | - */ |
|
1170 | - protected function prepare_links( $item ) { |
|
1171 | - |
|
1172 | - // Prepare the base REST API endpoint for items. |
|
1173 | - $base = sprintf( '%s/%s', $this->namespace, $this->rest_base ); |
|
1174 | - |
|
1175 | - // Entity meta. |
|
1176 | - $links = array( |
|
1177 | - 'self' => array( |
|
1178 | - 'href' => rest_url( trailingslashit( $base ) . $item->ID ), |
|
1179 | - ), |
|
1180 | - 'collection' => array( |
|
1181 | - 'href' => rest_url( $base ), |
|
1182 | - ), |
|
1183 | - ); |
|
1184 | - |
|
1185 | - /** |
|
1186 | - * Filters the returned item links for the REST API. |
|
1187 | - * |
|
1188 | - * Enables adding extra links to item API responses. |
|
1189 | - * |
|
1190 | - * @since 1.0.13 |
|
1191 | - * |
|
1192 | - * @param array $links Rest links. |
|
1193 | - */ |
|
1194 | - return apply_filters( "wpinv_rest_item_links", $links ); |
|
1195 | - |
|
1196 | - } |
|
1197 | - |
|
1198 | - /** |
|
1199 | - * Get the link relations available for the post and current user. |
|
1200 | - * |
|
1201 | - * @since 1.0.13 |
|
1202 | - * |
|
1203 | - * @param WPInv_Item $item Item object. |
|
1204 | - * @param WP_REST_Request $request Request object. |
|
1205 | - * @return array List of link relations. |
|
1206 | - */ |
|
1207 | - protected function get_available_actions( $item, $request ) { |
|
1208 | - |
|
1209 | - if ( 'edit' !== $request['context'] ) { |
|
1210 | - return array(); |
|
1211 | - } |
|
1212 | - |
|
1213 | - $rels = array(); |
|
1214 | - |
|
1215 | - // Retrieve the post type object. |
|
1216 | - $post_type = get_post_type_object( $item->post_type ); |
|
1217 | - |
|
1218 | - // Mark item as published. |
|
1219 | - if ( current_user_can( $post_type->cap->publish_posts ) ) { |
|
1220 | - $rels[] = 'https://api.w.org/action-publish'; |
|
1221 | - } |
|
1222 | - |
|
1223 | - /** |
|
1224 | - * Filters the available item link relations for the REST API. |
|
1225 | - * |
|
1226 | - * Enables adding extra link relation for the current user and request to item responses. |
|
1227 | - * |
|
1228 | - * @since 1.0.13 |
|
1229 | - * |
|
1230 | - * @param array $rels Available link relations. |
|
1231 | - */ |
|
1232 | - return apply_filters( "wpinv_rest_item_link_relations", $rels ); |
|
1233 | - } |
|
1234 | - |
|
1235 | - /** |
|
1236 | - * Handles rest requests for item types. |
|
1237 | - * |
|
1238 | - * @since 1.0.13 |
|
1239 | - * |
|
1240 | - * |
|
1241 | - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
1242 | - */ |
|
1243 | - public function get_item_types() { |
|
1244 | - return rest_ensure_response( wpinv_get_item_types() ); |
|
1245 | - } |
|
1128 | + return $this->add_additional_fields_schema( $this->schema ); |
|
1129 | + } |
|
1130 | + |
|
1131 | + /** |
|
1132 | + * Retrieve Link Description Objects that should be added to the Schema for the invoices collection. |
|
1133 | + * |
|
1134 | + * @since 1.0.13 |
|
1135 | + * |
|
1136 | + * @return array |
|
1137 | + */ |
|
1138 | + protected function get_schema_links() { |
|
1139 | + |
|
1140 | + $href = rest_url( "{$this->namespace}/{$this->rest_base}/{id}" ); |
|
1141 | + |
|
1142 | + $links = array(); |
|
1143 | + |
|
1144 | + $links[] = array( |
|
1145 | + 'rel' => 'https://api.w.org/action-publish', |
|
1146 | + 'title' => __( 'The current user can publish this item.' ), |
|
1147 | + 'href' => $href, |
|
1148 | + 'targetSchema' => array( |
|
1149 | + 'type' => 'object', |
|
1150 | + 'properties' => array( |
|
1151 | + 'status' => array( |
|
1152 | + 'type' => 'string', |
|
1153 | + 'enum' => array( 'publish', 'future' ), |
|
1154 | + ), |
|
1155 | + ), |
|
1156 | + ), |
|
1157 | + ); |
|
1158 | + |
|
1159 | + return $links; |
|
1160 | + } |
|
1161 | + |
|
1162 | + /** |
|
1163 | + * Prepares links for the request. |
|
1164 | + * |
|
1165 | + * @since 1.0.13 |
|
1166 | + * |
|
1167 | + * @param WPInv_Item $item Item Object. |
|
1168 | + * @return array Links for the given item. |
|
1169 | + */ |
|
1170 | + protected function prepare_links( $item ) { |
|
1171 | + |
|
1172 | + // Prepare the base REST API endpoint for items. |
|
1173 | + $base = sprintf( '%s/%s', $this->namespace, $this->rest_base ); |
|
1174 | + |
|
1175 | + // Entity meta. |
|
1176 | + $links = array( |
|
1177 | + 'self' => array( |
|
1178 | + 'href' => rest_url( trailingslashit( $base ) . $item->ID ), |
|
1179 | + ), |
|
1180 | + 'collection' => array( |
|
1181 | + 'href' => rest_url( $base ), |
|
1182 | + ), |
|
1183 | + ); |
|
1184 | + |
|
1185 | + /** |
|
1186 | + * Filters the returned item links for the REST API. |
|
1187 | + * |
|
1188 | + * Enables adding extra links to item API responses. |
|
1189 | + * |
|
1190 | + * @since 1.0.13 |
|
1191 | + * |
|
1192 | + * @param array $links Rest links. |
|
1193 | + */ |
|
1194 | + return apply_filters( "wpinv_rest_item_links", $links ); |
|
1195 | + |
|
1196 | + } |
|
1197 | + |
|
1198 | + /** |
|
1199 | + * Get the link relations available for the post and current user. |
|
1200 | + * |
|
1201 | + * @since 1.0.13 |
|
1202 | + * |
|
1203 | + * @param WPInv_Item $item Item object. |
|
1204 | + * @param WP_REST_Request $request Request object. |
|
1205 | + * @return array List of link relations. |
|
1206 | + */ |
|
1207 | + protected function get_available_actions( $item, $request ) { |
|
1208 | + |
|
1209 | + if ( 'edit' !== $request['context'] ) { |
|
1210 | + return array(); |
|
1211 | + } |
|
1212 | + |
|
1213 | + $rels = array(); |
|
1214 | + |
|
1215 | + // Retrieve the post type object. |
|
1216 | + $post_type = get_post_type_object( $item->post_type ); |
|
1217 | + |
|
1218 | + // Mark item as published. |
|
1219 | + if ( current_user_can( $post_type->cap->publish_posts ) ) { |
|
1220 | + $rels[] = 'https://api.w.org/action-publish'; |
|
1221 | + } |
|
1222 | + |
|
1223 | + /** |
|
1224 | + * Filters the available item link relations for the REST API. |
|
1225 | + * |
|
1226 | + * Enables adding extra link relation for the current user and request to item responses. |
|
1227 | + * |
|
1228 | + * @since 1.0.13 |
|
1229 | + * |
|
1230 | + * @param array $rels Available link relations. |
|
1231 | + */ |
|
1232 | + return apply_filters( "wpinv_rest_item_link_relations", $rels ); |
|
1233 | + } |
|
1234 | + |
|
1235 | + /** |
|
1236 | + * Handles rest requests for item types. |
|
1237 | + * |
|
1238 | + * @since 1.0.13 |
|
1239 | + * |
|
1240 | + * |
|
1241 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
1242 | + */ |
|
1243 | + public function get_item_types() { |
|
1244 | + return rest_ensure_response( wpinv_get_item_types() ); |
|
1245 | + } |
|
1246 | 1246 | |
1247 | 1247 | |
1248 | 1248 | } |
1249 | 1249 | \ No newline at end of file |
@@ -219,262 +219,262 @@ discard block |
||
219 | 219 | add_filter( 'wpinv_paypal_args', 'wpinv_get_paypal_recurring_args', 10, 3 ); |
220 | 220 | |
221 | 221 | function wpinv_process_paypal_ipn() { |
222 | - // Check the request method is POST |
|
223 | - if ( isset( $_SERVER['REQUEST_METHOD'] ) && $_SERVER['REQUEST_METHOD'] != 'POST' ) { |
|
224 | - return; |
|
225 | - } |
|
226 | - |
|
227 | - // Set initial post data to empty string |
|
228 | - $post_data = ''; |
|
229 | - |
|
230 | - // Fallback just in case post_max_size is lower than needed |
|
231 | - if ( ini_get( 'allow_url_fopen' ) ) { |
|
232 | - $post_data = file_get_contents( 'php://input' ); |
|
233 | - } else { |
|
234 | - // If allow_url_fopen is not enabled, then make sure that post_max_size is large enough |
|
235 | - ini_set( 'post_max_size', '12M' ); |
|
236 | - } |
|
237 | - // Start the encoded data collection with notification command |
|
238 | - $encoded_data = 'cmd=_notify-validate'; |
|
239 | - |
|
240 | - // Get current arg separator |
|
241 | - $arg_separator = wpinv_get_php_arg_separator_output(); |
|
242 | - |
|
243 | - // Verify there is a post_data |
|
244 | - if ( $post_data || strlen( $post_data ) > 0 ) { |
|
245 | - // Append the data |
|
246 | - $encoded_data .= $arg_separator.$post_data; |
|
247 | - } else { |
|
248 | - // Check if POST is empty |
|
249 | - if ( empty( $_POST ) ) { |
|
250 | - // Nothing to do |
|
251 | - return; |
|
252 | - } else { |
|
253 | - // Loop through each POST |
|
254 | - foreach ( $_POST as $key => $value ) { |
|
255 | - // Encode the value and append the data |
|
256 | - $encoded_data .= $arg_separator."$key=" . urlencode( $value ); |
|
257 | - } |
|
258 | - } |
|
259 | - } |
|
260 | - |
|
261 | - // Convert collected post data to an array |
|
262 | - wp_parse_str( $encoded_data, $encoded_data_array ); |
|
263 | - |
|
264 | - foreach ( $encoded_data_array as $key => $value ) { |
|
265 | - if ( false !== strpos( $key, 'amp;' ) ) { |
|
266 | - $new_key = str_replace( '&', '&', $key ); |
|
267 | - $new_key = str_replace( 'amp;', '&' , $new_key ); |
|
268 | - |
|
269 | - unset( $encoded_data_array[ $key ] ); |
|
270 | - $encoded_data_array[ $new_key ] = $value; |
|
271 | - } |
|
272 | - } |
|
273 | - |
|
274 | - // Get the PayPal redirect uri |
|
275 | - $paypal_redirect = wpinv_get_paypal_redirect( true ); |
|
276 | - |
|
277 | - if ( !wpinv_get_option( 'disable_paypal_verification', false ) ) { |
|
278 | - // Validate the IPN |
|
279 | - |
|
280 | - $remote_post_vars = array( |
|
281 | - 'method' => 'POST', |
|
282 | - 'timeout' => 45, |
|
283 | - 'redirection' => 5, |
|
284 | - 'httpversion' => '1.1', |
|
285 | - 'blocking' => true, |
|
286 | - 'headers' => array( |
|
287 | - 'host' => 'www.paypal.com', |
|
288 | - 'connection' => 'close', |
|
289 | - 'content-type' => 'application/x-www-form-urlencoded', |
|
290 | - 'post' => '/cgi-bin/webscr HTTP/1.1', |
|
291 | - |
|
292 | - ), |
|
293 | - 'sslverify' => false, |
|
294 | - 'body' => $encoded_data_array |
|
295 | - ); |
|
296 | - |
|
297 | - // Get response |
|
298 | - $api_response = wp_remote_post( wpinv_get_paypal_redirect(), $remote_post_vars ); |
|
299 | - |
|
300 | - if ( is_wp_error( $api_response ) ) { |
|
301 | - wpinv_record_gateway_error( __( 'IPN Error', 'invoicing' ), sprintf( __( 'Invalid IPN verification response. IPN data: %s', 'invoicing' ), json_encode( $api_response ) ) ); |
|
302 | - return; // Something went wrong |
|
303 | - } |
|
304 | - |
|
305 | - if ( $api_response['body'] !== 'VERIFIED' && wpinv_get_option( 'disable_paypal_verification', false ) ) { |
|
306 | - wpinv_record_gateway_error( __( 'IPN Error', 'invoicing' ), sprintf( __( 'Invalid IPN verification response. IPN data: %s', 'invoicing' ), json_encode( $api_response ) ) ); |
|
307 | - return; // Response not okay |
|
308 | - } |
|
309 | - } |
|
310 | - |
|
311 | - // Check if $post_data_array has been populated |
|
312 | - if ( !is_array( $encoded_data_array ) && !empty( $encoded_data_array ) ) |
|
313 | - return; |
|
314 | - |
|
315 | - $defaults = array( |
|
316 | - 'txn_type' => '', |
|
317 | - 'payment_status' => '' |
|
318 | - ); |
|
319 | - |
|
320 | - $encoded_data_array = wp_parse_args( $encoded_data_array, $defaults ); |
|
321 | - |
|
322 | - $invoice_id = isset( $encoded_data_array['custom'] ) ? absint( $encoded_data_array['custom'] ) : 0; |
|
222 | + // Check the request method is POST |
|
223 | + if ( isset( $_SERVER['REQUEST_METHOD'] ) && $_SERVER['REQUEST_METHOD'] != 'POST' ) { |
|
224 | + return; |
|
225 | + } |
|
226 | + |
|
227 | + // Set initial post data to empty string |
|
228 | + $post_data = ''; |
|
229 | + |
|
230 | + // Fallback just in case post_max_size is lower than needed |
|
231 | + if ( ini_get( 'allow_url_fopen' ) ) { |
|
232 | + $post_data = file_get_contents( 'php://input' ); |
|
233 | + } else { |
|
234 | + // If allow_url_fopen is not enabled, then make sure that post_max_size is large enough |
|
235 | + ini_set( 'post_max_size', '12M' ); |
|
236 | + } |
|
237 | + // Start the encoded data collection with notification command |
|
238 | + $encoded_data = 'cmd=_notify-validate'; |
|
239 | + |
|
240 | + // Get current arg separator |
|
241 | + $arg_separator = wpinv_get_php_arg_separator_output(); |
|
242 | + |
|
243 | + // Verify there is a post_data |
|
244 | + if ( $post_data || strlen( $post_data ) > 0 ) { |
|
245 | + // Append the data |
|
246 | + $encoded_data .= $arg_separator.$post_data; |
|
247 | + } else { |
|
248 | + // Check if POST is empty |
|
249 | + if ( empty( $_POST ) ) { |
|
250 | + // Nothing to do |
|
251 | + return; |
|
252 | + } else { |
|
253 | + // Loop through each POST |
|
254 | + foreach ( $_POST as $key => $value ) { |
|
255 | + // Encode the value and append the data |
|
256 | + $encoded_data .= $arg_separator."$key=" . urlencode( $value ); |
|
257 | + } |
|
258 | + } |
|
259 | + } |
|
260 | + |
|
261 | + // Convert collected post data to an array |
|
262 | + wp_parse_str( $encoded_data, $encoded_data_array ); |
|
263 | + |
|
264 | + foreach ( $encoded_data_array as $key => $value ) { |
|
265 | + if ( false !== strpos( $key, 'amp;' ) ) { |
|
266 | + $new_key = str_replace( '&', '&', $key ); |
|
267 | + $new_key = str_replace( 'amp;', '&' , $new_key ); |
|
268 | + |
|
269 | + unset( $encoded_data_array[ $key ] ); |
|
270 | + $encoded_data_array[ $new_key ] = $value; |
|
271 | + } |
|
272 | + } |
|
273 | + |
|
274 | + // Get the PayPal redirect uri |
|
275 | + $paypal_redirect = wpinv_get_paypal_redirect( true ); |
|
276 | + |
|
277 | + if ( !wpinv_get_option( 'disable_paypal_verification', false ) ) { |
|
278 | + // Validate the IPN |
|
279 | + |
|
280 | + $remote_post_vars = array( |
|
281 | + 'method' => 'POST', |
|
282 | + 'timeout' => 45, |
|
283 | + 'redirection' => 5, |
|
284 | + 'httpversion' => '1.1', |
|
285 | + 'blocking' => true, |
|
286 | + 'headers' => array( |
|
287 | + 'host' => 'www.paypal.com', |
|
288 | + 'connection' => 'close', |
|
289 | + 'content-type' => 'application/x-www-form-urlencoded', |
|
290 | + 'post' => '/cgi-bin/webscr HTTP/1.1', |
|
291 | + |
|
292 | + ), |
|
293 | + 'sslverify' => false, |
|
294 | + 'body' => $encoded_data_array |
|
295 | + ); |
|
296 | + |
|
297 | + // Get response |
|
298 | + $api_response = wp_remote_post( wpinv_get_paypal_redirect(), $remote_post_vars ); |
|
299 | + |
|
300 | + if ( is_wp_error( $api_response ) ) { |
|
301 | + wpinv_record_gateway_error( __( 'IPN Error', 'invoicing' ), sprintf( __( 'Invalid IPN verification response. IPN data: %s', 'invoicing' ), json_encode( $api_response ) ) ); |
|
302 | + return; // Something went wrong |
|
303 | + } |
|
304 | + |
|
305 | + if ( $api_response['body'] !== 'VERIFIED' && wpinv_get_option( 'disable_paypal_verification', false ) ) { |
|
306 | + wpinv_record_gateway_error( __( 'IPN Error', 'invoicing' ), sprintf( __( 'Invalid IPN verification response. IPN data: %s', 'invoicing' ), json_encode( $api_response ) ) ); |
|
307 | + return; // Response not okay |
|
308 | + } |
|
309 | + } |
|
310 | + |
|
311 | + // Check if $post_data_array has been populated |
|
312 | + if ( !is_array( $encoded_data_array ) && !empty( $encoded_data_array ) ) |
|
313 | + return; |
|
314 | + |
|
315 | + $defaults = array( |
|
316 | + 'txn_type' => '', |
|
317 | + 'payment_status' => '' |
|
318 | + ); |
|
319 | + |
|
320 | + $encoded_data_array = wp_parse_args( $encoded_data_array, $defaults ); |
|
321 | + |
|
322 | + $invoice_id = isset( $encoded_data_array['custom'] ) ? absint( $encoded_data_array['custom'] ) : 0; |
|
323 | 323 | |
324 | - wpinv_error_log( $encoded_data_array['txn_type'], 'PayPal txn_type', __FILE__, __LINE__ ); |
|
325 | - wpinv_error_log( $encoded_data_array, 'PayPal IPN response', __FILE__, __LINE__ ); |
|
326 | - |
|
327 | - if ( has_action( 'wpinv_paypal_' . $encoded_data_array['txn_type'] ) ) { |
|
328 | - // Allow PayPal IPN types to be processed separately |
|
329 | - do_action( 'wpinv_paypal_' . $encoded_data_array['txn_type'], $encoded_data_array, $invoice_id ); |
|
330 | - } else { |
|
331 | - // Fallback to web accept just in case the txn_type isn't present |
|
332 | - do_action( 'wpinv_paypal_web_accept', $encoded_data_array, $invoice_id ); |
|
333 | - } |
|
334 | - exit; |
|
324 | + wpinv_error_log( $encoded_data_array['txn_type'], 'PayPal txn_type', __FILE__, __LINE__ ); |
|
325 | + wpinv_error_log( $encoded_data_array, 'PayPal IPN response', __FILE__, __LINE__ ); |
|
326 | + |
|
327 | + if ( has_action( 'wpinv_paypal_' . $encoded_data_array['txn_type'] ) ) { |
|
328 | + // Allow PayPal IPN types to be processed separately |
|
329 | + do_action( 'wpinv_paypal_' . $encoded_data_array['txn_type'], $encoded_data_array, $invoice_id ); |
|
330 | + } else { |
|
331 | + // Fallback to web accept just in case the txn_type isn't present |
|
332 | + do_action( 'wpinv_paypal_web_accept', $encoded_data_array, $invoice_id ); |
|
333 | + } |
|
334 | + exit; |
|
335 | 335 | } |
336 | 336 | add_action( 'wpinv_verify_paypal_ipn', 'wpinv_process_paypal_ipn' ); |
337 | 337 | |
338 | 338 | function wpinv_process_paypal_web_accept_and_cart( $data, $invoice_id ) { |
339 | - if ( $data['txn_type'] != 'web_accept' && $data['txn_type'] != 'cart' && $data['payment_status'] != 'Refunded' ) { |
|
340 | - return; |
|
341 | - } |
|
342 | - |
|
343 | - if( empty( $invoice_id ) ) { |
|
344 | - return; |
|
345 | - } |
|
346 | - |
|
347 | - // Collect payment details |
|
348 | - $purchase_key = isset( $data['invoice'] ) ? $data['invoice'] : $data['item_number']; |
|
349 | - $paypal_amount = $data['mc_gross']; |
|
350 | - $payment_status = strtolower( $data['payment_status'] ); |
|
351 | - $currency_code = strtolower( $data['mc_currency'] ); |
|
352 | - $business_email = isset( $data['business'] ) && is_email( $data['business'] ) ? trim( $data['business'] ) : trim( $data['receiver_email'] ); |
|
353 | - $payment_meta = wpinv_get_invoice_meta( $invoice_id ); |
|
354 | - |
|
355 | - if ( wpinv_get_payment_gateway( $invoice_id ) != 'paypal' ) { |
|
356 | - return; // this isn't a PayPal standard IPN |
|
357 | - } |
|
358 | - |
|
359 | - // Verify payment recipient |
|
360 | - if ( strcasecmp( $business_email, trim( wpinv_get_option( 'paypal_email', false ) ) ) != 0 ) { |
|
361 | - wpinv_record_gateway_error( __( 'IPN Error', 'invoicing' ), sprintf( __( 'Invalid business email in IPN response. IPN data: %s', 'invoicing' ), json_encode( $data ) ), $invoice_id ); |
|
362 | - wpinv_update_payment_status( $invoice_id, 'wpi-failed' ); |
|
363 | - wpinv_insert_payment_note( $invoice_id, __( 'Payment failed due to invalid PayPal business email.', 'invoicing' ), '', '', true ); |
|
364 | - return; |
|
365 | - } |
|
366 | - |
|
367 | - // Verify payment currency |
|
368 | - if ( $currency_code != strtolower( $payment_meta['currency'] ) ) { |
|
369 | - wpinv_record_gateway_error( __( 'IPN Error', 'invoicing' ), sprintf( __( 'Invalid currency in IPN response. IPN data: %s', 'invoicing' ), json_encode( $data ) ), $invoice_id ); |
|
370 | - wpinv_update_payment_status( $invoice_id, 'wpi-failed' ); |
|
371 | - wpinv_insert_payment_note( $invoice_id, __( 'Payment failed due to invalid currency in PayPal IPN.', 'invoicing' ), '', '', true ); |
|
372 | - return; |
|
373 | - } |
|
374 | - |
|
375 | - if ( !wpinv_get_payment_user_email( $invoice_id ) ) { |
|
376 | - // This runs when a Buy Now purchase was made. It bypasses checkout so no personal info is collected until PayPal |
|
377 | - // No email associated with purchase, so store from PayPal |
|
378 | - wpinv_update_invoice_meta( $invoice_id, '_wpinv_email', $data['payer_email'] ); |
|
379 | - |
|
380 | - // Setup and store the customer's details |
|
381 | - $user_info = array( |
|
382 | - 'user_id' => '-1', |
|
383 | - 'email' => sanitize_text_field( $data['payer_email'] ), |
|
384 | - 'first_name' => sanitize_text_field( $data['first_name'] ), |
|
385 | - 'last_name' => sanitize_text_field( $data['last_name'] ), |
|
386 | - 'discount' => '', |
|
387 | - ); |
|
388 | - $user_info['address'] = ! empty( $data['address_street'] ) ? sanitize_text_field( $data['address_street'] ) : false; |
|
389 | - $user_info['city'] = ! empty( $data['address_city'] ) ? sanitize_text_field( $data['address_city'] ) : false; |
|
390 | - $user_info['state'] = ! empty( $data['address_state'] ) ? sanitize_text_field( $data['address_state'] ) : false; |
|
391 | - $user_info['country'] = ! empty( $data['address_country_code'] ) ? sanitize_text_field( $data['address_country_code'] ) : false; |
|
392 | - $user_info['zip'] = ! empty( $data['address_zip'] ) ? sanitize_text_field( $data['address_zip'] ) : false; |
|
393 | - |
|
394 | - $payment_meta['user_info'] = $user_info; |
|
395 | - wpinv_update_invoice_meta( $invoice_id, '_wpinv_payment_meta', $payment_meta ); |
|
396 | - } |
|
397 | - |
|
398 | - if ( $payment_status == 'refunded' || $payment_status == 'reversed' ) { |
|
399 | - // Process a refund |
|
400 | - wpinv_process_paypal_refund( $data, $invoice_id ); |
|
401 | - } else { |
|
402 | - if ( get_post_status( $invoice_id ) == 'publish' ) { |
|
403 | - return; // Only paid payments once |
|
404 | - } |
|
405 | - |
|
406 | - // Retrieve the total purchase amount (before PayPal) |
|
407 | - $payment_amount = wpinv_payment_total( $invoice_id ); |
|
408 | - |
|
409 | - if ( number_format( (float) $paypal_amount, 2 ) < number_format( (float) $payment_amount, 2 ) ) { |
|
410 | - // The prices don't match |
|
411 | - wpinv_record_gateway_error( __( 'IPN Error', 'invoicing' ), sprintf( __( 'Invalid payment amount in IPN response. IPN data: %s', 'invoicing' ), json_encode( $data ) ), $invoice_id ); |
|
412 | - wpinv_update_payment_status( $invoice_id, 'wpi-failed' ); |
|
413 | - wpinv_insert_payment_note( $invoice_id, __( 'Payment failed due to invalid amount in PayPal IPN.', 'invoicing' ), '', '', true ); |
|
414 | - return; |
|
415 | - } |
|
416 | - if ( $purchase_key != wpinv_get_payment_key( $invoice_id ) ) { |
|
417 | - // Purchase keys don't match |
|
418 | - wpinv_record_gateway_error( __( 'IPN Error', 'invoicing' ), sprintf( __( 'Invalid purchase key in IPN response. IPN data: %s', 'invoicing' ), json_encode( $data ) ), $invoice_id ); |
|
419 | - wpinv_update_payment_status( $invoice_id, 'wpi-failed' ); |
|
420 | - wpinv_insert_payment_note( $invoice_id, __( 'Payment failed due to invalid purchase key in PayPal IPN.', 'invoicing' ), '', '', true ); |
|
421 | - return; |
|
422 | - } |
|
423 | - |
|
424 | - if ( 'complete' == $payment_status || 'completed' == $payment_status || 'processed' == $payment_status || wpinv_is_test_mode( 'paypal' ) ) { |
|
425 | - wpinv_insert_payment_note( $invoice_id, sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , $data['txn_id'] ), '', '', true ); |
|
426 | - wpinv_set_payment_transaction_id( $invoice_id, $data['txn_id'] ); |
|
427 | - wpinv_update_payment_status( $invoice_id, 'publish' ); |
|
428 | - } else if ( 'pending' == $payment_status && isset( $data['pending_reason'] ) ) { |
|
429 | - // Look for possible pending reasons, such as an echeck |
|
430 | - $note = ''; |
|
431 | - |
|
432 | - switch( strtolower( $data['pending_reason'] ) ) { |
|
433 | - case 'echeck' : |
|
434 | - $note = __( 'Payment made via eCheck and will clear automatically in 5-8 days', 'invoicing' ); |
|
435 | - break; |
|
339 | + if ( $data['txn_type'] != 'web_accept' && $data['txn_type'] != 'cart' && $data['payment_status'] != 'Refunded' ) { |
|
340 | + return; |
|
341 | + } |
|
342 | + |
|
343 | + if( empty( $invoice_id ) ) { |
|
344 | + return; |
|
345 | + } |
|
346 | + |
|
347 | + // Collect payment details |
|
348 | + $purchase_key = isset( $data['invoice'] ) ? $data['invoice'] : $data['item_number']; |
|
349 | + $paypal_amount = $data['mc_gross']; |
|
350 | + $payment_status = strtolower( $data['payment_status'] ); |
|
351 | + $currency_code = strtolower( $data['mc_currency'] ); |
|
352 | + $business_email = isset( $data['business'] ) && is_email( $data['business'] ) ? trim( $data['business'] ) : trim( $data['receiver_email'] ); |
|
353 | + $payment_meta = wpinv_get_invoice_meta( $invoice_id ); |
|
354 | + |
|
355 | + if ( wpinv_get_payment_gateway( $invoice_id ) != 'paypal' ) { |
|
356 | + return; // this isn't a PayPal standard IPN |
|
357 | + } |
|
358 | + |
|
359 | + // Verify payment recipient |
|
360 | + if ( strcasecmp( $business_email, trim( wpinv_get_option( 'paypal_email', false ) ) ) != 0 ) { |
|
361 | + wpinv_record_gateway_error( __( 'IPN Error', 'invoicing' ), sprintf( __( 'Invalid business email in IPN response. IPN data: %s', 'invoicing' ), json_encode( $data ) ), $invoice_id ); |
|
362 | + wpinv_update_payment_status( $invoice_id, 'wpi-failed' ); |
|
363 | + wpinv_insert_payment_note( $invoice_id, __( 'Payment failed due to invalid PayPal business email.', 'invoicing' ), '', '', true ); |
|
364 | + return; |
|
365 | + } |
|
366 | + |
|
367 | + // Verify payment currency |
|
368 | + if ( $currency_code != strtolower( $payment_meta['currency'] ) ) { |
|
369 | + wpinv_record_gateway_error( __( 'IPN Error', 'invoicing' ), sprintf( __( 'Invalid currency in IPN response. IPN data: %s', 'invoicing' ), json_encode( $data ) ), $invoice_id ); |
|
370 | + wpinv_update_payment_status( $invoice_id, 'wpi-failed' ); |
|
371 | + wpinv_insert_payment_note( $invoice_id, __( 'Payment failed due to invalid currency in PayPal IPN.', 'invoicing' ), '', '', true ); |
|
372 | + return; |
|
373 | + } |
|
374 | + |
|
375 | + if ( !wpinv_get_payment_user_email( $invoice_id ) ) { |
|
376 | + // This runs when a Buy Now purchase was made. It bypasses checkout so no personal info is collected until PayPal |
|
377 | + // No email associated with purchase, so store from PayPal |
|
378 | + wpinv_update_invoice_meta( $invoice_id, '_wpinv_email', $data['payer_email'] ); |
|
379 | + |
|
380 | + // Setup and store the customer's details |
|
381 | + $user_info = array( |
|
382 | + 'user_id' => '-1', |
|
383 | + 'email' => sanitize_text_field( $data['payer_email'] ), |
|
384 | + 'first_name' => sanitize_text_field( $data['first_name'] ), |
|
385 | + 'last_name' => sanitize_text_field( $data['last_name'] ), |
|
386 | + 'discount' => '', |
|
387 | + ); |
|
388 | + $user_info['address'] = ! empty( $data['address_street'] ) ? sanitize_text_field( $data['address_street'] ) : false; |
|
389 | + $user_info['city'] = ! empty( $data['address_city'] ) ? sanitize_text_field( $data['address_city'] ) : false; |
|
390 | + $user_info['state'] = ! empty( $data['address_state'] ) ? sanitize_text_field( $data['address_state'] ) : false; |
|
391 | + $user_info['country'] = ! empty( $data['address_country_code'] ) ? sanitize_text_field( $data['address_country_code'] ) : false; |
|
392 | + $user_info['zip'] = ! empty( $data['address_zip'] ) ? sanitize_text_field( $data['address_zip'] ) : false; |
|
393 | + |
|
394 | + $payment_meta['user_info'] = $user_info; |
|
395 | + wpinv_update_invoice_meta( $invoice_id, '_wpinv_payment_meta', $payment_meta ); |
|
396 | + } |
|
397 | + |
|
398 | + if ( $payment_status == 'refunded' || $payment_status == 'reversed' ) { |
|
399 | + // Process a refund |
|
400 | + wpinv_process_paypal_refund( $data, $invoice_id ); |
|
401 | + } else { |
|
402 | + if ( get_post_status( $invoice_id ) == 'publish' ) { |
|
403 | + return; // Only paid payments once |
|
404 | + } |
|
405 | + |
|
406 | + // Retrieve the total purchase amount (before PayPal) |
|
407 | + $payment_amount = wpinv_payment_total( $invoice_id ); |
|
408 | + |
|
409 | + if ( number_format( (float) $paypal_amount, 2 ) < number_format( (float) $payment_amount, 2 ) ) { |
|
410 | + // The prices don't match |
|
411 | + wpinv_record_gateway_error( __( 'IPN Error', 'invoicing' ), sprintf( __( 'Invalid payment amount in IPN response. IPN data: %s', 'invoicing' ), json_encode( $data ) ), $invoice_id ); |
|
412 | + wpinv_update_payment_status( $invoice_id, 'wpi-failed' ); |
|
413 | + wpinv_insert_payment_note( $invoice_id, __( 'Payment failed due to invalid amount in PayPal IPN.', 'invoicing' ), '', '', true ); |
|
414 | + return; |
|
415 | + } |
|
416 | + if ( $purchase_key != wpinv_get_payment_key( $invoice_id ) ) { |
|
417 | + // Purchase keys don't match |
|
418 | + wpinv_record_gateway_error( __( 'IPN Error', 'invoicing' ), sprintf( __( 'Invalid purchase key in IPN response. IPN data: %s', 'invoicing' ), json_encode( $data ) ), $invoice_id ); |
|
419 | + wpinv_update_payment_status( $invoice_id, 'wpi-failed' ); |
|
420 | + wpinv_insert_payment_note( $invoice_id, __( 'Payment failed due to invalid purchase key in PayPal IPN.', 'invoicing' ), '', '', true ); |
|
421 | + return; |
|
422 | + } |
|
423 | + |
|
424 | + if ( 'complete' == $payment_status || 'completed' == $payment_status || 'processed' == $payment_status || wpinv_is_test_mode( 'paypal' ) ) { |
|
425 | + wpinv_insert_payment_note( $invoice_id, sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , $data['txn_id'] ), '', '', true ); |
|
426 | + wpinv_set_payment_transaction_id( $invoice_id, $data['txn_id'] ); |
|
427 | + wpinv_update_payment_status( $invoice_id, 'publish' ); |
|
428 | + } else if ( 'pending' == $payment_status && isset( $data['pending_reason'] ) ) { |
|
429 | + // Look for possible pending reasons, such as an echeck |
|
430 | + $note = ''; |
|
431 | + |
|
432 | + switch( strtolower( $data['pending_reason'] ) ) { |
|
433 | + case 'echeck' : |
|
434 | + $note = __( 'Payment made via eCheck and will clear automatically in 5-8 days', 'invoicing' ); |
|
435 | + break; |
|
436 | 436 | |
437 | 437 | case 'address' : |
438 | - $note = __( 'Payment requires a confirmed customer address and must be accepted manually through PayPal', 'invoicing' ); |
|
439 | - break; |
|
438 | + $note = __( 'Payment requires a confirmed customer address and must be accepted manually through PayPal', 'invoicing' ); |
|
439 | + break; |
|
440 | 440 | |
441 | 441 | case 'intl' : |
442 | - $note = __( 'Payment must be accepted manually through PayPal due to international account regulations', 'invoicing' ); |
|
443 | - break; |
|
442 | + $note = __( 'Payment must be accepted manually through PayPal due to international account regulations', 'invoicing' ); |
|
443 | + break; |
|
444 | 444 | |
445 | 445 | case 'multi-currency' : |
446 | - $note = __( 'Payment received in non-shop currency and must be accepted manually through PayPal', 'invoicing' ); |
|
447 | - break; |
|
446 | + $note = __( 'Payment received in non-shop currency and must be accepted manually through PayPal', 'invoicing' ); |
|
447 | + break; |
|
448 | 448 | |
449 | 449 | case 'paymentreview' : |
450 | 450 | case 'regulatory_review' : |
451 | - $note = __( 'Payment is being reviewed by PayPal staff as high-risk or in possible violation of government regulations', 'invoicing' ); |
|
452 | - break; |
|
451 | + $note = __( 'Payment is being reviewed by PayPal staff as high-risk or in possible violation of government regulations', 'invoicing' ); |
|
452 | + break; |
|
453 | 453 | |
454 | 454 | case 'unilateral' : |
455 | - $note = __( 'Payment was sent to non-confirmed or non-registered email address.', 'invoicing' ); |
|
456 | - break; |
|
455 | + $note = __( 'Payment was sent to non-confirmed or non-registered email address.', 'invoicing' ); |
|
456 | + break; |
|
457 | 457 | |
458 | 458 | case 'upgrade' : |
459 | - $note = __( 'PayPal account must be upgraded before this payment can be accepted', 'invoicing' ); |
|
460 | - break; |
|
459 | + $note = __( 'PayPal account must be upgraded before this payment can be accepted', 'invoicing' ); |
|
460 | + break; |
|
461 | 461 | |
462 | 462 | case 'verify' : |
463 | - $note = __( 'PayPal account is not verified. Verify account in order to accept this payment', 'invoicing' ); |
|
464 | - break; |
|
465 | - |
|
466 | - case 'other' : |
|
467 | - $note = __( 'Payment is pending for unknown reasons. Contact PayPal support for assistance', 'invoicing' ); |
|
468 | - break; |
|
469 | - } |
|
470 | - |
|
471 | - if ( ! empty( $note ) ) { |
|
472 | - wpinv_insert_payment_note( $invoice_id, $note, '', '', true ); |
|
473 | - } |
|
474 | - } else { |
|
475 | - wpinv_insert_payment_note( $invoice_id, wp_sprintf( __( 'PayPal IPN has been received with invalid payment status: %s', 'invoicing' ), $payment_status ), '', '', true ); |
|
476 | - } |
|
477 | - } |
|
463 | + $note = __( 'PayPal account is not verified. Verify account in order to accept this payment', 'invoicing' ); |
|
464 | + break; |
|
465 | + |
|
466 | + case 'other' : |
|
467 | + $note = __( 'Payment is pending for unknown reasons. Contact PayPal support for assistance', 'invoicing' ); |
|
468 | + break; |
|
469 | + } |
|
470 | + |
|
471 | + if ( ! empty( $note ) ) { |
|
472 | + wpinv_insert_payment_note( $invoice_id, $note, '', '', true ); |
|
473 | + } |
|
474 | + } else { |
|
475 | + wpinv_insert_payment_note( $invoice_id, wp_sprintf( __( 'PayPal IPN has been received with invalid payment status: %s', 'invoicing' ), $payment_status ), '', '', true ); |
|
476 | + } |
|
477 | + } |
|
478 | 478 | } |
479 | 479 | add_action( 'wpinv_paypal_web_accept', 'wpinv_process_paypal_web_accept_and_cart', 10, 2 ); |
480 | 480 | |
@@ -668,34 +668,34 @@ discard block |
||
668 | 668 | } |
669 | 669 | |
670 | 670 | function wpinv_process_paypal_refund( $data, $invoice_id = 0 ) { |
671 | - // Collect payment details |
|
671 | + // Collect payment details |
|
672 | 672 | |
673 | - if( empty( $invoice_id ) ) { |
|
674 | - return; |
|
675 | - } |
|
673 | + if( empty( $invoice_id ) ) { |
|
674 | + return; |
|
675 | + } |
|
676 | 676 | |
677 | - if ( get_post_status( $invoice_id ) == 'wpi-refunded' ) { |
|
678 | - return; // Only refund payments once |
|
679 | - } |
|
677 | + if ( get_post_status( $invoice_id ) == 'wpi-refunded' ) { |
|
678 | + return; // Only refund payments once |
|
679 | + } |
|
680 | 680 | |
681 | - $payment_amount = wpinv_payment_total( $invoice_id ); |
|
682 | - $refund_amount = $data['mc_gross'] * -1; |
|
681 | + $payment_amount = wpinv_payment_total( $invoice_id ); |
|
682 | + $refund_amount = $data['mc_gross'] * -1; |
|
683 | 683 | |
684 | - do_action( 'wpinv_paypal_refund_request', $data, $invoice_id ); |
|
684 | + do_action( 'wpinv_paypal_refund_request', $data, $invoice_id ); |
|
685 | 685 | |
686 | - if ( number_format( (float) $refund_amount, 2 ) < number_format( (float) $payment_amount, 2 ) ) { |
|
687 | - wpinv_insert_payment_note( $invoice_id, wp_sprintf( __( 'PayPal partial refund of %s processed for transaction #%s for reason: %s', 'invoicing' ), (float)$refund_amount . ' '. $data['mc_currency'], $data['parent_txn_id'], $data['reason_code'] ), '', '', true ); |
|
686 | + if ( number_format( (float) $refund_amount, 2 ) < number_format( (float) $payment_amount, 2 ) ) { |
|
687 | + wpinv_insert_payment_note( $invoice_id, wp_sprintf( __( 'PayPal partial refund of %s processed for transaction #%s for reason: %s', 'invoicing' ), (float)$refund_amount . ' '. $data['mc_currency'], $data['parent_txn_id'], $data['reason_code'] ), '', '', true ); |
|
688 | 688 | |
689 | - do_action( 'wpinv_paypal_invoice_partially_refunded', $data, $invoice_id, $refund_amount ); |
|
689 | + do_action( 'wpinv_paypal_invoice_partially_refunded', $data, $invoice_id, $refund_amount ); |
|
690 | 690 | |
691 | - return; // This is a partial refund |
|
692 | - } |
|
691 | + return; // This is a partial refund |
|
692 | + } |
|
693 | 693 | |
694 | - wpinv_insert_payment_note( $invoice_id, sprintf( __( 'PayPal Payment #%s Refunded for reason: %s', 'invoicing' ), $data['parent_txn_id'], $data['reason_code'] ), '', '', true ); |
|
695 | - wpinv_insert_payment_note( $invoice_id, sprintf( __( 'PayPal Refund Transaction ID: %s', 'invoicing' ), $data['txn_id'] ), '', '', true ); |
|
696 | - wpinv_update_payment_status( $invoice_id, 'wpi-refunded' ); |
|
694 | + wpinv_insert_payment_note( $invoice_id, sprintf( __( 'PayPal Payment #%s Refunded for reason: %s', 'invoicing' ), $data['parent_txn_id'], $data['reason_code'] ), '', '', true ); |
|
695 | + wpinv_insert_payment_note( $invoice_id, sprintf( __( 'PayPal Refund Transaction ID: %s', 'invoicing' ), $data['txn_id'] ), '', '', true ); |
|
696 | + wpinv_update_payment_status( $invoice_id, 'wpi-refunded' ); |
|
697 | 697 | |
698 | - do_action( 'wpinv_paypal_invoice_fully_refunded', $data, $invoice_id ); |
|
698 | + do_action( 'wpinv_paypal_invoice_fully_refunded', $data, $invoice_id ); |
|
699 | 699 | } |
700 | 700 | |
701 | 701 | function wpinv_get_paypal_redirect( $ssl_check = false ) { |
@@ -20,91 +20,91 @@ discard block |
||
20 | 20 | class WPInv_REST_Discounts_Controller extends WP_REST_Posts_Controller { |
21 | 21 | |
22 | 22 | /** |
23 | - * Post type. |
|
24 | - * |
|
25 | - * @var string |
|
26 | - */ |
|
27 | - protected $post_type = 'wpi_discount'; |
|
23 | + * Post type. |
|
24 | + * |
|
25 | + * @var string |
|
26 | + */ |
|
27 | + protected $post_type = 'wpi_discount'; |
|
28 | 28 | |
29 | - /** |
|
30 | - * Cached results of get_item_schema. |
|
31 | - * |
|
32 | - * @since 1.0.13 |
|
33 | - * @var array |
|
34 | - */ |
|
35 | - protected $schema; |
|
29 | + /** |
|
30 | + * Cached results of get_item_schema. |
|
31 | + * |
|
32 | + * @since 1.0.13 |
|
33 | + * @var array |
|
34 | + */ |
|
35 | + protected $schema; |
|
36 | 36 | |
37 | 37 | /** |
38 | - * Constructor. |
|
39 | - * |
|
40 | - * @since 1.0.13 |
|
41 | - * |
|
42 | - * @param string $namespace Api Namespace |
|
43 | - */ |
|
44 | - public function __construct( $namespace ) { |
|
38 | + * Constructor. |
|
39 | + * |
|
40 | + * @since 1.0.13 |
|
41 | + * |
|
42 | + * @param string $namespace Api Namespace |
|
43 | + */ |
|
44 | + public function __construct( $namespace ) { |
|
45 | 45 | |
46 | 46 | // Set api namespace... |
47 | - $this->namespace = $namespace; |
|
47 | + $this->namespace = $namespace; |
|
48 | 48 | |
49 | 49 | // ... and the rest base |
50 | 50 | $this->rest_base = 'discounts'; |
51 | 51 | |
52 | 52 | } |
53 | 53 | |
54 | - /** |
|
55 | - * Registers the routes for the objects of the controller. |
|
56 | - * |
|
57 | - * @since 1.0.13 |
|
58 | - * |
|
59 | - * @see register_rest_route() |
|
60 | - */ |
|
61 | - public function register_routes() { |
|
62 | - |
|
63 | - parent::register_routes(); |
|
64 | - |
|
65 | - register_rest_route( |
|
66 | - $this->namespace, |
|
67 | - '/' . $this->rest_base . '/discount-types', |
|
68 | - array( |
|
69 | - array( |
|
70 | - 'methods' => WP_REST_Server::READABLE, |
|
71 | - 'callback' => array( $this, 'get_discount_types' ), |
|
72 | - ), |
|
73 | - ) |
|
74 | - ); |
|
75 | - |
|
76 | - } |
|
54 | + /** |
|
55 | + * Registers the routes for the objects of the controller. |
|
56 | + * |
|
57 | + * @since 1.0.13 |
|
58 | + * |
|
59 | + * @see register_rest_route() |
|
60 | + */ |
|
61 | + public function register_routes() { |
|
62 | + |
|
63 | + parent::register_routes(); |
|
64 | + |
|
65 | + register_rest_route( |
|
66 | + $this->namespace, |
|
67 | + '/' . $this->rest_base . '/discount-types', |
|
68 | + array( |
|
69 | + array( |
|
70 | + 'methods' => WP_REST_Server::READABLE, |
|
71 | + 'callback' => array( $this, 'get_discount_types' ), |
|
72 | + ), |
|
73 | + ) |
|
74 | + ); |
|
75 | + |
|
76 | + } |
|
77 | 77 | |
78 | 78 | /** |
79 | - * Checks if a given request has access to read discounts. |
|
79 | + * Checks if a given request has access to read discounts. |
|
80 | 80 | * |
81 | - * |
|
82 | - * @since 1.0.13 |
|
83 | - * |
|
84 | - * @param WP_REST_Request $request Full details about the request. |
|
85 | - * @return true|WP_Error True if the request has read access, WP_Error object otherwise. |
|
86 | - */ |
|
87 | - public function get_items_permissions_check( $request ) { |
|
81 | + * |
|
82 | + * @since 1.0.13 |
|
83 | + * |
|
84 | + * @param WP_REST_Request $request Full details about the request. |
|
85 | + * @return true|WP_Error True if the request has read access, WP_Error object otherwise. |
|
86 | + */ |
|
87 | + public function get_items_permissions_check( $request ) { |
|
88 | 88 | |
89 | - if ( wpinv_current_user_can_manage_invoicing() ) { |
|
90 | - return true; |
|
91 | - } |
|
89 | + if ( wpinv_current_user_can_manage_invoicing() ) { |
|
90 | + return true; |
|
91 | + } |
|
92 | 92 | |
93 | - return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to view invoice discounts.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
93 | + return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to view invoice discounts.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) ); |
|
94 | 94 | |
95 | 95 | } |
96 | 96 | |
97 | 97 | /** |
98 | - * Retrieves a collection of discounts. |
|
99 | - * |
|
100 | - * @since 1.0.13 |
|
101 | - * |
|
102 | - * @param WP_REST_Request $request Full details about the request. |
|
103 | - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
104 | - */ |
|
105 | - public function get_items( $request ) { |
|
98 | + * Retrieves a collection of discounts. |
|
99 | + * |
|
100 | + * @since 1.0.13 |
|
101 | + * |
|
102 | + * @param WP_REST_Request $request Full details about the request. |
|
103 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
104 | + */ |
|
105 | + public function get_items( $request ) { |
|
106 | 106 | |
107 | - // Retrieve the list of registered item query parameters. |
|
107 | + // Retrieve the list of registered item query parameters. |
|
108 | 108 | $registered = $this->get_collection_params(); |
109 | 109 | |
110 | 110 | $args = array(); |
@@ -115,52 +115,52 @@ discard block |
||
115 | 115 | $args[ $key ] = $request[ $key]; |
116 | 116 | } |
117 | 117 | |
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * Filters the wpinv_get_all_discounts arguments for discounts rest requests. |
|
122 | - * |
|
123 | - * |
|
124 | - * @since 1.0.13 |
|
125 | - * |
|
126 | - * |
|
127 | - * @param array $args Key value array of query var to query value. |
|
128 | - * @param WP_REST_Request $request The request used. |
|
129 | - */ |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * Filters the wpinv_get_all_discounts arguments for discounts rest requests. |
|
122 | + * |
|
123 | + * |
|
124 | + * @since 1.0.13 |
|
125 | + * |
|
126 | + * |
|
127 | + * @param array $args Key value array of query var to query value. |
|
128 | + * @param WP_REST_Request $request The request used. |
|
129 | + */ |
|
130 | 130 | $args = apply_filters( "wpinv_rest_get_discounts_arguments", $args, $request, $this ); |
131 | 131 | |
132 | - // Special args |
|
133 | - $args[ 'return' ] = 'objects'; |
|
134 | - $args[ 'paginate' ] = true; |
|
132 | + // Special args |
|
133 | + $args[ 'return' ] = 'objects'; |
|
134 | + $args[ 'paginate' ] = true; |
|
135 | 135 | |
136 | 136 | // Run the query. |
137 | - $query = wpinv_get_all_discounts( $args ); |
|
137 | + $query = wpinv_get_all_discounts( $args ); |
|
138 | 138 | |
139 | - // Prepare the retrieved discounts |
|
140 | - $discounts = array(); |
|
141 | - foreach( $query->discounts as $discount ) { |
|
142 | - |
|
143 | - $data = $this->prepare_item_for_response( $discount, $request ); |
|
144 | - $discounts[] = $this->prepare_response_for_collection( $data ); |
|
145 | - |
|
146 | - } |
|
147 | - |
|
148 | - // Prepare the response. |
|
149 | - $response = rest_ensure_response( $discounts ); |
|
150 | - $response->header( 'X-WP-Total', (int) $query->total ); |
|
151 | - $response->header( 'X-WP-TotalPages', (int) $query->max_num_pages ); |
|
152 | - |
|
153 | - /** |
|
154 | - * Filters the responses for discount requests. |
|
155 | - * |
|
156 | - * |
|
157 | - * @since 1.0.13 |
|
158 | - * |
|
159 | - * |
|
160 | - * @param arrWP_REST_Response $response Response object. |
|
161 | - * @param WP_REST_Request $request The request used. |
|
139 | + // Prepare the retrieved discounts |
|
140 | + $discounts = array(); |
|
141 | + foreach( $query->discounts as $discount ) { |
|
142 | + |
|
143 | + $data = $this->prepare_item_for_response( $discount, $request ); |
|
144 | + $discounts[] = $this->prepare_response_for_collection( $data ); |
|
145 | + |
|
146 | + } |
|
147 | + |
|
148 | + // Prepare the response. |
|
149 | + $response = rest_ensure_response( $discounts ); |
|
150 | + $response->header( 'X-WP-Total', (int) $query->total ); |
|
151 | + $response->header( 'X-WP-TotalPages', (int) $query->max_num_pages ); |
|
152 | + |
|
153 | + /** |
|
154 | + * Filters the responses for discount requests. |
|
155 | + * |
|
156 | + * |
|
157 | + * @since 1.0.13 |
|
158 | + * |
|
159 | + * |
|
160 | + * @param arrWP_REST_Response $response Response object. |
|
161 | + * @param WP_REST_Request $request The request used. |
|
162 | 162 | * @param array $args Array of args used to retrieve the discounts |
163 | - */ |
|
163 | + */ |
|
164 | 164 | $response = apply_filters( "wpinv_rest_discounts_response", $response, $request, $args ); |
165 | 165 | |
166 | 166 | return rest_ensure_response( $response ); |
@@ -168,25 +168,25 @@ discard block |
||
168 | 168 | } |
169 | 169 | |
170 | 170 | /** |
171 | - * Get the post, if the ID is valid. |
|
172 | - * |
|
173 | - * @since 1.0.13 |
|
174 | - * |
|
175 | - * @param int $discount_id Supplied ID. |
|
176 | - * @return WP_Post|WP_Error Post object if ID is valid, WP_Error otherwise. |
|
177 | - */ |
|
178 | - protected function get_post( $discount_id ) { |
|
171 | + * Get the post, if the ID is valid. |
|
172 | + * |
|
173 | + * @since 1.0.13 |
|
174 | + * |
|
175 | + * @param int $discount_id Supplied ID. |
|
176 | + * @return WP_Post|WP_Error Post object if ID is valid, WP_Error otherwise. |
|
177 | + */ |
|
178 | + protected function get_post( $discount_id ) { |
|
179 | 179 | |
180 | - $error = new WP_Error( 'rest_item_invalid_id', __( 'Invalid discount ID.', 'invoicing' ), array( 'status' => 404 ) ); |
|
180 | + $error = new WP_Error( 'rest_item_invalid_id', __( 'Invalid discount ID.', 'invoicing' ), array( 'status' => 404 ) ); |
|
181 | 181 | |
182 | 182 | // Ids start from 1 |
183 | 183 | if ( (int) $discount_id <= 0 ) { |
184 | - return $error; |
|
185 | - } |
|
184 | + return $error; |
|
185 | + } |
|
186 | 186 | |
187 | - $discount = wpinv_get_discount( (int) $discount_id ); |
|
188 | - if ( empty( $discount ) ) { |
|
189 | - return $error; |
|
187 | + $discount = wpinv_get_discount( (int) $discount_id ); |
|
188 | + if ( empty( $discount ) ) { |
|
189 | + return $error; |
|
190 | 190 | } |
191 | 191 | |
192 | 192 | return $discount; |
@@ -194,25 +194,25 @@ discard block |
||
194 | 194 | } |
195 | 195 | |
196 | 196 | /** |
197 | - * Checks if a given request has access to read a discount. |
|
198 | - * |
|
199 | - * @since 1.0.13 |
|
200 | - * |
|
201 | - * @param WP_REST_Request $request Full details about the request. |
|
202 | - * @return bool|WP_Error True if the request has read access for the invoice item, WP_Error object otherwise. |
|
203 | - */ |
|
204 | - public function get_item_permissions_check( $request ) { |
|
197 | + * Checks if a given request has access to read a discount. |
|
198 | + * |
|
199 | + * @since 1.0.13 |
|
200 | + * |
|
201 | + * @param WP_REST_Request $request Full details about the request. |
|
202 | + * @return bool|WP_Error True if the request has read access for the invoice item, WP_Error object otherwise. |
|
203 | + */ |
|
204 | + public function get_item_permissions_check( $request ) { |
|
205 | 205 | |
206 | 206 | // Retrieve the discount object. |
207 | 207 | $discount = $this->get_post( $request['id'] ); |
208 | 208 | |
209 | 209 | // Ensure it is valid. |
210 | - if ( is_wp_error( $discount ) ) { |
|
211 | - return $discount; |
|
212 | - } |
|
210 | + if ( is_wp_error( $discount ) ) { |
|
211 | + return $discount; |
|
212 | + } |
|
213 | 213 | |
214 | - if ( ! wpinv_current_user_can_manage_invoicing() ) { |
|
215 | - return new WP_Error( |
|
214 | + if ( ! wpinv_current_user_can_manage_invoicing() ) { |
|
215 | + return new WP_Error( |
|
216 | 216 | 'rest_cannot_view', |
217 | 217 | __( 'Sorry, you are not allowed to view this discount.', 'invoicing' ), |
218 | 218 | array( |
@@ -221,40 +221,40 @@ discard block |
||
221 | 221 | ); |
222 | 222 | } |
223 | 223 | |
224 | - return true; |
|
224 | + return true; |
|
225 | 225 | } |
226 | 226 | |
227 | 227 | /** |
228 | - * Retrieves a single invoice item. |
|
229 | - * |
|
230 | - * @since 1.0.13 |
|
231 | - * |
|
232 | - * @param WP_REST_Request $request Full details about the request. |
|
233 | - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
234 | - */ |
|
235 | - public function get_item( $request ) { |
|
228 | + * Retrieves a single invoice item. |
|
229 | + * |
|
230 | + * @since 1.0.13 |
|
231 | + * |
|
232 | + * @param WP_REST_Request $request Full details about the request. |
|
233 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
234 | + */ |
|
235 | + public function get_item( $request ) { |
|
236 | 236 | |
237 | 237 | // Fetch the discount. |
238 | 238 | $discount = $this->get_post( $request['id'] ); |
239 | 239 | |
240 | 240 | // Abort early if it does not exist |
241 | - if ( is_wp_error( $discount ) ) { |
|
242 | - return $discount; |
|
243 | - } |
|
244 | - |
|
245 | - // Prepare the response |
|
246 | - $response = $this->prepare_item_for_response( $discount, $request ); |
|
247 | - |
|
248 | - /** |
|
249 | - * Filters the responses for single discount requests. |
|
250 | - * |
|
251 | - * |
|
252 | - * @since 1.0.13 |
|
253 | - * @var WP_HTTP_Response |
|
254 | - * |
|
255 | - * @param WP_HTTP_Response $response Response. |
|
256 | - * @param WP_REST_Request $request The request used. |
|
257 | - */ |
|
241 | + if ( is_wp_error( $discount ) ) { |
|
242 | + return $discount; |
|
243 | + } |
|
244 | + |
|
245 | + // Prepare the response |
|
246 | + $response = $this->prepare_item_for_response( $discount, $request ); |
|
247 | + |
|
248 | + /** |
|
249 | + * Filters the responses for single discount requests. |
|
250 | + * |
|
251 | + * |
|
252 | + * @since 1.0.13 |
|
253 | + * @var WP_HTTP_Response |
|
254 | + * |
|
255 | + * @param WP_HTTP_Response $response Response. |
|
256 | + * @param WP_REST_Request $request The request used. |
|
257 | + */ |
|
258 | 258 | $response = apply_filters( "wpinv_rest_get_discount_response", $response, $request ); |
259 | 259 | |
260 | 260 | return rest_ensure_response( $response ); |
@@ -262,26 +262,26 @@ discard block |
||
262 | 262 | } |
263 | 263 | |
264 | 264 | /** |
265 | - * Checks if a given request has access to create an invoice item. |
|
266 | - * |
|
267 | - * @since 1.0.13 |
|
268 | - * |
|
269 | - * @param WP_REST_Request $request Full details about the request. |
|
270 | - * @return true|WP_Error True if the request has access to create items, WP_Error object otherwise. |
|
271 | - */ |
|
272 | - public function create_item_permissions_check( $request ) { |
|
265 | + * Checks if a given request has access to create an invoice item. |
|
266 | + * |
|
267 | + * @since 1.0.13 |
|
268 | + * |
|
269 | + * @param WP_REST_Request $request Full details about the request. |
|
270 | + * @return true|WP_Error True if the request has access to create items, WP_Error object otherwise. |
|
271 | + */ |
|
272 | + public function create_item_permissions_check( $request ) { |
|
273 | 273 | |
274 | - if ( ! empty( $request['id'] ) ) { |
|
275 | - return new WP_Error( 'rest_item_exists', __( 'Cannot create existing item.', 'invoicing' ), array( 'status' => 400 ) ); |
|
276 | - } |
|
274 | + if ( ! empty( $request['id'] ) ) { |
|
275 | + return new WP_Error( 'rest_item_exists', __( 'Cannot create existing item.', 'invoicing' ), array( 'status' => 400 ) ); |
|
276 | + } |
|
277 | 277 | |
278 | - if ( wpinv_current_user_can_manage_invoicing() ) { |
|
279 | - return true; |
|
280 | - } |
|
278 | + if ( wpinv_current_user_can_manage_invoicing() ) { |
|
279 | + return true; |
|
280 | + } |
|
281 | 281 | |
282 | - $post_type = get_post_type_object( $this->post_type ); |
|
283 | - if ( ! current_user_can( $post_type->cap->create_posts ) ) { |
|
284 | - return new WP_Error( |
|
282 | + $post_type = get_post_type_object( $this->post_type ); |
|
283 | + if ( ! current_user_can( $post_type->cap->create_posts ) ) { |
|
284 | + return new WP_Error( |
|
285 | 285 | 'rest_cannot_create', |
286 | 286 | __( 'Sorry, you are not allowed to create discounts as this user.', 'invoicing' ), |
287 | 287 | array( |
@@ -290,261 +290,261 @@ discard block |
||
290 | 290 | ); |
291 | 291 | } |
292 | 292 | |
293 | - return true; |
|
293 | + return true; |
|
294 | 294 | } |
295 | 295 | |
296 | 296 | /** |
297 | - * Creates a single discount. |
|
298 | - * |
|
299 | - * @since 1.0.13 |
|
300 | - * |
|
301 | - * @param WP_REST_Request $request Full details about the request. |
|
302 | - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
303 | - */ |
|
304 | - public function create_item( $request ) { |
|
305 | - |
|
306 | - if ( ! empty( $request['id'] ) ) { |
|
307 | - return new WP_Error( 'rest_item_exists', __( 'Cannot create existing discount.', 'invoicing' ), array( 'status' => 400 ) ); |
|
308 | - } |
|
297 | + * Creates a single discount. |
|
298 | + * |
|
299 | + * @since 1.0.13 |
|
300 | + * |
|
301 | + * @param WP_REST_Request $request Full details about the request. |
|
302 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
303 | + */ |
|
304 | + public function create_item( $request ) { |
|
305 | + |
|
306 | + if ( ! empty( $request['id'] ) ) { |
|
307 | + return new WP_Error( 'rest_item_exists', __( 'Cannot create existing discount.', 'invoicing' ), array( 'status' => 400 ) ); |
|
308 | + } |
|
309 | 309 | |
310 | - $request->set_param( 'context', 'edit' ); |
|
310 | + $request->set_param( 'context', 'edit' ); |
|
311 | 311 | |
312 | - // Prepare the updated data. |
|
313 | - $discount_data = $this->prepare_item_for_database( $request ); |
|
312 | + // Prepare the updated data. |
|
313 | + $discount_data = $this->prepare_item_for_database( $request ); |
|
314 | 314 | |
315 | - if ( is_wp_error( $discount_data ) ) { |
|
316 | - return $discount_data; |
|
317 | - } |
|
315 | + if ( is_wp_error( $discount_data ) ) { |
|
316 | + return $discount_data; |
|
317 | + } |
|
318 | 318 | |
319 | - $discount_data['post_type'] = $this->post_type; |
|
319 | + $discount_data['post_type'] = $this->post_type; |
|
320 | 320 | |
321 | - // Try creating the discount. |
|
321 | + // Try creating the discount. |
|
322 | 322 | $discount = wp_insert_post( $discount_data, true ); |
323 | 323 | |
324 | - if ( is_wp_error( $discount ) ) { |
|
324 | + if ( is_wp_error( $discount ) ) { |
|
325 | 325 | return $discount; |
326 | - } |
|
327 | - |
|
328 | - // Prepare the response |
|
329 | - $response = $this->prepare_item_for_response( $discount, $request ); |
|
330 | - |
|
331 | - /** |
|
332 | - * Fires after a single discount is created or updated via the REST API. |
|
333 | - * |
|
334 | - * @since 1.0.13 |
|
335 | - * |
|
336 | - * @param WP_Post $discount Inserted or updated discount object. |
|
337 | - * @param WP_REST_Request $request Request object. |
|
338 | - * @param bool $creating True when creating a post, false when updating. |
|
339 | - */ |
|
340 | - do_action( "wpinv_rest_insert_discount", $discount, $request, true ); |
|
341 | - |
|
342 | - /** |
|
343 | - * Filters the responses for creating single item requests. |
|
344 | - * |
|
345 | - * |
|
346 | - * @since 1.0.13 |
|
347 | - * |
|
348 | - * |
|
349 | - * @param array $response Invoice properties. |
|
350 | - * @param WP_REST_Request $request The request used. |
|
351 | - */ |
|
326 | + } |
|
327 | + |
|
328 | + // Prepare the response |
|
329 | + $response = $this->prepare_item_for_response( $discount, $request ); |
|
330 | + |
|
331 | + /** |
|
332 | + * Fires after a single discount is created or updated via the REST API. |
|
333 | + * |
|
334 | + * @since 1.0.13 |
|
335 | + * |
|
336 | + * @param WP_Post $discount Inserted or updated discount object. |
|
337 | + * @param WP_REST_Request $request Request object. |
|
338 | + * @param bool $creating True when creating a post, false when updating. |
|
339 | + */ |
|
340 | + do_action( "wpinv_rest_insert_discount", $discount, $request, true ); |
|
341 | + |
|
342 | + /** |
|
343 | + * Filters the responses for creating single item requests. |
|
344 | + * |
|
345 | + * |
|
346 | + * @since 1.0.13 |
|
347 | + * |
|
348 | + * |
|
349 | + * @param array $response Invoice properties. |
|
350 | + * @param WP_REST_Request $request The request used. |
|
351 | + */ |
|
352 | 352 | $response = apply_filters( "wpinv_rest_create_discount_response", $response, $request ); |
353 | 353 | |
354 | 354 | return rest_ensure_response( $response ); |
355 | - } |
|
356 | - |
|
357 | - /** |
|
358 | - * Checks if a given request has access to update a discount. |
|
359 | - * |
|
360 | - * @since 1.0.13 |
|
361 | - * |
|
362 | - * @param WP_REST_Request $request Full details about the request. |
|
363 | - * @return true|WP_Error True if the request has access to update the item, WP_Error object otherwise. |
|
364 | - */ |
|
365 | - public function update_item_permissions_check( $request ) { |
|
366 | - |
|
367 | - // Retrieve the item. |
|
368 | - $item = $this->get_post( $request['id'] ); |
|
369 | - if ( is_wp_error( $item ) ) { |
|
370 | - return $item; |
|
371 | - } |
|
372 | - |
|
373 | - if ( wpinv_current_user_can_manage_invoicing() ) { |
|
374 | - return true; |
|
375 | - } |
|
376 | - |
|
377 | - return new WP_Error( |
|
378 | - 'rest_cannot_edit', |
|
379 | - __( 'Sorry, you are not allowed to update this discount.', 'invoicing' ), |
|
380 | - array( |
|
381 | - 'status' => rest_authorization_required_code(), |
|
382 | - ) |
|
383 | - ); |
|
384 | - |
|
385 | - } |
|
386 | - |
|
387 | - /** |
|
388 | - * Updates a single discount. |
|
389 | - * |
|
390 | - * @since 1.0.13 |
|
391 | - * |
|
392 | - * @param WP_REST_Request $request Full details about the request. |
|
393 | - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
394 | - */ |
|
395 | - public function update_item( $request ) { |
|
355 | + } |
|
356 | + |
|
357 | + /** |
|
358 | + * Checks if a given request has access to update a discount. |
|
359 | + * |
|
360 | + * @since 1.0.13 |
|
361 | + * |
|
362 | + * @param WP_REST_Request $request Full details about the request. |
|
363 | + * @return true|WP_Error True if the request has access to update the item, WP_Error object otherwise. |
|
364 | + */ |
|
365 | + public function update_item_permissions_check( $request ) { |
|
366 | + |
|
367 | + // Retrieve the item. |
|
368 | + $item = $this->get_post( $request['id'] ); |
|
369 | + if ( is_wp_error( $item ) ) { |
|
370 | + return $item; |
|
371 | + } |
|
372 | + |
|
373 | + if ( wpinv_current_user_can_manage_invoicing() ) { |
|
374 | + return true; |
|
375 | + } |
|
376 | + |
|
377 | + return new WP_Error( |
|
378 | + 'rest_cannot_edit', |
|
379 | + __( 'Sorry, you are not allowed to update this discount.', 'invoicing' ), |
|
380 | + array( |
|
381 | + 'status' => rest_authorization_required_code(), |
|
382 | + ) |
|
383 | + ); |
|
384 | + |
|
385 | + } |
|
386 | + |
|
387 | + /** |
|
388 | + * Updates a single discount. |
|
389 | + * |
|
390 | + * @since 1.0.13 |
|
391 | + * |
|
392 | + * @param WP_REST_Request $request Full details about the request. |
|
393 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
394 | + */ |
|
395 | + public function update_item( $request ) { |
|
396 | 396 | |
397 | - // Ensure the item exists. |
|
397 | + // Ensure the item exists. |
|
398 | 398 | $valid_check = $this->get_post( $request['id'] ); |
399 | 399 | |
400 | 400 | // Abort early if it does not exist |
401 | - if ( is_wp_error( $valid_check ) ) { |
|
402 | - return $valid_check; |
|
403 | - } |
|
401 | + if ( is_wp_error( $valid_check ) ) { |
|
402 | + return $valid_check; |
|
403 | + } |
|
404 | 404 | |
405 | - $request->set_param( 'context', 'edit' ); |
|
405 | + $request->set_param( 'context', 'edit' ); |
|
406 | 406 | |
407 | - // Prepare the updated data. |
|
408 | - $data_to_update = $this->prepare_item_for_database( $request ); |
|
407 | + // Prepare the updated data. |
|
408 | + $data_to_update = $this->prepare_item_for_database( $request ); |
|
409 | 409 | |
410 | - if ( is_wp_error( $data_to_update ) ) { |
|
411 | - return $data_to_update; |
|
412 | - } |
|
410 | + if ( is_wp_error( $data_to_update ) ) { |
|
411 | + return $data_to_update; |
|
412 | + } |
|
413 | 413 | |
414 | - if( empty( $data_to_update['meta_input'] ) ) { |
|
415 | - unset( $data_to_update['meta_input'] ); |
|
416 | - } |
|
414 | + if( empty( $data_to_update['meta_input'] ) ) { |
|
415 | + unset( $data_to_update['meta_input'] ); |
|
416 | + } |
|
417 | 417 | |
418 | - // Abort if no item data is provided |
|
418 | + // Abort if no item data is provided |
|
419 | 419 | if( empty( $data_to_update ) ) { |
420 | 420 | return new WP_Error( 'missing_data', __( 'An update request cannot be empty.', 'invoicing' ) ); |
421 | - } |
|
421 | + } |
|
422 | 422 | |
423 | - // post_status |
|
424 | - if( ! empty( $data_to_update['post_status'] ) ) { |
|
425 | - wpinv_update_discount_status( $request['id'], $data_to_update['post_status'] ); |
|
426 | - unset( $data_to_update['post_status'] ); |
|
427 | - } |
|
423 | + // post_status |
|
424 | + if( ! empty( $data_to_update['post_status'] ) ) { |
|
425 | + wpinv_update_discount_status( $request['id'], $data_to_update['post_status'] ); |
|
426 | + unset( $data_to_update['post_status'] ); |
|
427 | + } |
|
428 | 428 | |
429 | - // Update the item |
|
430 | - if( ! empty( $data_to_update ) ) { |
|
429 | + // Update the item |
|
430 | + if( ! empty( $data_to_update ) ) { |
|
431 | 431 | |
432 | - // Include the item ID |
|
433 | - $data_to_update['ID'] = $request['id']; |
|
432 | + // Include the item ID |
|
433 | + $data_to_update['ID'] = $request['id']; |
|
434 | 434 | |
435 | - $updated = wp_update_post( $data_to_update, true ); |
|
435 | + $updated = wp_update_post( $data_to_update, true ); |
|
436 | 436 | |
437 | - // Incase the update operation failed... |
|
438 | - if ( is_wp_error( $updated ) ) { |
|
439 | - return $updated; |
|
440 | - } |
|
437 | + // Incase the update operation failed... |
|
438 | + if ( is_wp_error( $updated ) ) { |
|
439 | + return $updated; |
|
440 | + } |
|
441 | 441 | |
442 | - } |
|
442 | + } |
|
443 | 443 | |
444 | - $updated_discount = get_post( $request['id'] ); |
|
444 | + $updated_discount = get_post( $request['id'] ); |
|
445 | 445 | |
446 | - // Prepare the response |
|
447 | - $response = $this->prepare_item_for_response( $updated_discount, $request ); |
|
446 | + // Prepare the response |
|
447 | + $response = $this->prepare_item_for_response( $updated_discount, $request ); |
|
448 | 448 | |
449 | - /** This action is documented in includes/class-wpinv-rest-item-controller.php */ |
|
450 | - do_action( "wpinv_rest_insert_discount", $updated_discount, $request, false ); |
|
449 | + /** This action is documented in includes/class-wpinv-rest-item-controller.php */ |
|
450 | + do_action( "wpinv_rest_insert_discount", $updated_discount, $request, false ); |
|
451 | 451 | |
452 | - /** |
|
453 | - * Filters the responses for updating single discount requests. |
|
454 | - * |
|
455 | - * |
|
456 | - * @since 1.0.13 |
|
457 | - * |
|
458 | - * |
|
459 | - * @param array $data_to_update Discount properties. |
|
460 | - * @param WP_REST_Request $request The request used. |
|
461 | - */ |
|
452 | + /** |
|
453 | + * Filters the responses for updating single discount requests. |
|
454 | + * |
|
455 | + * |
|
456 | + * @since 1.0.13 |
|
457 | + * |
|
458 | + * |
|
459 | + * @param array $data_to_update Discount properties. |
|
460 | + * @param WP_REST_Request $request The request used. |
|
461 | + */ |
|
462 | 462 | $response = apply_filters( "wpinv_rest_update_discount_response", $response, $data_to_update, $request ); |
463 | 463 | |
464 | 464 | return rest_ensure_response( $response ); |
465 | - } |
|
466 | - |
|
467 | - /** |
|
468 | - * Checks if a given request has access to delete a discount. |
|
469 | - * |
|
470 | - * @since 1.0.13 |
|
471 | - * |
|
472 | - * @param WP_REST_Request $request Full details about the request. |
|
473 | - * @return true|WP_Error True if the request has access to delete the discount, WP_Error object otherwise. |
|
474 | - */ |
|
475 | - public function delete_item_permissions_check( $request ) { |
|
476 | - |
|
477 | - // Retrieve the discount. |
|
478 | - $discount = $this->get_post( $request['id'] ); |
|
479 | - if ( is_wp_error( $discount ) ) { |
|
480 | - return $discount; |
|
481 | - } |
|
482 | - |
|
483 | - // Ensure the current user can delete the discount |
|
484 | - if (! wpinv_current_user_can_manage_invoicing() ) { |
|
485 | - return new WP_Error( |
|
465 | + } |
|
466 | + |
|
467 | + /** |
|
468 | + * Checks if a given request has access to delete a discount. |
|
469 | + * |
|
470 | + * @since 1.0.13 |
|
471 | + * |
|
472 | + * @param WP_REST_Request $request Full details about the request. |
|
473 | + * @return true|WP_Error True if the request has access to delete the discount, WP_Error object otherwise. |
|
474 | + */ |
|
475 | + public function delete_item_permissions_check( $request ) { |
|
476 | + |
|
477 | + // Retrieve the discount. |
|
478 | + $discount = $this->get_post( $request['id'] ); |
|
479 | + if ( is_wp_error( $discount ) ) { |
|
480 | + return $discount; |
|
481 | + } |
|
482 | + |
|
483 | + // Ensure the current user can delete the discount |
|
484 | + if (! wpinv_current_user_can_manage_invoicing() ) { |
|
485 | + return new WP_Error( |
|
486 | 486 | 'rest_cannot_delete', |
487 | 487 | __( 'Sorry, you are not allowed to delete this discount.', 'invoicing' ), |
488 | 488 | array( |
489 | 489 | 'status' => rest_authorization_required_code(), |
490 | 490 | ) |
491 | 491 | ); |
492 | - } |
|
493 | - |
|
494 | - return true; |
|
495 | - } |
|
496 | - |
|
497 | - /** |
|
498 | - * Deletes a single discount. |
|
499 | - * |
|
500 | - * @since 1.0.13 |
|
501 | - * |
|
502 | - * @param WP_REST_Request $request Full details about the request. |
|
503 | - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
504 | - */ |
|
505 | - public function delete_item( $request ) { |
|
492 | + } |
|
493 | + |
|
494 | + return true; |
|
495 | + } |
|
496 | + |
|
497 | + /** |
|
498 | + * Deletes a single discount. |
|
499 | + * |
|
500 | + * @since 1.0.13 |
|
501 | + * |
|
502 | + * @param WP_REST_Request $request Full details about the request. |
|
503 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
504 | + */ |
|
505 | + public function delete_item( $request ) { |
|
506 | 506 | |
507 | - // Retrieve the discount. |
|
508 | - $discount = $this->get_post( $request['id'] ); |
|
509 | - if ( is_wp_error( $discount ) ) { |
|
510 | - return $discount; |
|
511 | - } |
|
507 | + // Retrieve the discount. |
|
508 | + $discount = $this->get_post( $request['id'] ); |
|
509 | + if ( is_wp_error( $discount ) ) { |
|
510 | + return $discount; |
|
511 | + } |
|
512 | 512 | |
513 | - $request->set_param( 'context', 'edit' ); |
|
513 | + $request->set_param( 'context', 'edit' ); |
|
514 | 514 | |
515 | - // Prepare the discount id |
|
516 | - $id = $discount->ID; |
|
515 | + // Prepare the discount id |
|
516 | + $id = $discount->ID; |
|
517 | 517 | |
518 | - // Prepare the response |
|
519 | - $response = $this->prepare_item_for_response( $discount, $request ); |
|
518 | + // Prepare the response |
|
519 | + $response = $this->prepare_item_for_response( $discount, $request ); |
|
520 | 520 | |
521 | - // Delete the discount... |
|
522 | - wpinv_remove_discount( $id ); |
|
521 | + // Delete the discount... |
|
522 | + wpinv_remove_discount( $id ); |
|
523 | 523 | |
524 | - /** |
|
525 | - * Fires immediately after a single discount is deleted via the REST API. |
|
526 | - * |
|
527 | - * |
|
528 | - * @since 1.0.13 |
|
529 | - * |
|
530 | - * @param WP_POST $discount The deleted discount. |
|
531 | - * @param WP_REST_Request $request The request sent to the API. |
|
532 | - */ |
|
533 | - do_action( "wpinv_rest_delete_discount", $discount, $request ); |
|
524 | + /** |
|
525 | + * Fires immediately after a single discount is deleted via the REST API. |
|
526 | + * |
|
527 | + * |
|
528 | + * @since 1.0.13 |
|
529 | + * |
|
530 | + * @param WP_POST $discount The deleted discount. |
|
531 | + * @param WP_REST_Request $request The request sent to the API. |
|
532 | + */ |
|
533 | + do_action( "wpinv_rest_delete_discount", $discount, $request ); |
|
534 | 534 | |
535 | - return $response; |
|
535 | + return $response; |
|
536 | 536 | |
537 | - } |
|
537 | + } |
|
538 | 538 | |
539 | 539 | |
540 | 540 | /** |
541 | - * Retrieves the query params for the discount collection. |
|
542 | - * |
|
543 | - * @since 1.0.13 |
|
544 | - * |
|
545 | - * @return array Collection parameters. |
|
546 | - */ |
|
547 | - public function get_collection_params() { |
|
541 | + * Retrieves the query params for the discount collection. |
|
542 | + * |
|
543 | + * @since 1.0.13 |
|
544 | + * |
|
545 | + * @return array Collection parameters. |
|
546 | + */ |
|
547 | + public function get_collection_params() { |
|
548 | 548 | |
549 | 549 | $query_params = array( |
550 | 550 | |
@@ -558,27 +558,27 @@ discard block |
||
558 | 558 | |
559 | 559 | // Discount types |
560 | 560 | 'type' => array( |
561 | - 'description' => __( 'Type of discounts to fetch.', 'invoicing' ), |
|
562 | - 'type' => 'array', |
|
563 | - 'default' => array_keys( wpinv_get_discount_types() ), |
|
564 | - 'items' => array( |
|
561 | + 'description' => __( 'Type of discounts to fetch.', 'invoicing' ), |
|
562 | + 'type' => 'array', |
|
563 | + 'default' => array_keys( wpinv_get_discount_types() ), |
|
564 | + 'items' => array( |
|
565 | 565 | 'enum' => array_keys( wpinv_get_discount_types() ), |
566 | 566 | 'type' => 'string', |
567 | 567 | ), |
568 | - ), |
|
568 | + ), |
|
569 | 569 | |
570 | - // Number of results per page |
|
570 | + // Number of results per page |
|
571 | 571 | 'limit' => array( |
572 | - 'description' => __( 'Number of discounts to fetch.', 'invoicing' ), |
|
573 | - 'type' => 'integer', |
|
574 | - 'default' => (int) get_option( 'posts_per_page' ), |
|
572 | + 'description' => __( 'Number of discounts to fetch.', 'invoicing' ), |
|
573 | + 'type' => 'integer', |
|
574 | + 'default' => (int) get_option( 'posts_per_page' ), |
|
575 | 575 | ), |
576 | 576 | |
577 | 577 | // Pagination |
578 | 578 | 'page' => array( |
579 | - 'description' => __( 'Current page to fetch.', 'invoicing' ), |
|
580 | - 'type' => 'integer', |
|
581 | - 'default' => 1, |
|
579 | + 'description' => __( 'Current page to fetch.', 'invoicing' ), |
|
580 | + 'type' => 'integer', |
|
581 | + 'default' => 1, |
|
582 | 582 | ), |
583 | 583 | |
584 | 584 | // Exclude certain items |
@@ -601,9 +601,9 @@ discard block |
||
601 | 601 | 'date', |
602 | 602 | 'ID', |
603 | 603 | 'modified', |
604 | - 'title', |
|
605 | - 'relevance', |
|
606 | - 'rand' |
|
604 | + 'title', |
|
605 | + 'relevance', |
|
606 | + 'rand' |
|
607 | 607 | ), |
608 | 608 | ), |
609 | 609 | |
@@ -613,598 +613,598 @@ discard block |
||
613 | 613 | 'type' => 'string', |
614 | 614 | 'default' => 'DESC', |
615 | 615 | 'enum' => array( 'ASC', 'DESC' ), |
616 | - ), |
|
616 | + ), |
|
617 | 617 | |
618 | - // Search term |
|
618 | + // Search term |
|
619 | 619 | 'search' => array( |
620 | - 'description' => __( 'Return discounts that match the search term.', 'invoicing' ), |
|
621 | - 'type' => 'string', |
|
620 | + 'description' => __( 'Return discounts that match the search term.', 'invoicing' ), |
|
621 | + 'type' => 'string', |
|
622 | 622 | ), |
623 | 623 | ); |
624 | 624 | |
625 | - /** |
|
626 | - * Filter collection parameters for the discounts controller. |
|
627 | - * |
|
628 | - * |
|
629 | - * @since 1.0.13 |
|
630 | - * |
|
631 | - * @param array $query_params JSON Schema-formatted collection parameters. |
|
632 | - */ |
|
633 | - return apply_filters( "wpinv_rest_discounts_collection_params", $query_params ); |
|
625 | + /** |
|
626 | + * Filter collection parameters for the discounts controller. |
|
627 | + * |
|
628 | + * |
|
629 | + * @since 1.0.13 |
|
630 | + * |
|
631 | + * @param array $query_params JSON Schema-formatted collection parameters. |
|
632 | + */ |
|
633 | + return apply_filters( "wpinv_rest_discounts_collection_params", $query_params ); |
|
634 | 634 | } |
635 | 635 | |
636 | 636 | /** |
637 | - * Checks if a given post type can be viewed or managed. |
|
638 | - * |
|
639 | - * @since 1.0.13 |
|
640 | - * |
|
641 | - * @param object|string $post_type Post type name or object. |
|
642 | - * @return bool Whether the post type is allowed in REST. |
|
643 | - */ |
|
644 | - protected function check_is_post_type_allowed( $post_type ) { |
|
645 | - return true; |
|
646 | - } |
|
647 | - |
|
648 | - /** |
|
649 | - * Prepares a single item for create or update. |
|
650 | - * |
|
651 | - * @since 1.0.13 |
|
652 | - * |
|
653 | - * @param WP_REST_Request $request Request object. |
|
654 | - * @return array|WP_Error Discount Properties or WP_Error. |
|
655 | - */ |
|
656 | - protected function prepare_item_for_database( $request ) { |
|
657 | - $prepared_item = new stdClass(); |
|
658 | - $prepared_item->meta_input = array(); |
|
659 | - |
|
660 | - // Post ID. |
|
661 | - if ( isset( $request['id'] ) ) { |
|
662 | - $existing_item = $this->get_post( $request['id'] ); |
|
663 | - if ( is_wp_error( $existing_item ) ) { |
|
664 | - return $existing_item; |
|
665 | - } |
|
666 | - |
|
667 | - $prepared_item->ID = $existing_item->ID; |
|
668 | - } |
|
669 | - |
|
670 | - $schema = $this->get_item_schema(); |
|
671 | - |
|
672 | - // item title. |
|
673 | - if ( ! empty( $schema['properties']['title'] ) && isset( $request['title'] ) ) { |
|
674 | - $prepared_item->post_title = sanitize_text_field( $request['title'] ); |
|
675 | - } |
|
676 | - |
|
677 | - // item status. |
|
678 | - if ( ! empty( $schema['properties']['status'] ) && isset( $request['status'] ) && in_array( $request['status'], array_keys( get_post_stati( array( 'internal' => false ) ) ) ) ) { |
|
679 | - $prepared_item->post_status = sanitize_text_field( $request['status'] ); |
|
680 | - } |
|
681 | - |
|
682 | - // Code. |
|
683 | - if ( ! empty( $schema['properties']['code'] ) && isset( $request['code'] ) ) { |
|
684 | - $prepared_item->meta_input['_wpi_discount_code'] = trim( $request['code'] ); |
|
685 | - } |
|
686 | - |
|
687 | - // Type. |
|
688 | - if ( ! empty( $schema['properties']['type'] ) && isset( $request['type'] ) && in_array( $request['type'], array_keys( wpinv_get_discount_types() ) ) ) { |
|
689 | - $prepared_item->meta_input['_wpi_discount_type'] = trim( $request['type'] ); |
|
690 | - } |
|
691 | - |
|
692 | - // Amount. |
|
693 | - if ( ! empty( $schema['properties']['amount'] ) && isset( $request['amount'] ) ) { |
|
694 | - $prepared_item->meta_input['_wpi_discount_amount'] = floatval( $request['amount'] ); |
|
695 | - } |
|
696 | - |
|
697 | - // Items. |
|
698 | - if ( ! empty( $schema['properties']['items'] ) && isset( $request['items'] ) ) { |
|
699 | - $prepared_item->meta_input['_wpi_discount_items'] = wpinv_parse_list( $request['items'] ); |
|
700 | - } |
|
701 | - |
|
702 | - // Excluded Items. |
|
703 | - if ( ! empty( $schema['properties']['exclude_items'] ) && isset( $request['exclude_items'] ) ) { |
|
704 | - $prepared_item->meta_input['_wpi_discount_excluded_items'] = wpinv_parse_list( $request['exclude_items'] ); |
|
705 | - } |
|
706 | - |
|
707 | - // Start date. |
|
708 | - if ( ! empty( $schema['properties']['start_date'] ) && isset( $request['start_date'] ) ) { |
|
709 | - $prepared_item->meta_input['_wpi_discount_start'] = trim( $request['start_date'] ); |
|
710 | - } |
|
711 | - |
|
712 | - // End date. |
|
713 | - if ( ! empty( $schema['properties']['end_date'] ) && isset( $request['end_date'] ) ) { |
|
714 | - $prepared_item->meta_input['_wpi_discount_expiration'] = trim( $request['end_date'] ); |
|
715 | - } |
|
716 | - |
|
717 | - // Minimum amount. |
|
718 | - if ( ! empty( $schema['properties']['minimum_amount'] ) && isset( $request['minimum_amount'] ) ) { |
|
719 | - $prepared_item->meta_input['_wpi_discount_min_total'] = floatval( $request['minimum_amount'] ); |
|
720 | - } |
|
721 | - |
|
722 | - // Maximum amount. |
|
723 | - if ( ! empty( $schema['properties']['maximum_amount'] ) && isset( $request['maximum_amount'] ) ) { |
|
724 | - $prepared_item->meta_input['_wpi_discount_max_total'] = floatval( $request['maximum_amount'] ); |
|
725 | - } |
|
726 | - |
|
727 | - // Recurring. |
|
728 | - if ( ! empty( $schema['properties']['recurring'] ) && isset( $request['recurring'] ) ) { |
|
729 | - $prepared_item->meta_input['_wpi_discount_is_recurring'] = empty( (int) $request['recurring'] ) ? 0 : 1; |
|
730 | - } |
|
731 | - |
|
732 | - // Maximum uses. |
|
733 | - if ( ! empty( $schema['properties']['max_uses'] ) && isset( $request['max_uses'] ) ) { |
|
734 | - $prepared_item->meta_input['_wpi_discount_max_uses'] = intval( $request['max_uses'] ); |
|
735 | - } |
|
736 | - |
|
737 | - // Single use. |
|
738 | - if ( ! empty( $schema['properties']['single_use'] ) && isset( $request['single_use'] ) ) { |
|
739 | - $prepared_item->meta_input['_wpi_discount_is_single_use'] = empty( (int) $request['single_use'] ) ? 0 : 1; |
|
740 | - } |
|
741 | - |
|
742 | - $discount_data = (array) wp_unslash( $prepared_item ); |
|
743 | - |
|
744 | - /** |
|
745 | - * Filters an item before it is inserted via the REST API. |
|
746 | - * |
|
747 | - * @since 1.0.13 |
|
748 | - * |
|
749 | - * @param array $discount_data An array of discount data |
|
750 | - * @param WP_REST_Request $request Request object. |
|
751 | - */ |
|
752 | - return apply_filters( "wpinv_rest_pre_insert_discount", $discount_data, $request ); |
|
753 | - |
|
754 | - } |
|
755 | - |
|
756 | - /** |
|
757 | - * Prepares a single discount output for response. |
|
758 | - * |
|
759 | - * @since 1.0.13 |
|
760 | - * |
|
761 | - * @param WP_Post $discount WP_Post object. |
|
762 | - * @param WP_REST_Request $request Request object. |
|
763 | - * @return WP_REST_Response Response object. |
|
764 | - */ |
|
765 | - public function prepare_item_for_response( $discount, $request ) { |
|
766 | - |
|
767 | - $GLOBALS['post'] = get_post( $discount->ID ); |
|
768 | - |
|
769 | - setup_postdata( $discount->ID ); |
|
770 | - |
|
771 | - // Fetch the fields to include in this response. |
|
772 | - $fields = $this->get_fields_for_response( $request ); |
|
773 | - |
|
774 | - // Base fields for every discount. |
|
775 | - $data = array(); |
|
776 | - |
|
777 | - // Set up ID. |
|
778 | - if ( rest_is_field_included( 'id', $fields ) ) { |
|
779 | - $data['id'] = $discount->ID; |
|
780 | - } |
|
781 | - |
|
782 | - // Title. |
|
783 | - if ( rest_is_field_included( 'title', $fields ) ) { |
|
784 | - $data['title'] = get_the_title( $discount->ID ); |
|
785 | - } |
|
786 | - |
|
787 | - // Code. |
|
788 | - if ( rest_is_field_included( 'code', $fields ) ) { |
|
789 | - $data['code'] = wpinv_get_discount_code( $discount->ID ); |
|
790 | - } |
|
791 | - |
|
792 | - // Type. |
|
793 | - if ( rest_is_field_included( 'type', $fields ) ) { |
|
794 | - $data['type'] = wpinv_get_discount_type( $discount->ID ); |
|
795 | - } |
|
796 | - |
|
797 | - // Amount. |
|
798 | - if ( rest_is_field_included( 'amount', $fields ) ) { |
|
799 | - $data['amount'] = wpinv_get_discount_amount( $discount->ID ); |
|
800 | - } |
|
801 | - |
|
802 | - // Status. |
|
803 | - if ( rest_is_field_included( 'status', $fields ) ) { |
|
804 | - $data['status'] = get_post_status( $discount->ID ); |
|
805 | - } |
|
806 | - |
|
807 | - // Items. |
|
808 | - if ( rest_is_field_included( 'items', $fields ) ) { |
|
809 | - $data['items'] = wpinv_get_discount_item_reqs( $discount->ID ); |
|
810 | - } |
|
811 | - |
|
812 | - // Excluded Items. |
|
813 | - if ( rest_is_field_included( 'exclude_items', $fields ) ) { |
|
814 | - $data['exclude_items'] = wpinv_get_discount_excluded_items( $discount->ID ); |
|
815 | - } |
|
816 | - |
|
817 | - // Start date. |
|
818 | - if ( rest_is_field_included( 'start_date', $fields ) ) { |
|
819 | - $data['start_date'] = wpinv_get_discount_start_date( $discount->ID ); |
|
820 | - } |
|
821 | - |
|
822 | - // End date. |
|
823 | - if ( rest_is_field_included( 'end_date', $fields ) ) { |
|
824 | - $data['end_date'] = wpinv_get_discount_expiration( $discount->ID ); |
|
825 | - } |
|
826 | - |
|
827 | - // Minimum amount. |
|
828 | - if ( rest_is_field_included( 'minimum_amount', $fields ) ) { |
|
829 | - $data['minimum_amount'] = wpinv_get_discount_min_total( $discount->ID ); |
|
830 | - } |
|
831 | - |
|
832 | - // Maximum amount. |
|
833 | - if ( rest_is_field_included( 'maximum_amount', $fields ) ) { |
|
834 | - $data['maximum_amount'] = wpinv_get_discount_max_total( $discount->ID ); |
|
835 | - } |
|
836 | - |
|
837 | - // Recurring. |
|
838 | - if ( rest_is_field_included( 'recurring', $fields ) ) { |
|
839 | - $data['recurring'] = wpinv_discount_is_recurring( $discount->ID ); |
|
840 | - } |
|
841 | - |
|
842 | - // Maximum uses. |
|
843 | - if ( rest_is_field_included( 'max_uses', $fields ) ) { |
|
844 | - $data['max_uses'] = wpinv_get_discount_max_uses( $discount->ID ); |
|
845 | - } |
|
846 | - |
|
847 | - // Single use. |
|
848 | - if ( rest_is_field_included( 'single_use', $fields ) ) { |
|
849 | - $data['single_use'] = wpinv_discount_is_single_use( $discount->ID ); |
|
850 | - } |
|
851 | - |
|
852 | - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
853 | - $data = $this->add_additional_fields_to_object( $data, $request ); |
|
854 | - $data = $this->filter_response_by_context( $data, $context ); |
|
855 | - |
|
856 | - // Wrap the data in a response object. |
|
857 | - $response = rest_ensure_response( $data ); |
|
858 | - |
|
859 | - $links = $this->prepare_links( $discount ); |
|
860 | - $response->add_links( $links ); |
|
861 | - |
|
862 | - if ( ! empty( $links['self']['href'] ) ) { |
|
863 | - $actions = $this->get_available_actions( $discount, $request ); |
|
864 | - |
|
865 | - $self = $links['self']['href']; |
|
866 | - |
|
867 | - foreach ( $actions as $rel ) { |
|
868 | - $response->add_link( $rel, $self ); |
|
869 | - } |
|
870 | - } |
|
871 | - |
|
872 | - /** |
|
873 | - * Filters the discount data for a response. |
|
874 | - * |
|
875 | - * @since 1.0.13 |
|
876 | - * |
|
877 | - * @param WP_REST_Response $response The response object. |
|
878 | - * @param WP_Post $discount The discount post object. |
|
879 | - * @param WP_REST_Request $request Request object. |
|
880 | - */ |
|
881 | - return apply_filters( "wpinv_rest_prepare_discount", $response, $discount, $request ); |
|
882 | - } |
|
883 | - |
|
884 | - /** |
|
885 | - * Gets an array of fields to be included on the response. |
|
886 | - * |
|
887 | - * Included fields are based on item schema and `_fields=` request argument. |
|
888 | - * |
|
889 | - * @since 1.0.13 |
|
890 | - * |
|
891 | - * @param WP_REST_Request $request Full details about the request. |
|
892 | - * @return array Fields to be included in the response. |
|
893 | - */ |
|
894 | - public function get_fields_for_response( $request ) { |
|
895 | - $schema = $this->get_item_schema(); |
|
896 | - $properties = isset( $schema['properties'] ) ? $schema['properties'] : array(); |
|
897 | - |
|
898 | - $additional_fields = $this->get_additional_fields(); |
|
899 | - foreach ( $additional_fields as $field_name => $field_options ) { |
|
900 | - // For back-compat, include any field with an empty schema |
|
901 | - // because it won't be present in $this->get_item_schema(). |
|
902 | - if ( is_null( $field_options['schema'] ) ) { |
|
903 | - $properties[ $field_name ] = $field_options; |
|
904 | - } |
|
905 | - } |
|
906 | - |
|
907 | - // Exclude fields that specify a different context than the request context. |
|
908 | - $context = $request['context']; |
|
909 | - if ( $context ) { |
|
910 | - foreach ( $properties as $name => $options ) { |
|
911 | - if ( ! empty( $options['context'] ) && ! in_array( $context, $options['context'], true ) ) { |
|
912 | - unset( $properties[ $name ] ); |
|
913 | - } |
|
914 | - } |
|
915 | - } |
|
916 | - |
|
917 | - $fields = array_keys( $properties ); |
|
918 | - |
|
919 | - if ( ! isset( $request['_fields'] ) ) { |
|
920 | - return $fields; |
|
921 | - } |
|
922 | - $requested_fields = wpinv_parse_list( $request['_fields'] ); |
|
923 | - if ( 0 === count( $requested_fields ) ) { |
|
924 | - return $fields; |
|
925 | - } |
|
926 | - // Trim off outside whitespace from the comma delimited list. |
|
927 | - $requested_fields = array_map( 'trim', $requested_fields ); |
|
928 | - // Always persist 'id', because it can be needed for add_additional_fields_to_object(). |
|
929 | - if ( in_array( 'id', $fields, true ) ) { |
|
930 | - $requested_fields[] = 'id'; |
|
931 | - } |
|
932 | - // Return the list of all requested fields which appear in the schema. |
|
933 | - return array_reduce( |
|
934 | - $requested_fields, |
|
935 | - function( $response_fields, $field ) use ( $fields ) { |
|
936 | - if ( in_array( $field, $fields, true ) ) { |
|
937 | - $response_fields[] = $field; |
|
938 | - return $response_fields; |
|
939 | - } |
|
940 | - // Check for nested fields if $field is not a direct match. |
|
941 | - $nested_fields = explode( '.', $field ); |
|
942 | - // A nested field is included so long as its top-level property is |
|
943 | - // present in the schema. |
|
944 | - if ( in_array( $nested_fields[0], $fields, true ) ) { |
|
945 | - $response_fields[] = $field; |
|
946 | - } |
|
947 | - return $response_fields; |
|
948 | - }, |
|
949 | - array() |
|
950 | - ); |
|
951 | - } |
|
952 | - |
|
953 | - /** |
|
954 | - * Retrieves the discount's schema, conforming to JSON Schema. |
|
955 | - * |
|
956 | - * @since 1.0.13 |
|
957 | - * |
|
958 | - * @return array Discount schema data. |
|
959 | - */ |
|
960 | - public function get_item_schema() { |
|
961 | - |
|
962 | - // Maybe retrieve the schema from cache. |
|
963 | - if ( empty( $this->schema ) ) { |
|
964 | - return $this->add_additional_fields_schema( $this->schema ); |
|
965 | - } |
|
966 | - |
|
967 | - $schema = array( |
|
968 | - '$schema' => 'http://json-schema.org/draft-04/schema#', |
|
969 | - 'title' => $this->post_type, |
|
970 | - 'type' => 'object', |
|
971 | - |
|
972 | - // Base properties for every Item. |
|
973 | - 'properties' => array( |
|
974 | - |
|
975 | - 'id' => array( |
|
976 | - 'description' => __( 'Unique identifier for the discount.', 'invoicing' ), |
|
977 | - 'type' => 'integer', |
|
978 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
979 | - 'readonly' => true, |
|
980 | - ), |
|
981 | - |
|
982 | - 'title' => array( |
|
983 | - 'description' => __( 'The title for the discount.', 'invoicing' ), |
|
984 | - 'type' => 'string', |
|
985 | - 'context' => array( 'view', 'edit' ), |
|
986 | - ), |
|
987 | - |
|
988 | - 'code' => array( |
|
989 | - 'description' => __( 'The discount code.', 'invoicing' ), |
|
990 | - 'type' => 'string', |
|
991 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
992 | - 'required' => true, |
|
993 | - ), |
|
994 | - |
|
995 | - 'type' => array( |
|
996 | - 'description' => __( 'The type of discount.', 'invoicing' ), |
|
997 | - 'type' => 'string', |
|
998 | - 'enum' => array_keys( wpinv_get_discount_types() ), |
|
999 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1000 | - 'default' => 'percentage', |
|
1001 | - ), |
|
1002 | - |
|
1003 | - 'amount' => array( |
|
1004 | - 'description' => __( 'The discount value.', 'invoicing' ), |
|
1005 | - 'type' => 'number', |
|
1006 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1007 | - 'required' => true, |
|
1008 | - ), |
|
1009 | - |
|
1010 | - 'status' => array( |
|
1011 | - 'description' => __( 'A named status for the discount.', 'invoicing' ), |
|
1012 | - 'type' => 'string', |
|
1013 | - 'enum' => array_keys( get_post_stati( array( 'internal' => false ) ) ), |
|
1014 | - 'context' => array( 'view', 'edit' ), |
|
1015 | - ), |
|
1016 | - |
|
1017 | - 'items' => array( |
|
1018 | - 'description' => __( 'Items which need to be in the cart to use this discount or, for "Item Discounts", which items are discounted. If left blank, this discount will be used on any item.', 'invoicing' ), |
|
1019 | - 'type' => 'array', |
|
1020 | - 'context' => array( 'view', 'edit' ), |
|
1021 | - ), |
|
1022 | - |
|
1023 | - 'exclude_items' => array( |
|
1024 | - 'description' => __( 'Items which are NOT allowed to use this discount.', 'invoicing' ), |
|
1025 | - 'type' => 'array', |
|
1026 | - 'context' => array( 'view', 'edit' ), |
|
1027 | - ), |
|
1028 | - |
|
1029 | - 'start_date' => array( |
|
1030 | - 'description' => __( 'The start date for the discount in the format of yyyy-mm-dd hh:mm:ss . If provided, the discount can only be used after or on this date.', 'invoicing' ), |
|
1031 | - 'type' => 'string', |
|
1032 | - 'context' => array( 'view', 'edit' ), |
|
1033 | - ), |
|
1034 | - |
|
1035 | - 'end_date' => array( |
|
1036 | - 'description' => __( 'The expiration date for the discount.', 'invoicing' ), |
|
1037 | - 'type' => 'string', |
|
1038 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1039 | - ), |
|
637 | + * Checks if a given post type can be viewed or managed. |
|
638 | + * |
|
639 | + * @since 1.0.13 |
|
640 | + * |
|
641 | + * @param object|string $post_type Post type name or object. |
|
642 | + * @return bool Whether the post type is allowed in REST. |
|
643 | + */ |
|
644 | + protected function check_is_post_type_allowed( $post_type ) { |
|
645 | + return true; |
|
646 | + } |
|
647 | + |
|
648 | + /** |
|
649 | + * Prepares a single item for create or update. |
|
650 | + * |
|
651 | + * @since 1.0.13 |
|
652 | + * |
|
653 | + * @param WP_REST_Request $request Request object. |
|
654 | + * @return array|WP_Error Discount Properties or WP_Error. |
|
655 | + */ |
|
656 | + protected function prepare_item_for_database( $request ) { |
|
657 | + $prepared_item = new stdClass(); |
|
658 | + $prepared_item->meta_input = array(); |
|
659 | + |
|
660 | + // Post ID. |
|
661 | + if ( isset( $request['id'] ) ) { |
|
662 | + $existing_item = $this->get_post( $request['id'] ); |
|
663 | + if ( is_wp_error( $existing_item ) ) { |
|
664 | + return $existing_item; |
|
665 | + } |
|
666 | + |
|
667 | + $prepared_item->ID = $existing_item->ID; |
|
668 | + } |
|
669 | + |
|
670 | + $schema = $this->get_item_schema(); |
|
671 | + |
|
672 | + // item title. |
|
673 | + if ( ! empty( $schema['properties']['title'] ) && isset( $request['title'] ) ) { |
|
674 | + $prepared_item->post_title = sanitize_text_field( $request['title'] ); |
|
675 | + } |
|
676 | + |
|
677 | + // item status. |
|
678 | + if ( ! empty( $schema['properties']['status'] ) && isset( $request['status'] ) && in_array( $request['status'], array_keys( get_post_stati( array( 'internal' => false ) ) ) ) ) { |
|
679 | + $prepared_item->post_status = sanitize_text_field( $request['status'] ); |
|
680 | + } |
|
681 | + |
|
682 | + // Code. |
|
683 | + if ( ! empty( $schema['properties']['code'] ) && isset( $request['code'] ) ) { |
|
684 | + $prepared_item->meta_input['_wpi_discount_code'] = trim( $request['code'] ); |
|
685 | + } |
|
686 | + |
|
687 | + // Type. |
|
688 | + if ( ! empty( $schema['properties']['type'] ) && isset( $request['type'] ) && in_array( $request['type'], array_keys( wpinv_get_discount_types() ) ) ) { |
|
689 | + $prepared_item->meta_input['_wpi_discount_type'] = trim( $request['type'] ); |
|
690 | + } |
|
691 | + |
|
692 | + // Amount. |
|
693 | + if ( ! empty( $schema['properties']['amount'] ) && isset( $request['amount'] ) ) { |
|
694 | + $prepared_item->meta_input['_wpi_discount_amount'] = floatval( $request['amount'] ); |
|
695 | + } |
|
696 | + |
|
697 | + // Items. |
|
698 | + if ( ! empty( $schema['properties']['items'] ) && isset( $request['items'] ) ) { |
|
699 | + $prepared_item->meta_input['_wpi_discount_items'] = wpinv_parse_list( $request['items'] ); |
|
700 | + } |
|
701 | + |
|
702 | + // Excluded Items. |
|
703 | + if ( ! empty( $schema['properties']['exclude_items'] ) && isset( $request['exclude_items'] ) ) { |
|
704 | + $prepared_item->meta_input['_wpi_discount_excluded_items'] = wpinv_parse_list( $request['exclude_items'] ); |
|
705 | + } |
|
706 | + |
|
707 | + // Start date. |
|
708 | + if ( ! empty( $schema['properties']['start_date'] ) && isset( $request['start_date'] ) ) { |
|
709 | + $prepared_item->meta_input['_wpi_discount_start'] = trim( $request['start_date'] ); |
|
710 | + } |
|
711 | + |
|
712 | + // End date. |
|
713 | + if ( ! empty( $schema['properties']['end_date'] ) && isset( $request['end_date'] ) ) { |
|
714 | + $prepared_item->meta_input['_wpi_discount_expiration'] = trim( $request['end_date'] ); |
|
715 | + } |
|
716 | + |
|
717 | + // Minimum amount. |
|
718 | + if ( ! empty( $schema['properties']['minimum_amount'] ) && isset( $request['minimum_amount'] ) ) { |
|
719 | + $prepared_item->meta_input['_wpi_discount_min_total'] = floatval( $request['minimum_amount'] ); |
|
720 | + } |
|
721 | + |
|
722 | + // Maximum amount. |
|
723 | + if ( ! empty( $schema['properties']['maximum_amount'] ) && isset( $request['maximum_amount'] ) ) { |
|
724 | + $prepared_item->meta_input['_wpi_discount_max_total'] = floatval( $request['maximum_amount'] ); |
|
725 | + } |
|
726 | + |
|
727 | + // Recurring. |
|
728 | + if ( ! empty( $schema['properties']['recurring'] ) && isset( $request['recurring'] ) ) { |
|
729 | + $prepared_item->meta_input['_wpi_discount_is_recurring'] = empty( (int) $request['recurring'] ) ? 0 : 1; |
|
730 | + } |
|
731 | + |
|
732 | + // Maximum uses. |
|
733 | + if ( ! empty( $schema['properties']['max_uses'] ) && isset( $request['max_uses'] ) ) { |
|
734 | + $prepared_item->meta_input['_wpi_discount_max_uses'] = intval( $request['max_uses'] ); |
|
735 | + } |
|
736 | + |
|
737 | + // Single use. |
|
738 | + if ( ! empty( $schema['properties']['single_use'] ) && isset( $request['single_use'] ) ) { |
|
739 | + $prepared_item->meta_input['_wpi_discount_is_single_use'] = empty( (int) $request['single_use'] ) ? 0 : 1; |
|
740 | + } |
|
741 | + |
|
742 | + $discount_data = (array) wp_unslash( $prepared_item ); |
|
743 | + |
|
744 | + /** |
|
745 | + * Filters an item before it is inserted via the REST API. |
|
746 | + * |
|
747 | + * @since 1.0.13 |
|
748 | + * |
|
749 | + * @param array $discount_data An array of discount data |
|
750 | + * @param WP_REST_Request $request Request object. |
|
751 | + */ |
|
752 | + return apply_filters( "wpinv_rest_pre_insert_discount", $discount_data, $request ); |
|
753 | + |
|
754 | + } |
|
755 | + |
|
756 | + /** |
|
757 | + * Prepares a single discount output for response. |
|
758 | + * |
|
759 | + * @since 1.0.13 |
|
760 | + * |
|
761 | + * @param WP_Post $discount WP_Post object. |
|
762 | + * @param WP_REST_Request $request Request object. |
|
763 | + * @return WP_REST_Response Response object. |
|
764 | + */ |
|
765 | + public function prepare_item_for_response( $discount, $request ) { |
|
766 | + |
|
767 | + $GLOBALS['post'] = get_post( $discount->ID ); |
|
768 | + |
|
769 | + setup_postdata( $discount->ID ); |
|
770 | + |
|
771 | + // Fetch the fields to include in this response. |
|
772 | + $fields = $this->get_fields_for_response( $request ); |
|
773 | + |
|
774 | + // Base fields for every discount. |
|
775 | + $data = array(); |
|
776 | + |
|
777 | + // Set up ID. |
|
778 | + if ( rest_is_field_included( 'id', $fields ) ) { |
|
779 | + $data['id'] = $discount->ID; |
|
780 | + } |
|
781 | + |
|
782 | + // Title. |
|
783 | + if ( rest_is_field_included( 'title', $fields ) ) { |
|
784 | + $data['title'] = get_the_title( $discount->ID ); |
|
785 | + } |
|
786 | + |
|
787 | + // Code. |
|
788 | + if ( rest_is_field_included( 'code', $fields ) ) { |
|
789 | + $data['code'] = wpinv_get_discount_code( $discount->ID ); |
|
790 | + } |
|
791 | + |
|
792 | + // Type. |
|
793 | + if ( rest_is_field_included( 'type', $fields ) ) { |
|
794 | + $data['type'] = wpinv_get_discount_type( $discount->ID ); |
|
795 | + } |
|
796 | + |
|
797 | + // Amount. |
|
798 | + if ( rest_is_field_included( 'amount', $fields ) ) { |
|
799 | + $data['amount'] = wpinv_get_discount_amount( $discount->ID ); |
|
800 | + } |
|
801 | + |
|
802 | + // Status. |
|
803 | + if ( rest_is_field_included( 'status', $fields ) ) { |
|
804 | + $data['status'] = get_post_status( $discount->ID ); |
|
805 | + } |
|
806 | + |
|
807 | + // Items. |
|
808 | + if ( rest_is_field_included( 'items', $fields ) ) { |
|
809 | + $data['items'] = wpinv_get_discount_item_reqs( $discount->ID ); |
|
810 | + } |
|
811 | + |
|
812 | + // Excluded Items. |
|
813 | + if ( rest_is_field_included( 'exclude_items', $fields ) ) { |
|
814 | + $data['exclude_items'] = wpinv_get_discount_excluded_items( $discount->ID ); |
|
815 | + } |
|
816 | + |
|
817 | + // Start date. |
|
818 | + if ( rest_is_field_included( 'start_date', $fields ) ) { |
|
819 | + $data['start_date'] = wpinv_get_discount_start_date( $discount->ID ); |
|
820 | + } |
|
821 | + |
|
822 | + // End date. |
|
823 | + if ( rest_is_field_included( 'end_date', $fields ) ) { |
|
824 | + $data['end_date'] = wpinv_get_discount_expiration( $discount->ID ); |
|
825 | + } |
|
826 | + |
|
827 | + // Minimum amount. |
|
828 | + if ( rest_is_field_included( 'minimum_amount', $fields ) ) { |
|
829 | + $data['minimum_amount'] = wpinv_get_discount_min_total( $discount->ID ); |
|
830 | + } |
|
831 | + |
|
832 | + // Maximum amount. |
|
833 | + if ( rest_is_field_included( 'maximum_amount', $fields ) ) { |
|
834 | + $data['maximum_amount'] = wpinv_get_discount_max_total( $discount->ID ); |
|
835 | + } |
|
836 | + |
|
837 | + // Recurring. |
|
838 | + if ( rest_is_field_included( 'recurring', $fields ) ) { |
|
839 | + $data['recurring'] = wpinv_discount_is_recurring( $discount->ID ); |
|
840 | + } |
|
841 | + |
|
842 | + // Maximum uses. |
|
843 | + if ( rest_is_field_included( 'max_uses', $fields ) ) { |
|
844 | + $data['max_uses'] = wpinv_get_discount_max_uses( $discount->ID ); |
|
845 | + } |
|
846 | + |
|
847 | + // Single use. |
|
848 | + if ( rest_is_field_included( 'single_use', $fields ) ) { |
|
849 | + $data['single_use'] = wpinv_discount_is_single_use( $discount->ID ); |
|
850 | + } |
|
851 | + |
|
852 | + $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
853 | + $data = $this->add_additional_fields_to_object( $data, $request ); |
|
854 | + $data = $this->filter_response_by_context( $data, $context ); |
|
855 | + |
|
856 | + // Wrap the data in a response object. |
|
857 | + $response = rest_ensure_response( $data ); |
|
858 | + |
|
859 | + $links = $this->prepare_links( $discount ); |
|
860 | + $response->add_links( $links ); |
|
861 | + |
|
862 | + if ( ! empty( $links['self']['href'] ) ) { |
|
863 | + $actions = $this->get_available_actions( $discount, $request ); |
|
864 | + |
|
865 | + $self = $links['self']['href']; |
|
866 | + |
|
867 | + foreach ( $actions as $rel ) { |
|
868 | + $response->add_link( $rel, $self ); |
|
869 | + } |
|
870 | + } |
|
871 | + |
|
872 | + /** |
|
873 | + * Filters the discount data for a response. |
|
874 | + * |
|
875 | + * @since 1.0.13 |
|
876 | + * |
|
877 | + * @param WP_REST_Response $response The response object. |
|
878 | + * @param WP_Post $discount The discount post object. |
|
879 | + * @param WP_REST_Request $request Request object. |
|
880 | + */ |
|
881 | + return apply_filters( "wpinv_rest_prepare_discount", $response, $discount, $request ); |
|
882 | + } |
|
883 | + |
|
884 | + /** |
|
885 | + * Gets an array of fields to be included on the response. |
|
886 | + * |
|
887 | + * Included fields are based on item schema and `_fields=` request argument. |
|
888 | + * |
|
889 | + * @since 1.0.13 |
|
890 | + * |
|
891 | + * @param WP_REST_Request $request Full details about the request. |
|
892 | + * @return array Fields to be included in the response. |
|
893 | + */ |
|
894 | + public function get_fields_for_response( $request ) { |
|
895 | + $schema = $this->get_item_schema(); |
|
896 | + $properties = isset( $schema['properties'] ) ? $schema['properties'] : array(); |
|
897 | + |
|
898 | + $additional_fields = $this->get_additional_fields(); |
|
899 | + foreach ( $additional_fields as $field_name => $field_options ) { |
|
900 | + // For back-compat, include any field with an empty schema |
|
901 | + // because it won't be present in $this->get_item_schema(). |
|
902 | + if ( is_null( $field_options['schema'] ) ) { |
|
903 | + $properties[ $field_name ] = $field_options; |
|
904 | + } |
|
905 | + } |
|
906 | + |
|
907 | + // Exclude fields that specify a different context than the request context. |
|
908 | + $context = $request['context']; |
|
909 | + if ( $context ) { |
|
910 | + foreach ( $properties as $name => $options ) { |
|
911 | + if ( ! empty( $options['context'] ) && ! in_array( $context, $options['context'], true ) ) { |
|
912 | + unset( $properties[ $name ] ); |
|
913 | + } |
|
914 | + } |
|
915 | + } |
|
916 | + |
|
917 | + $fields = array_keys( $properties ); |
|
918 | + |
|
919 | + if ( ! isset( $request['_fields'] ) ) { |
|
920 | + return $fields; |
|
921 | + } |
|
922 | + $requested_fields = wpinv_parse_list( $request['_fields'] ); |
|
923 | + if ( 0 === count( $requested_fields ) ) { |
|
924 | + return $fields; |
|
925 | + } |
|
926 | + // Trim off outside whitespace from the comma delimited list. |
|
927 | + $requested_fields = array_map( 'trim', $requested_fields ); |
|
928 | + // Always persist 'id', because it can be needed for add_additional_fields_to_object(). |
|
929 | + if ( in_array( 'id', $fields, true ) ) { |
|
930 | + $requested_fields[] = 'id'; |
|
931 | + } |
|
932 | + // Return the list of all requested fields which appear in the schema. |
|
933 | + return array_reduce( |
|
934 | + $requested_fields, |
|
935 | + function( $response_fields, $field ) use ( $fields ) { |
|
936 | + if ( in_array( $field, $fields, true ) ) { |
|
937 | + $response_fields[] = $field; |
|
938 | + return $response_fields; |
|
939 | + } |
|
940 | + // Check for nested fields if $field is not a direct match. |
|
941 | + $nested_fields = explode( '.', $field ); |
|
942 | + // A nested field is included so long as its top-level property is |
|
943 | + // present in the schema. |
|
944 | + if ( in_array( $nested_fields[0], $fields, true ) ) { |
|
945 | + $response_fields[] = $field; |
|
946 | + } |
|
947 | + return $response_fields; |
|
948 | + }, |
|
949 | + array() |
|
950 | + ); |
|
951 | + } |
|
952 | + |
|
953 | + /** |
|
954 | + * Retrieves the discount's schema, conforming to JSON Schema. |
|
955 | + * |
|
956 | + * @since 1.0.13 |
|
957 | + * |
|
958 | + * @return array Discount schema data. |
|
959 | + */ |
|
960 | + public function get_item_schema() { |
|
961 | + |
|
962 | + // Maybe retrieve the schema from cache. |
|
963 | + if ( empty( $this->schema ) ) { |
|
964 | + return $this->add_additional_fields_schema( $this->schema ); |
|
965 | + } |
|
966 | + |
|
967 | + $schema = array( |
|
968 | + '$schema' => 'http://json-schema.org/draft-04/schema#', |
|
969 | + 'title' => $this->post_type, |
|
970 | + 'type' => 'object', |
|
971 | + |
|
972 | + // Base properties for every Item. |
|
973 | + 'properties' => array( |
|
974 | + |
|
975 | + 'id' => array( |
|
976 | + 'description' => __( 'Unique identifier for the discount.', 'invoicing' ), |
|
977 | + 'type' => 'integer', |
|
978 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
979 | + 'readonly' => true, |
|
980 | + ), |
|
981 | + |
|
982 | + 'title' => array( |
|
983 | + 'description' => __( 'The title for the discount.', 'invoicing' ), |
|
984 | + 'type' => 'string', |
|
985 | + 'context' => array( 'view', 'edit' ), |
|
986 | + ), |
|
987 | + |
|
988 | + 'code' => array( |
|
989 | + 'description' => __( 'The discount code.', 'invoicing' ), |
|
990 | + 'type' => 'string', |
|
991 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
992 | + 'required' => true, |
|
993 | + ), |
|
994 | + |
|
995 | + 'type' => array( |
|
996 | + 'description' => __( 'The type of discount.', 'invoicing' ), |
|
997 | + 'type' => 'string', |
|
998 | + 'enum' => array_keys( wpinv_get_discount_types() ), |
|
999 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1000 | + 'default' => 'percentage', |
|
1001 | + ), |
|
1002 | + |
|
1003 | + 'amount' => array( |
|
1004 | + 'description' => __( 'The discount value.', 'invoicing' ), |
|
1005 | + 'type' => 'number', |
|
1006 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1007 | + 'required' => true, |
|
1008 | + ), |
|
1009 | + |
|
1010 | + 'status' => array( |
|
1011 | + 'description' => __( 'A named status for the discount.', 'invoicing' ), |
|
1012 | + 'type' => 'string', |
|
1013 | + 'enum' => array_keys( get_post_stati( array( 'internal' => false ) ) ), |
|
1014 | + 'context' => array( 'view', 'edit' ), |
|
1015 | + ), |
|
1016 | + |
|
1017 | + 'items' => array( |
|
1018 | + 'description' => __( 'Items which need to be in the cart to use this discount or, for "Item Discounts", which items are discounted. If left blank, this discount will be used on any item.', 'invoicing' ), |
|
1019 | + 'type' => 'array', |
|
1020 | + 'context' => array( 'view', 'edit' ), |
|
1021 | + ), |
|
1022 | + |
|
1023 | + 'exclude_items' => array( |
|
1024 | + 'description' => __( 'Items which are NOT allowed to use this discount.', 'invoicing' ), |
|
1025 | + 'type' => 'array', |
|
1026 | + 'context' => array( 'view', 'edit' ), |
|
1027 | + ), |
|
1028 | + |
|
1029 | + 'start_date' => array( |
|
1030 | + 'description' => __( 'The start date for the discount in the format of yyyy-mm-dd hh:mm:ss . If provided, the discount can only be used after or on this date.', 'invoicing' ), |
|
1031 | + 'type' => 'string', |
|
1032 | + 'context' => array( 'view', 'edit' ), |
|
1033 | + ), |
|
1034 | + |
|
1035 | + 'end_date' => array( |
|
1036 | + 'description' => __( 'The expiration date for the discount.', 'invoicing' ), |
|
1037 | + 'type' => 'string', |
|
1038 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1039 | + ), |
|
1040 | 1040 | |
1041 | - 'minimum_amount' => array( |
|
1042 | - 'description' => __( 'Minimum amount needed to use this invoice.', 'invoicing' ), |
|
1043 | - 'type' => 'number', |
|
1044 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1045 | - ), |
|
1046 | - |
|
1047 | - 'maximum_amount' => array( |
|
1048 | - 'description' => __( 'Maximum amount needed to use this invoice.', 'invoicing' ), |
|
1049 | - 'type' => 'number', |
|
1050 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1051 | - ), |
|
1052 | - |
|
1053 | - 'recurring' => array( |
|
1054 | - 'description' => __( 'Whether the discount is applied to all recurring payments or only the first recurring payment.', 'invoicing' ), |
|
1055 | - 'type' => 'integer', |
|
1056 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1057 | - ), |
|
1058 | - |
|
1059 | - 'max_uses' => array( |
|
1060 | - 'description' => __( 'The maximum number of times this discount code can be used.', 'invoicing' ), |
|
1061 | - 'type' => 'number', |
|
1062 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1063 | - ), |
|
1064 | - |
|
1065 | - 'single_use' => array( |
|
1066 | - 'description' => __( 'Whether or not this discount can only be used once per user.', 'invoicing' ), |
|
1067 | - 'type' => 'integer', |
|
1068 | - 'context' => array( 'view', 'edit', 'embed' ), |
|
1069 | - ) |
|
1070 | - |
|
1071 | - ), |
|
1072 | - ); |
|
1073 | - |
|
1074 | - // Add helpful links to the discount schem. |
|
1075 | - $schema['links'] = $this->get_schema_links(); |
|
1076 | - |
|
1077 | - /** |
|
1078 | - * Filters the discount schema for the REST API. |
|
1079 | - * |
|
1080 | - * Enables adding extra properties to discounts. |
|
1081 | - * |
|
1082 | - * @since 1.0.13 |
|
1083 | - * |
|
1084 | - * @param array $schema The discount schema. |
|
1085 | - */ |
|
1041 | + 'minimum_amount' => array( |
|
1042 | + 'description' => __( 'Minimum amount needed to use this invoice.', 'invoicing' ), |
|
1043 | + 'type' => 'number', |
|
1044 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1045 | + ), |
|
1046 | + |
|
1047 | + 'maximum_amount' => array( |
|
1048 | + 'description' => __( 'Maximum amount needed to use this invoice.', 'invoicing' ), |
|
1049 | + 'type' => 'number', |
|
1050 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1051 | + ), |
|
1052 | + |
|
1053 | + 'recurring' => array( |
|
1054 | + 'description' => __( 'Whether the discount is applied to all recurring payments or only the first recurring payment.', 'invoicing' ), |
|
1055 | + 'type' => 'integer', |
|
1056 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1057 | + ), |
|
1058 | + |
|
1059 | + 'max_uses' => array( |
|
1060 | + 'description' => __( 'The maximum number of times this discount code can be used.', 'invoicing' ), |
|
1061 | + 'type' => 'number', |
|
1062 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1063 | + ), |
|
1064 | + |
|
1065 | + 'single_use' => array( |
|
1066 | + 'description' => __( 'Whether or not this discount can only be used once per user.', 'invoicing' ), |
|
1067 | + 'type' => 'integer', |
|
1068 | + 'context' => array( 'view', 'edit', 'embed' ), |
|
1069 | + ) |
|
1070 | + |
|
1071 | + ), |
|
1072 | + ); |
|
1073 | + |
|
1074 | + // Add helpful links to the discount schem. |
|
1075 | + $schema['links'] = $this->get_schema_links(); |
|
1076 | + |
|
1077 | + /** |
|
1078 | + * Filters the discount schema for the REST API. |
|
1079 | + * |
|
1080 | + * Enables adding extra properties to discounts. |
|
1081 | + * |
|
1082 | + * @since 1.0.13 |
|
1083 | + * |
|
1084 | + * @param array $schema The discount schema. |
|
1085 | + */ |
|
1086 | 1086 | $schema = apply_filters( "wpinv_rest_discount_schema", $schema ); |
1087 | 1087 | |
1088 | - // Cache the discount schema. |
|
1089 | - $this->schema = $schema; |
|
1088 | + // Cache the discount schema. |
|
1089 | + $this->schema = $schema; |
|
1090 | 1090 | |
1091 | - return $this->add_additional_fields_schema( $this->schema ); |
|
1092 | - } |
|
1093 | - |
|
1094 | - /** |
|
1095 | - * Retrieve Link Description Objects that should be added to the Schema for the discounts collection. |
|
1096 | - * |
|
1097 | - * @since 1.0.13 |
|
1098 | - * |
|
1099 | - * @return array |
|
1100 | - */ |
|
1101 | - protected function get_schema_links() { |
|
1102 | - |
|
1103 | - $href = rest_url( "{$this->namespace}/{$this->rest_base}/{id}" ); |
|
1104 | - |
|
1105 | - $links = array(); |
|
1106 | - |
|
1107 | - $links[] = array( |
|
1108 | - 'rel' => 'https://api.w.org/action-publish', |
|
1109 | - 'title' => __( 'The current user can publish this discount.', 'invoicing' ), |
|
1110 | - 'href' => $href, |
|
1111 | - 'targetSchema' => array( |
|
1112 | - 'type' => 'object', |
|
1113 | - 'properties' => array( |
|
1114 | - 'status' => array( |
|
1115 | - 'type' => 'string', |
|
1116 | - 'enum' => array( 'publish', 'future' ), |
|
1117 | - ), |
|
1118 | - ), |
|
1119 | - ), |
|
1120 | - ); |
|
1121 | - |
|
1122 | - return $links; |
|
1123 | - } |
|
1124 | - |
|
1125 | - /** |
|
1126 | - * Prepares links for the request. |
|
1127 | - * |
|
1128 | - * @since 1.0.13 |
|
1129 | - * |
|
1130 | - * @param WP_Post $discount Post Object. |
|
1131 | - * @return array Links for the given discount. |
|
1132 | - */ |
|
1133 | - protected function prepare_links( $discount ) { |
|
1134 | - |
|
1135 | - // Prepare the base REST API endpoint for discounts. |
|
1136 | - $base = sprintf( '%s/%s', $this->namespace, $this->rest_base ); |
|
1137 | - |
|
1138 | - // Entity meta. |
|
1139 | - $links = array( |
|
1140 | - 'self' => array( |
|
1141 | - 'href' => rest_url( trailingslashit( $base ) . $discount->ID ), |
|
1142 | - ), |
|
1143 | - 'collection' => array( |
|
1144 | - 'href' => rest_url( $base ), |
|
1145 | - ), |
|
1146 | - ); |
|
1147 | - |
|
1148 | - /** |
|
1149 | - * Filters the returned discount links for the REST API. |
|
1150 | - * |
|
1151 | - * Enables adding extra links to discount API responses. |
|
1152 | - * |
|
1153 | - * @since 1.0.13 |
|
1154 | - * |
|
1155 | - * @param array $links Rest links. |
|
1156 | - */ |
|
1157 | - return apply_filters( "wpinv_rest_discount_links", $links ); |
|
1158 | - |
|
1159 | - } |
|
1160 | - |
|
1161 | - /** |
|
1162 | - * Get the link relations available for the post and current user. |
|
1163 | - * |
|
1164 | - * @since 1.0.13 |
|
1165 | - * |
|
1166 | - * @param WP_Post $discount WP_Post object. |
|
1167 | - * @param WP_REST_Request $request Request object. |
|
1168 | - * @return array List of link relations. |
|
1169 | - */ |
|
1170 | - protected function get_available_actions( $discount, $request ) { |
|
1171 | - |
|
1172 | - if ( 'edit' !== $request['context'] ) { |
|
1173 | - return array(); |
|
1174 | - } |
|
1175 | - |
|
1176 | - $rels = array(); |
|
1177 | - |
|
1178 | - // Retrieve the post type object. |
|
1179 | - $post_type = get_post_type_object( $discount->post_type ); |
|
1180 | - |
|
1181 | - // Mark discount as published. |
|
1182 | - if ( current_user_can( $post_type->cap->publish_posts ) ) { |
|
1183 | - $rels[] = 'https://api.w.org/action-publish'; |
|
1184 | - } |
|
1185 | - |
|
1186 | - /** |
|
1187 | - * Filters the available discount link relations for the REST API. |
|
1188 | - * |
|
1189 | - * Enables adding extra link relation for the current user and request to discount responses. |
|
1190 | - * |
|
1191 | - * @since 1.0.13 |
|
1192 | - * |
|
1193 | - * @param array $rels Available link relations. |
|
1194 | - */ |
|
1195 | - return apply_filters( "wpinv_rest_discount_link_relations", $rels ); |
|
1196 | - } |
|
1197 | - |
|
1198 | - /** |
|
1199 | - * Handles rest requests for discount types. |
|
1200 | - * |
|
1201 | - * @since 1.0.13 |
|
1202 | - * |
|
1203 | - * |
|
1204 | - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
1205 | - */ |
|
1206 | - public function get_discount_types() { |
|
1207 | - return rest_ensure_response( wpinv_get_discount_types() ); |
|
1208 | - } |
|
1091 | + return $this->add_additional_fields_schema( $this->schema ); |
|
1092 | + } |
|
1093 | + |
|
1094 | + /** |
|
1095 | + * Retrieve Link Description Objects that should be added to the Schema for the discounts collection. |
|
1096 | + * |
|
1097 | + * @since 1.0.13 |
|
1098 | + * |
|
1099 | + * @return array |
|
1100 | + */ |
|
1101 | + protected function get_schema_links() { |
|
1102 | + |
|
1103 | + $href = rest_url( "{$this->namespace}/{$this->rest_base}/{id}" ); |
|
1104 | + |
|
1105 | + $links = array(); |
|
1106 | + |
|
1107 | + $links[] = array( |
|
1108 | + 'rel' => 'https://api.w.org/action-publish', |
|
1109 | + 'title' => __( 'The current user can publish this discount.', 'invoicing' ), |
|
1110 | + 'href' => $href, |
|
1111 | + 'targetSchema' => array( |
|
1112 | + 'type' => 'object', |
|
1113 | + 'properties' => array( |
|
1114 | + 'status' => array( |
|
1115 | + 'type' => 'string', |
|
1116 | + 'enum' => array( 'publish', 'future' ), |
|
1117 | + ), |
|
1118 | + ), |
|
1119 | + ), |
|
1120 | + ); |
|
1121 | + |
|
1122 | + return $links; |
|
1123 | + } |
|
1124 | + |
|
1125 | + /** |
|
1126 | + * Prepares links for the request. |
|
1127 | + * |
|
1128 | + * @since 1.0.13 |
|
1129 | + * |
|
1130 | + * @param WP_Post $discount Post Object. |
|
1131 | + * @return array Links for the given discount. |
|
1132 | + */ |
|
1133 | + protected function prepare_links( $discount ) { |
|
1134 | + |
|
1135 | + // Prepare the base REST API endpoint for discounts. |
|
1136 | + $base = sprintf( '%s/%s', $this->namespace, $this->rest_base ); |
|
1137 | + |
|
1138 | + // Entity meta. |
|
1139 | + $links = array( |
|
1140 | + 'self' => array( |
|
1141 | + 'href' => rest_url( trailingslashit( $base ) . $discount->ID ), |
|
1142 | + ), |
|
1143 | + 'collection' => array( |
|
1144 | + 'href' => rest_url( $base ), |
|
1145 | + ), |
|
1146 | + ); |
|
1147 | + |
|
1148 | + /** |
|
1149 | + * Filters the returned discount links for the REST API. |
|
1150 | + * |
|
1151 | + * Enables adding extra links to discount API responses. |
|
1152 | + * |
|
1153 | + * @since 1.0.13 |
|
1154 | + * |
|
1155 | + * @param array $links Rest links. |
|
1156 | + */ |
|
1157 | + return apply_filters( "wpinv_rest_discount_links", $links ); |
|
1158 | + |
|
1159 | + } |
|
1160 | + |
|
1161 | + /** |
|
1162 | + * Get the link relations available for the post and current user. |
|
1163 | + * |
|
1164 | + * @since 1.0.13 |
|
1165 | + * |
|
1166 | + * @param WP_Post $discount WP_Post object. |
|
1167 | + * @param WP_REST_Request $request Request object. |
|
1168 | + * @return array List of link relations. |
|
1169 | + */ |
|
1170 | + protected function get_available_actions( $discount, $request ) { |
|
1171 | + |
|
1172 | + if ( 'edit' !== $request['context'] ) { |
|
1173 | + return array(); |
|
1174 | + } |
|
1175 | + |
|
1176 | + $rels = array(); |
|
1177 | + |
|
1178 | + // Retrieve the post type object. |
|
1179 | + $post_type = get_post_type_object( $discount->post_type ); |
|
1180 | + |
|
1181 | + // Mark discount as published. |
|
1182 | + if ( current_user_can( $post_type->cap->publish_posts ) ) { |
|
1183 | + $rels[] = 'https://api.w.org/action-publish'; |
|
1184 | + } |
|
1185 | + |
|
1186 | + /** |
|
1187 | + * Filters the available discount link relations for the REST API. |
|
1188 | + * |
|
1189 | + * Enables adding extra link relation for the current user and request to discount responses. |
|
1190 | + * |
|
1191 | + * @since 1.0.13 |
|
1192 | + * |
|
1193 | + * @param array $rels Available link relations. |
|
1194 | + */ |
|
1195 | + return apply_filters( "wpinv_rest_discount_link_relations", $rels ); |
|
1196 | + } |
|
1197 | + |
|
1198 | + /** |
|
1199 | + * Handles rest requests for discount types. |
|
1200 | + * |
|
1201 | + * @since 1.0.13 |
|
1202 | + * |
|
1203 | + * |
|
1204 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
1205 | + */ |
|
1206 | + public function get_discount_types() { |
|
1207 | + return rest_ensure_response( wpinv_get_discount_types() ); |
|
1208 | + } |
|
1209 | 1209 | |
1210 | 1210 | } |
1211 | 1211 | \ No newline at end of file |
@@ -1,243 +1,243 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | if ( ! defined( 'ABSPATH' ) ) { |
3 | - exit; |
|
3 | + exit; |
|
4 | 4 | } |
5 | 5 | |
6 | 6 | if ( ! class_exists( 'WP_Super_Duper' ) ) { |
7 | 7 | |
8 | 8 | |
9 | - /** |
|
10 | - * A Class to be able to create a Widget, Shortcode or Block to be able to output content for WordPress. |
|
11 | - * |
|
12 | - * Should not be called direct but extended instead. |
|
13 | - * |
|
14 | - * Class WP_Super_Duper |
|
15 | - * @since 1.0.16 change log moved to file change-log.txt - CHANGED |
|
16 | - * @ver 1.0.16 |
|
17 | - */ |
|
18 | - class WP_Super_Duper extends WP_Widget { |
|
19 | - |
|
20 | - public $version = "1.0.16"; |
|
21 | - public $font_awesome_icon_version = "5.11.2"; |
|
22 | - public $block_code; |
|
23 | - public $options; |
|
24 | - public $base_id; |
|
25 | - public $arguments = array(); |
|
26 | - public $instance = array(); |
|
27 | - private $class_name; |
|
28 | - |
|
29 | - /** |
|
30 | - * The relative url to the current folder. |
|
31 | - * |
|
32 | - * @var string |
|
33 | - */ |
|
34 | - public $url = ''; |
|
35 | - |
|
36 | - /** |
|
37 | - * Take the array options and use them to build. |
|
38 | - */ |
|
39 | - public function __construct( $options ) { |
|
40 | - global $sd_widgets; |
|
41 | - |
|
42 | - $sd_widgets[ $options['base_id'] ] = array( |
|
43 | - 'name' => $options['name'], |
|
44 | - 'class_name' => $options['class_name'] |
|
45 | - ); |
|
46 | - $this->base_id = $options['base_id']; |
|
47 | - // lets filter the options before we do anything |
|
48 | - $options = apply_filters( "wp_super_duper_options", $options ); |
|
49 | - $options = apply_filters( "wp_super_duper_options_{$this->base_id}", $options ); |
|
50 | - $options = $this->add_name_from_key( $options ); |
|
51 | - $this->options = $options; |
|
52 | - |
|
53 | - $this->base_id = $options['base_id']; |
|
54 | - $this->arguments = isset( $options['arguments'] ) ? $options['arguments'] : array(); |
|
55 | - |
|
56 | - // init parent |
|
57 | - parent::__construct( $options['base_id'], $options['name'], $options['widget_ops'] ); |
|
58 | - |
|
59 | - if ( isset( $options['class_name'] ) ) { |
|
60 | - // register widget |
|
61 | - $this->class_name = $options['class_name']; |
|
62 | - |
|
63 | - // register shortcode |
|
64 | - $this->register_shortcode(); |
|
65 | - |
|
66 | - // Fusion Builder (avada) support |
|
67 | - if( function_exists('fusion_builder_map') ){ $this->register_fusion_element(); } |
|
68 | - |
|
69 | - // register block |
|
70 | - add_action( 'admin_enqueue_scripts', array( $this, 'register_block' ) ); |
|
71 | - } |
|
72 | - |
|
73 | - // add the CSS and JS we need ONCE |
|
74 | - global $sd_widget_scripts; |
|
75 | - |
|
76 | - if ( ! $sd_widget_scripts ) { |
|
77 | - wp_add_inline_script( 'admin-widgets', $this->widget_js() ); |
|
78 | - wp_add_inline_script( 'customize-controls', $this->widget_js() ); |
|
79 | - wp_add_inline_style( 'widgets', $this->widget_css() ); |
|
80 | - |
|
81 | - $sd_widget_scripts = true; |
|
82 | - |
|
83 | - // add shortcode insert button once |
|
84 | - add_action( 'media_buttons', array( $this, 'shortcode_insert_button' ) ); |
|
85 | - // generatepress theme sections compatibility |
|
86 | - if ( function_exists( 'generate_sections_sections_metabox' ) ) { |
|
87 | - add_action( 'generate_sections_metabox', array( $this, 'shortcode_insert_button_script' ) ); |
|
88 | - } |
|
89 | - if ( $this->is_preview() ) { |
|
90 | - add_action( 'wp_footer', array( $this, 'shortcode_insert_button_script' ) ); |
|
91 | - // this makes the insert button work for elementor |
|
92 | - add_action( 'elementor/editor/after_enqueue_scripts', array( |
|
93 | - $this, |
|
94 | - 'shortcode_insert_button_script' |
|
95 | - ) ); // for elementor |
|
96 | - } |
|
97 | - // this makes the insert button work for cornerstone |
|
98 | - add_action('wp_print_footer_scripts',array( __CLASS__, 'maybe_cornerstone_builder' )); |
|
99 | - |
|
100 | - add_action( 'wp_ajax_super_duper_get_widget_settings', array( __CLASS__, 'get_widget_settings' ) ); |
|
101 | - add_action( 'wp_ajax_super_duper_get_picker', array( __CLASS__, 'get_picker' ) ); |
|
102 | - |
|
103 | - // add generator text to admin head |
|
104 | - add_action( 'admin_head', array( $this, 'generator' ) ); |
|
105 | - } |
|
106 | - |
|
107 | - do_action( 'wp_super_duper_widget_init', $options, $this ); |
|
108 | - } |
|
109 | - |
|
110 | - public function register_fusion_element(){ |
|
111 | - |
|
112 | - $options = $this->options; |
|
113 | - |
|
114 | - if($this->base_id){ |
|
115 | - |
|
116 | - $params = $this->get_fusion_params(); |
|
117 | - |
|
118 | - $args = array( |
|
119 | - 'name' => $options['name'], |
|
120 | - 'shortcode' => $this->base_id, |
|
121 | - 'icon' => $options['block-icon'] ? $options['block-icon'] : 'far fa-square', |
|
122 | - 'allow_generator' => true, |
|
123 | - ); |
|
124 | - |
|
125 | - if(!empty($params)){ |
|
126 | - $args['params'] = $params; |
|
127 | - } |
|
128 | - |
|
129 | - fusion_builder_map($args); |
|
130 | - } |
|
131 | - |
|
132 | - } |
|
133 | - |
|
134 | - public function get_fusion_params(){ |
|
135 | - $params = array(); |
|
136 | - $arguments = $this->get_arguments(); |
|
137 | - |
|
138 | - if(!empty($arguments)){ |
|
139 | - foreach($arguments as $key => $val){ |
|
140 | - $param = array(); |
|
141 | - // type |
|
142 | - $param['type'] = str_replace( |
|
143 | - array( |
|
144 | - "text", |
|
145 | - "number", |
|
146 | - "email", |
|
147 | - "color", |
|
148 | - "checkbox" |
|
149 | - ), |
|
150 | - array( |
|
151 | - "textfield", |
|
152 | - "textfield", |
|
153 | - "textfield", |
|
154 | - "colorpicker", |
|
155 | - "select", |
|
156 | - |
|
157 | - ), |
|
158 | - $val['type']); |
|
159 | - |
|
160 | - // heading |
|
161 | - $param['heading'] = $val['title']; |
|
162 | - |
|
163 | - // description |
|
164 | - $param['description'] = isset($val['desc']) ? $val['desc'] : ''; |
|
165 | - |
|
166 | - // param_name |
|
167 | - $param['param_name'] = $key; |
|
168 | - |
|
169 | - // Default |
|
170 | - $param['default'] = isset($val['default']) ? $val['default'] : ''; |
|
171 | - |
|
172 | - // Group |
|
173 | - if(isset($val['group'])){ |
|
174 | - $param['group'] = $val['group']; |
|
175 | - } |
|
176 | - |
|
177 | - // value |
|
178 | - if($val['type'] == 'checkbox'){ |
|
179 | - if(isset($val['default']) && $val['default'] == '0'){ |
|
180 | - unset($param['default']); |
|
181 | - } |
|
182 | - $param['value'] = array(''=>__("No"),'1'=>__("Yes")); |
|
183 | - }elseif($param['type'] == 'select'){ |
|
184 | - $param['value'] = isset($val['options']) ? $val['options'] : array(); |
|
185 | - }else{ |
|
186 | - $param['value'] = isset($val['default']) ? $val['default'] : ''; |
|
187 | - } |
|
188 | - |
|
189 | - // setup the param |
|
190 | - $params[] = $param; |
|
191 | - |
|
192 | - } |
|
193 | - } |
|
194 | - |
|
195 | - |
|
196 | - return $params; |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * Maybe insert the shortcode inserter button in the footer if we are in the cornerstone builder |
|
201 | - */ |
|
202 | - public static function maybe_cornerstone_builder(){ |
|
203 | - if(did_action('cornerstone_before_boot_app')){ |
|
204 | - self::shortcode_insert_button_script(); |
|
205 | - } |
|
206 | - } |
|
207 | - |
|
208 | - /** |
|
209 | - * A function to ge the shortcode builder picker html. |
|
210 | - * |
|
211 | - * @param string $editor_id |
|
212 | - * |
|
213 | - * @return string |
|
214 | - */ |
|
215 | - public static function get_picker( $editor_id = '' ) { |
|
216 | - |
|
217 | - ob_start(); |
|
218 | - if ( isset( $_POST['editor_id'] ) ) { |
|
219 | - $editor_id = esc_attr( $_POST['editor_id'] ); |
|
220 | - } elseif ( isset( $_REQUEST['et_fb'] ) ) { |
|
221 | - $editor_id = 'main_content_content_vb_tiny_mce'; |
|
222 | - } |
|
223 | - |
|
224 | - global $sd_widgets; |
|
225 | - ?> |
|
9 | + /** |
|
10 | + * A Class to be able to create a Widget, Shortcode or Block to be able to output content for WordPress. |
|
11 | + * |
|
12 | + * Should not be called direct but extended instead. |
|
13 | + * |
|
14 | + * Class WP_Super_Duper |
|
15 | + * @since 1.0.16 change log moved to file change-log.txt - CHANGED |
|
16 | + * @ver 1.0.16 |
|
17 | + */ |
|
18 | + class WP_Super_Duper extends WP_Widget { |
|
19 | + |
|
20 | + public $version = "1.0.16"; |
|
21 | + public $font_awesome_icon_version = "5.11.2"; |
|
22 | + public $block_code; |
|
23 | + public $options; |
|
24 | + public $base_id; |
|
25 | + public $arguments = array(); |
|
26 | + public $instance = array(); |
|
27 | + private $class_name; |
|
28 | + |
|
29 | + /** |
|
30 | + * The relative url to the current folder. |
|
31 | + * |
|
32 | + * @var string |
|
33 | + */ |
|
34 | + public $url = ''; |
|
35 | + |
|
36 | + /** |
|
37 | + * Take the array options and use them to build. |
|
38 | + */ |
|
39 | + public function __construct( $options ) { |
|
40 | + global $sd_widgets; |
|
41 | + |
|
42 | + $sd_widgets[ $options['base_id'] ] = array( |
|
43 | + 'name' => $options['name'], |
|
44 | + 'class_name' => $options['class_name'] |
|
45 | + ); |
|
46 | + $this->base_id = $options['base_id']; |
|
47 | + // lets filter the options before we do anything |
|
48 | + $options = apply_filters( "wp_super_duper_options", $options ); |
|
49 | + $options = apply_filters( "wp_super_duper_options_{$this->base_id}", $options ); |
|
50 | + $options = $this->add_name_from_key( $options ); |
|
51 | + $this->options = $options; |
|
52 | + |
|
53 | + $this->base_id = $options['base_id']; |
|
54 | + $this->arguments = isset( $options['arguments'] ) ? $options['arguments'] : array(); |
|
55 | + |
|
56 | + // init parent |
|
57 | + parent::__construct( $options['base_id'], $options['name'], $options['widget_ops'] ); |
|
58 | + |
|
59 | + if ( isset( $options['class_name'] ) ) { |
|
60 | + // register widget |
|
61 | + $this->class_name = $options['class_name']; |
|
62 | + |
|
63 | + // register shortcode |
|
64 | + $this->register_shortcode(); |
|
65 | + |
|
66 | + // Fusion Builder (avada) support |
|
67 | + if( function_exists('fusion_builder_map') ){ $this->register_fusion_element(); } |
|
68 | + |
|
69 | + // register block |
|
70 | + add_action( 'admin_enqueue_scripts', array( $this, 'register_block' ) ); |
|
71 | + } |
|
72 | + |
|
73 | + // add the CSS and JS we need ONCE |
|
74 | + global $sd_widget_scripts; |
|
75 | + |
|
76 | + if ( ! $sd_widget_scripts ) { |
|
77 | + wp_add_inline_script( 'admin-widgets', $this->widget_js() ); |
|
78 | + wp_add_inline_script( 'customize-controls', $this->widget_js() ); |
|
79 | + wp_add_inline_style( 'widgets', $this->widget_css() ); |
|
80 | + |
|
81 | + $sd_widget_scripts = true; |
|
82 | + |
|
83 | + // add shortcode insert button once |
|
84 | + add_action( 'media_buttons', array( $this, 'shortcode_insert_button' ) ); |
|
85 | + // generatepress theme sections compatibility |
|
86 | + if ( function_exists( 'generate_sections_sections_metabox' ) ) { |
|
87 | + add_action( 'generate_sections_metabox', array( $this, 'shortcode_insert_button_script' ) ); |
|
88 | + } |
|
89 | + if ( $this->is_preview() ) { |
|
90 | + add_action( 'wp_footer', array( $this, 'shortcode_insert_button_script' ) ); |
|
91 | + // this makes the insert button work for elementor |
|
92 | + add_action( 'elementor/editor/after_enqueue_scripts', array( |
|
93 | + $this, |
|
94 | + 'shortcode_insert_button_script' |
|
95 | + ) ); // for elementor |
|
96 | + } |
|
97 | + // this makes the insert button work for cornerstone |
|
98 | + add_action('wp_print_footer_scripts',array( __CLASS__, 'maybe_cornerstone_builder' )); |
|
99 | + |
|
100 | + add_action( 'wp_ajax_super_duper_get_widget_settings', array( __CLASS__, 'get_widget_settings' ) ); |
|
101 | + add_action( 'wp_ajax_super_duper_get_picker', array( __CLASS__, 'get_picker' ) ); |
|
102 | + |
|
103 | + // add generator text to admin head |
|
104 | + add_action( 'admin_head', array( $this, 'generator' ) ); |
|
105 | + } |
|
106 | + |
|
107 | + do_action( 'wp_super_duper_widget_init', $options, $this ); |
|
108 | + } |
|
109 | + |
|
110 | + public function register_fusion_element(){ |
|
111 | + |
|
112 | + $options = $this->options; |
|
113 | + |
|
114 | + if($this->base_id){ |
|
115 | + |
|
116 | + $params = $this->get_fusion_params(); |
|
117 | + |
|
118 | + $args = array( |
|
119 | + 'name' => $options['name'], |
|
120 | + 'shortcode' => $this->base_id, |
|
121 | + 'icon' => $options['block-icon'] ? $options['block-icon'] : 'far fa-square', |
|
122 | + 'allow_generator' => true, |
|
123 | + ); |
|
124 | + |
|
125 | + if(!empty($params)){ |
|
126 | + $args['params'] = $params; |
|
127 | + } |
|
128 | + |
|
129 | + fusion_builder_map($args); |
|
130 | + } |
|
131 | + |
|
132 | + } |
|
133 | + |
|
134 | + public function get_fusion_params(){ |
|
135 | + $params = array(); |
|
136 | + $arguments = $this->get_arguments(); |
|
137 | + |
|
138 | + if(!empty($arguments)){ |
|
139 | + foreach($arguments as $key => $val){ |
|
140 | + $param = array(); |
|
141 | + // type |
|
142 | + $param['type'] = str_replace( |
|
143 | + array( |
|
144 | + "text", |
|
145 | + "number", |
|
146 | + "email", |
|
147 | + "color", |
|
148 | + "checkbox" |
|
149 | + ), |
|
150 | + array( |
|
151 | + "textfield", |
|
152 | + "textfield", |
|
153 | + "textfield", |
|
154 | + "colorpicker", |
|
155 | + "select", |
|
156 | + |
|
157 | + ), |
|
158 | + $val['type']); |
|
159 | + |
|
160 | + // heading |
|
161 | + $param['heading'] = $val['title']; |
|
162 | + |
|
163 | + // description |
|
164 | + $param['description'] = isset($val['desc']) ? $val['desc'] : ''; |
|
165 | + |
|
166 | + // param_name |
|
167 | + $param['param_name'] = $key; |
|
168 | + |
|
169 | + // Default |
|
170 | + $param['default'] = isset($val['default']) ? $val['default'] : ''; |
|
171 | + |
|
172 | + // Group |
|
173 | + if(isset($val['group'])){ |
|
174 | + $param['group'] = $val['group']; |
|
175 | + } |
|
176 | + |
|
177 | + // value |
|
178 | + if($val['type'] == 'checkbox'){ |
|
179 | + if(isset($val['default']) && $val['default'] == '0'){ |
|
180 | + unset($param['default']); |
|
181 | + } |
|
182 | + $param['value'] = array(''=>__("No"),'1'=>__("Yes")); |
|
183 | + }elseif($param['type'] == 'select'){ |
|
184 | + $param['value'] = isset($val['options']) ? $val['options'] : array(); |
|
185 | + }else{ |
|
186 | + $param['value'] = isset($val['default']) ? $val['default'] : ''; |
|
187 | + } |
|
188 | + |
|
189 | + // setup the param |
|
190 | + $params[] = $param; |
|
191 | + |
|
192 | + } |
|
193 | + } |
|
194 | + |
|
195 | + |
|
196 | + return $params; |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * Maybe insert the shortcode inserter button in the footer if we are in the cornerstone builder |
|
201 | + */ |
|
202 | + public static function maybe_cornerstone_builder(){ |
|
203 | + if(did_action('cornerstone_before_boot_app')){ |
|
204 | + self::shortcode_insert_button_script(); |
|
205 | + } |
|
206 | + } |
|
207 | + |
|
208 | + /** |
|
209 | + * A function to ge the shortcode builder picker html. |
|
210 | + * |
|
211 | + * @param string $editor_id |
|
212 | + * |
|
213 | + * @return string |
|
214 | + */ |
|
215 | + public static function get_picker( $editor_id = '' ) { |
|
216 | + |
|
217 | + ob_start(); |
|
218 | + if ( isset( $_POST['editor_id'] ) ) { |
|
219 | + $editor_id = esc_attr( $_POST['editor_id'] ); |
|
220 | + } elseif ( isset( $_REQUEST['et_fb'] ) ) { |
|
221 | + $editor_id = 'main_content_content_vb_tiny_mce'; |
|
222 | + } |
|
223 | + |
|
224 | + global $sd_widgets; |
|
225 | + ?> |
|
226 | 226 | |
227 | 227 | <div class="sd-shortcode-left-wrap"> |
228 | 228 | <?php |
229 | - ksort( $sd_widgets ); |
|
230 | - // print_r($sd_widgets);exit; |
|
231 | - if ( ! empty( $sd_widgets ) ) { |
|
232 | - echo '<select class="widefat" onchange="sd_get_shortcode_options(this);">'; |
|
233 | - echo "<option>" . __( 'Select shortcode' ) . "</option>"; |
|
234 | - foreach ( $sd_widgets as $shortcode => $class ) { |
|
235 | - echo "<option value='" . esc_attr( $shortcode ) . "'>" . esc_attr( $shortcode ) . " (" . esc_attr( $class['name'] ) . ")</option>"; |
|
236 | - } |
|
237 | - echo "</select>"; |
|
238 | - |
|
239 | - } |
|
240 | - ?> |
|
229 | + ksort( $sd_widgets ); |
|
230 | + // print_r($sd_widgets);exit; |
|
231 | + if ( ! empty( $sd_widgets ) ) { |
|
232 | + echo '<select class="widefat" onchange="sd_get_shortcode_options(this);">'; |
|
233 | + echo "<option>" . __( 'Select shortcode' ) . "</option>"; |
|
234 | + foreach ( $sd_widgets as $shortcode => $class ) { |
|
235 | + echo "<option value='" . esc_attr( $shortcode ) . "'>" . esc_attr( $shortcode ) . " (" . esc_attr( $class['name'] ) . ")</option>"; |
|
236 | + } |
|
237 | + echo "</select>"; |
|
238 | + |
|
239 | + } |
|
240 | + ?> |
|
241 | 241 | <div class="sd-shortcode-settings"></div> |
242 | 242 | |
243 | 243 | </div> |
@@ -248,8 +248,8 @@ discard block |
||
248 | 248 | <?php if ( $editor_id != '' ) { ?> |
249 | 249 | <button class="button sd-insert-shortcode-button" |
250 | 250 | onclick="sd_insert_shortcode(<?php if ( ! empty( $editor_id ) ) { |
251 | - echo "'" . $editor_id . "'"; |
|
252 | - } ?>)"><?php _e( 'Insert shortcode' ); ?></button> |
|
251 | + echo "'" . $editor_id . "'"; |
|
252 | + } ?>)"><?php _e( 'Insert shortcode' ); ?></button> |
|
253 | 253 | <?php } ?> |
254 | 254 | <button class="button" |
255 | 255 | onclick="sd_copy_to_clipboard()"><?php _e( 'Copy shortcode' ); ?></button> |
@@ -257,134 +257,134 @@ discard block |
||
257 | 257 | </div> |
258 | 258 | <?php |
259 | 259 | |
260 | - $html = ob_get_clean(); |
|
261 | - |
|
262 | - if ( wp_doing_ajax() ) { |
|
263 | - echo $html; |
|
264 | - $should_die = true; |
|
265 | - |
|
266 | - // some builder get the editor via ajax so we should not die on those ocasions |
|
267 | - $dont_die = array( |
|
268 | - 'parent_tag',// WP Bakery |
|
269 | - 'avia_request' // enfold |
|
270 | - ); |
|
271 | - |
|
272 | - foreach ( $dont_die as $request ) { |
|
273 | - if ( isset( $_REQUEST[ $request ] ) ) { |
|
274 | - $should_die = false; |
|
275 | - } |
|
276 | - } |
|
277 | - |
|
278 | - if ( $should_die ) { |
|
279 | - wp_die(); |
|
280 | - } |
|
281 | - |
|
282 | - } else { |
|
283 | - return $html; |
|
284 | - } |
|
285 | - |
|
286 | - return ''; |
|
287 | - |
|
288 | - } |
|
289 | - |
|
290 | - /** |
|
291 | - * Output the version in the admin header. |
|
292 | - */ |
|
293 | - public function generator() { |
|
294 | - echo '<meta name="generator" content="WP Super Duper v' . $this->version . '" />'; |
|
295 | - } |
|
296 | - |
|
297 | - /** |
|
298 | - * Get widget settings. |
|
299 | - * |
|
300 | - * @since 1.0.0 |
|
301 | - */ |
|
302 | - public static function get_widget_settings() { |
|
303 | - global $sd_widgets; |
|
304 | - |
|
305 | - $shortcode = isset( $_REQUEST['shortcode'] ) && $_REQUEST['shortcode'] ? sanitize_title_with_dashes( $_REQUEST['shortcode'] ) : ''; |
|
306 | - if ( ! $shortcode ) { |
|
307 | - wp_die(); |
|
308 | - } |
|
309 | - $widget_args = isset( $sd_widgets[ $shortcode ] ) ? $sd_widgets[ $shortcode ] : ''; |
|
310 | - if ( ! $widget_args ) { |
|
311 | - wp_die(); |
|
312 | - } |
|
313 | - $class_name = isset( $widget_args['class_name'] ) && $widget_args['class_name'] ? $widget_args['class_name'] : ''; |
|
314 | - if ( ! $class_name ) { |
|
315 | - wp_die(); |
|
316 | - } |
|
317 | - |
|
318 | - // invoke an instance method |
|
319 | - $widget = new $class_name; |
|
320 | - |
|
321 | - ob_start(); |
|
322 | - $widget->form( array() ); |
|
323 | - $form = ob_get_clean(); |
|
324 | - echo "<form id='$shortcode'>" . $form . "<div class=\"widget-control-save\"></div></form>"; |
|
325 | - echo "<style>" . $widget->widget_css() . "</style>"; |
|
326 | - echo "<script>" . $widget->widget_js() . "</script>"; |
|
327 | - ?> |
|
260 | + $html = ob_get_clean(); |
|
261 | + |
|
262 | + if ( wp_doing_ajax() ) { |
|
263 | + echo $html; |
|
264 | + $should_die = true; |
|
265 | + |
|
266 | + // some builder get the editor via ajax so we should not die on those ocasions |
|
267 | + $dont_die = array( |
|
268 | + 'parent_tag',// WP Bakery |
|
269 | + 'avia_request' // enfold |
|
270 | + ); |
|
271 | + |
|
272 | + foreach ( $dont_die as $request ) { |
|
273 | + if ( isset( $_REQUEST[ $request ] ) ) { |
|
274 | + $should_die = false; |
|
275 | + } |
|
276 | + } |
|
277 | + |
|
278 | + if ( $should_die ) { |
|
279 | + wp_die(); |
|
280 | + } |
|
281 | + |
|
282 | + } else { |
|
283 | + return $html; |
|
284 | + } |
|
285 | + |
|
286 | + return ''; |
|
287 | + |
|
288 | + } |
|
289 | + |
|
290 | + /** |
|
291 | + * Output the version in the admin header. |
|
292 | + */ |
|
293 | + public function generator() { |
|
294 | + echo '<meta name="generator" content="WP Super Duper v' . $this->version . '" />'; |
|
295 | + } |
|
296 | + |
|
297 | + /** |
|
298 | + * Get widget settings. |
|
299 | + * |
|
300 | + * @since 1.0.0 |
|
301 | + */ |
|
302 | + public static function get_widget_settings() { |
|
303 | + global $sd_widgets; |
|
304 | + |
|
305 | + $shortcode = isset( $_REQUEST['shortcode'] ) && $_REQUEST['shortcode'] ? sanitize_title_with_dashes( $_REQUEST['shortcode'] ) : ''; |
|
306 | + if ( ! $shortcode ) { |
|
307 | + wp_die(); |
|
308 | + } |
|
309 | + $widget_args = isset( $sd_widgets[ $shortcode ] ) ? $sd_widgets[ $shortcode ] : ''; |
|
310 | + if ( ! $widget_args ) { |
|
311 | + wp_die(); |
|
312 | + } |
|
313 | + $class_name = isset( $widget_args['class_name'] ) && $widget_args['class_name'] ? $widget_args['class_name'] : ''; |
|
314 | + if ( ! $class_name ) { |
|
315 | + wp_die(); |
|
316 | + } |
|
317 | + |
|
318 | + // invoke an instance method |
|
319 | + $widget = new $class_name; |
|
320 | + |
|
321 | + ob_start(); |
|
322 | + $widget->form( array() ); |
|
323 | + $form = ob_get_clean(); |
|
324 | + echo "<form id='$shortcode'>" . $form . "<div class=\"widget-control-save\"></div></form>"; |
|
325 | + echo "<style>" . $widget->widget_css() . "</style>"; |
|
326 | + echo "<script>" . $widget->widget_js() . "</script>"; |
|
327 | + ?> |
|
328 | 328 | <?php |
329 | - wp_die(); |
|
330 | - } |
|
331 | - |
|
332 | - /** |
|
333 | - * Insert shortcode builder button to classic editor (not inside Gutenberg, not needed). |
|
334 | - * |
|
335 | - * @since 1.0.0 |
|
336 | - * |
|
337 | - * @param string $editor_id Optional. Shortcode editor id. Default null. |
|
338 | - * @param string $insert_shortcode_function Optional. Insert shotcode function. Default null. |
|
339 | - */ |
|
340 | - public static function shortcode_insert_button( $editor_id = '', $insert_shortcode_function = '' ) { |
|
341 | - global $sd_widgets, $shortcode_insert_button_once; |
|
342 | - if ( $shortcode_insert_button_once ) { |
|
343 | - return; |
|
344 | - } |
|
345 | - add_thickbox(); |
|
346 | - |
|
347 | - |
|
348 | - /** |
|
349 | - * Cornerstone makes us play dirty tricks :/ |
|
350 | - * All media_buttons are removed via JS unless they are two specific id's so we wrap our content in this ID so it is not removed. |
|
351 | - */ |
|
352 | - if ( function_exists( 'cornerstone_plugin_init' ) && ! is_admin() ) { |
|
353 | - echo '<span id="insert-media-button">'; |
|
354 | - } |
|
355 | - |
|
356 | - echo self::shortcode_button( 'this', 'true' ); |
|
357 | - |
|
358 | - // see opening note |
|
359 | - if ( function_exists( 'cornerstone_plugin_init' ) && ! is_admin() ) { |
|
360 | - echo '</span>'; // end #insert-media-button |
|
361 | - } |
|
362 | - |
|
363 | - // Add separate script for generatepress theme sections |
|
364 | - if ( function_exists( 'generate_sections_sections_metabox' ) && did_action( 'generate_sections_metabox' ) ) { |
|
365 | - } else { |
|
366 | - self::shortcode_insert_button_script( $editor_id, $insert_shortcode_function ); |
|
367 | - } |
|
368 | - |
|
369 | - $shortcode_insert_button_once = true; |
|
370 | - } |
|
371 | - |
|
372 | - /** |
|
373 | - * Gets the shortcode insert button html. |
|
374 | - * |
|
375 | - * @param string $id |
|
376 | - * @param string $search_for_id |
|
377 | - * |
|
378 | - * @return mixed |
|
379 | - */ |
|
380 | - public static function shortcode_button( $id = '', $search_for_id = '' ) { |
|
381 | - ob_start(); |
|
382 | - ?> |
|
329 | + wp_die(); |
|
330 | + } |
|
331 | + |
|
332 | + /** |
|
333 | + * Insert shortcode builder button to classic editor (not inside Gutenberg, not needed). |
|
334 | + * |
|
335 | + * @since 1.0.0 |
|
336 | + * |
|
337 | + * @param string $editor_id Optional. Shortcode editor id. Default null. |
|
338 | + * @param string $insert_shortcode_function Optional. Insert shotcode function. Default null. |
|
339 | + */ |
|
340 | + public static function shortcode_insert_button( $editor_id = '', $insert_shortcode_function = '' ) { |
|
341 | + global $sd_widgets, $shortcode_insert_button_once; |
|
342 | + if ( $shortcode_insert_button_once ) { |
|
343 | + return; |
|
344 | + } |
|
345 | + add_thickbox(); |
|
346 | + |
|
347 | + |
|
348 | + /** |
|
349 | + * Cornerstone makes us play dirty tricks :/ |
|
350 | + * All media_buttons are removed via JS unless they are two specific id's so we wrap our content in this ID so it is not removed. |
|
351 | + */ |
|
352 | + if ( function_exists( 'cornerstone_plugin_init' ) && ! is_admin() ) { |
|
353 | + echo '<span id="insert-media-button">'; |
|
354 | + } |
|
355 | + |
|
356 | + echo self::shortcode_button( 'this', 'true' ); |
|
357 | + |
|
358 | + // see opening note |
|
359 | + if ( function_exists( 'cornerstone_plugin_init' ) && ! is_admin() ) { |
|
360 | + echo '</span>'; // end #insert-media-button |
|
361 | + } |
|
362 | + |
|
363 | + // Add separate script for generatepress theme sections |
|
364 | + if ( function_exists( 'generate_sections_sections_metabox' ) && did_action( 'generate_sections_metabox' ) ) { |
|
365 | + } else { |
|
366 | + self::shortcode_insert_button_script( $editor_id, $insert_shortcode_function ); |
|
367 | + } |
|
368 | + |
|
369 | + $shortcode_insert_button_once = true; |
|
370 | + } |
|
371 | + |
|
372 | + /** |
|
373 | + * Gets the shortcode insert button html. |
|
374 | + * |
|
375 | + * @param string $id |
|
376 | + * @param string $search_for_id |
|
377 | + * |
|
378 | + * @return mixed |
|
379 | + */ |
|
380 | + public static function shortcode_button( $id = '', $search_for_id = '' ) { |
|
381 | + ob_start(); |
|
382 | + ?> |
|
383 | 383 | <span class="sd-lable-shortcode-inserter"> |
384 | 384 | <a onclick="sd_ajax_get_picker(<?php echo $id; |
385 | - if ( $search_for_id ) { |
|
386 | - echo "," . $search_for_id; |
|
387 | - } ?>);" href="#TB_inline?width=100%&height=550&inlineId=super-duper-content-ajaxed" |
|
385 | + if ( $search_for_id ) { |
|
386 | + echo "," . $search_for_id; |
|
387 | + } ?>);" href="#TB_inline?width=100%&height=550&inlineId=super-duper-content-ajaxed" |
|
388 | 388 | class="thickbox button super-duper-content-open" title="Add Shortcode"> |
389 | 389 | <span style="vertical-align: middle;line-height: 18px;font-size: 20px;" |
390 | 390 | class="dashicons dashicons-screenoptions"></span> |
@@ -395,21 +395,21 @@ discard block |
||
395 | 395 | </span> |
396 | 396 | |
397 | 397 | <?php |
398 | - $html = ob_get_clean(); |
|
399 | - |
|
400 | - // remove line breaks so we can use it in js |
|
401 | - return preg_replace( "/\r|\n/", "", trim( $html ) ); |
|
402 | - } |
|
403 | - |
|
404 | - /** |
|
405 | - * Makes SD work with the siteOrigin page builder. |
|
406 | - * |
|
407 | - * @since 1.0.6 |
|
408 | - * @return mixed |
|
409 | - */ |
|
410 | - public static function siteorigin_js() { |
|
411 | - ob_start(); |
|
412 | - ?> |
|
398 | + $html = ob_get_clean(); |
|
399 | + |
|
400 | + // remove line breaks so we can use it in js |
|
401 | + return preg_replace( "/\r|\n/", "", trim( $html ) ); |
|
402 | + } |
|
403 | + |
|
404 | + /** |
|
405 | + * Makes SD work with the siteOrigin page builder. |
|
406 | + * |
|
407 | + * @since 1.0.6 |
|
408 | + * @return mixed |
|
409 | + */ |
|
410 | + public static function siteorigin_js() { |
|
411 | + ob_start(); |
|
412 | + ?> |
|
413 | 413 | <script> |
414 | 414 | /** |
415 | 415 | * Check a form to see what items shoudl be shown or hidden. |
@@ -487,28 +487,28 @@ discard block |
||
487 | 487 | }); |
488 | 488 | </script> |
489 | 489 | <?php |
490 | - $output = ob_get_clean(); |
|
490 | + $output = ob_get_clean(); |
|
491 | 491 | |
492 | - /* |
|
492 | + /* |
|
493 | 493 | * We only add the <script> tags for code highlighting, so we strip them from the output. |
494 | 494 | */ |
495 | 495 | |
496 | - return str_replace( array( |
|
497 | - '<script>', |
|
498 | - '</script>' |
|
499 | - ), '', $output ); |
|
500 | - } |
|
501 | - |
|
502 | - /** |
|
503 | - * Output the JS and CSS for the shortcode insert button. |
|
504 | - * |
|
505 | - * @since 1.0.6 |
|
506 | - * |
|
507 | - * @param string $editor_id |
|
508 | - * @param string $insert_shortcode_function |
|
509 | - */ |
|
510 | - public static function shortcode_insert_button_script( $editor_id = '', $insert_shortcode_function = '' ) { |
|
511 | - ?> |
|
496 | + return str_replace( array( |
|
497 | + '<script>', |
|
498 | + '</script>' |
|
499 | + ), '', $output ); |
|
500 | + } |
|
501 | + |
|
502 | + /** |
|
503 | + * Output the JS and CSS for the shortcode insert button. |
|
504 | + * |
|
505 | + * @since 1.0.6 |
|
506 | + * |
|
507 | + * @param string $editor_id |
|
508 | + * @param string $insert_shortcode_function |
|
509 | + */ |
|
510 | + public static function shortcode_insert_button_script( $editor_id = '', $insert_shortcode_function = '' ) { |
|
511 | + ?> |
|
512 | 512 | <style> |
513 | 513 | .sd-shortcode-left-wrap { |
514 | 514 | float: left; |
@@ -632,35 +632,35 @@ discard block |
||
632 | 632 | <?php } ?> |
633 | 633 | </style> |
634 | 634 | <?php |
635 | - if ( class_exists( 'SiteOrigin_Panels' ) ) { |
|
636 | - echo "<script>" . self::siteorigin_js() . "</script>"; |
|
637 | - } |
|
638 | - ?> |
|
635 | + if ( class_exists( 'SiteOrigin_Panels' ) ) { |
|
636 | + echo "<script>" . self::siteorigin_js() . "</script>"; |
|
637 | + } |
|
638 | + ?> |
|
639 | 639 | <script> |
640 | 640 | <?php |
641 | - if(! empty( $insert_shortcode_function )){ |
|
642 | - echo $insert_shortcode_function; |
|
643 | - }else{ |
|
644 | - |
|
645 | - /** |
|
646 | - * Function for super duper insert shortcode. |
|
647 | - * |
|
648 | - * @since 1.0.0 |
|
649 | - */ |
|
650 | - ?> |
|
641 | + if(! empty( $insert_shortcode_function )){ |
|
642 | + echo $insert_shortcode_function; |
|
643 | + }else{ |
|
644 | + |
|
645 | + /** |
|
646 | + * Function for super duper insert shortcode. |
|
647 | + * |
|
648 | + * @since 1.0.0 |
|
649 | + */ |
|
650 | + ?> |
|
651 | 651 | function sd_insert_shortcode($editor_id) { |
652 | 652 | $shortcode = jQuery('#TB_ajaxContent #sd-shortcode-output').val(); |
653 | 653 | if ($shortcode) { |
654 | 654 | if (!$editor_id) { |
655 | 655 | <?php |
656 | - if ( isset( $_REQUEST['et_fb'] ) ) { |
|
657 | - echo '$editor_id = "#main_content_content_vb_tiny_mce";'; |
|
658 | - } elseif ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'elementor' ) { |
|
659 | - echo '$editor_id = "#elementor-controls .wp-editor-container textarea";'; |
|
660 | - } else { |
|
661 | - echo '$editor_id = "#wp-content-editor-container textarea";'; |
|
662 | - } |
|
663 | - ?> |
|
656 | + if ( isset( $_REQUEST['et_fb'] ) ) { |
|
657 | + echo '$editor_id = "#main_content_content_vb_tiny_mce";'; |
|
658 | + } elseif ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'elementor' ) { |
|
659 | + echo '$editor_id = "#elementor-controls .wp-editor-container textarea";'; |
|
660 | + } else { |
|
661 | + echo '$editor_id = "#wp-content-editor-container textarea";'; |
|
662 | + } |
|
663 | + ?> |
|
664 | 664 | } else { |
665 | 665 | $editor_id = '#' + $editor_id; |
666 | 666 | } |
@@ -986,16 +986,16 @@ discard block |
||
986 | 986 | |
987 | 987 | </script> |
988 | 988 | <?php |
989 | - } |
|
990 | - |
|
991 | - /** |
|
992 | - * Gets some CSS for the widgets screen. |
|
993 | - * |
|
994 | - * @return mixed |
|
995 | - */ |
|
996 | - public function widget_css() { |
|
997 | - ob_start(); |
|
998 | - ?> |
|
989 | + } |
|
990 | + |
|
991 | + /** |
|
992 | + * Gets some CSS for the widgets screen. |
|
993 | + * |
|
994 | + * @return mixed |
|
995 | + */ |
|
996 | + public function widget_css() { |
|
997 | + ob_start(); |
|
998 | + ?> |
|
999 | 999 | <style> |
1000 | 1000 | .sd-advanced-setting { |
1001 | 1001 | display: none; |
@@ -1030,26 +1030,26 @@ discard block |
||
1030 | 1030 | } |
1031 | 1031 | </style> |
1032 | 1032 | <?php |
1033 | - $output = ob_get_clean(); |
|
1033 | + $output = ob_get_clean(); |
|
1034 | 1034 | |
1035 | - /* |
|
1035 | + /* |
|
1036 | 1036 | * We only add the <script> tags for code highlighting, so we strip them from the output. |
1037 | 1037 | */ |
1038 | 1038 | |
1039 | - return str_replace( array( |
|
1040 | - '<style>', |
|
1041 | - '</style>' |
|
1042 | - ), '', $output ); |
|
1043 | - } |
|
1044 | - |
|
1045 | - /** |
|
1046 | - * Gets some JS for the widgets screen. |
|
1047 | - * |
|
1048 | - * @return mixed |
|
1049 | - */ |
|
1050 | - public function widget_js() { |
|
1051 | - ob_start(); |
|
1052 | - ?> |
|
1039 | + return str_replace( array( |
|
1040 | + '<style>', |
|
1041 | + '</style>' |
|
1042 | + ), '', $output ); |
|
1043 | + } |
|
1044 | + |
|
1045 | + /** |
|
1046 | + * Gets some JS for the widgets screen. |
|
1047 | + * |
|
1048 | + * @return mixed |
|
1049 | + */ |
|
1050 | + public function widget_js() { |
|
1051 | + ob_start(); |
|
1052 | + ?> |
|
1053 | 1053 | <script> |
1054 | 1054 | |
1055 | 1055 | /** |
@@ -1204,397 +1204,397 @@ discard block |
||
1204 | 1204 | <?php do_action( 'wp_super_duper_widget_js', $this ); ?> |
1205 | 1205 | </script> |
1206 | 1206 | <?php |
1207 | - $output = ob_get_clean(); |
|
1207 | + $output = ob_get_clean(); |
|
1208 | 1208 | |
1209 | - /* |
|
1209 | + /* |
|
1210 | 1210 | * We only add the <script> tags for code highlighting, so we strip them from the output. |
1211 | 1211 | */ |
1212 | 1212 | |
1213 | - return str_replace( array( |
|
1214 | - '<script>', |
|
1215 | - '</script>' |
|
1216 | - ), '', $output ); |
|
1217 | - } |
|
1218 | - |
|
1219 | - |
|
1220 | - /** |
|
1221 | - * Set the name from the argument key. |
|
1222 | - * |
|
1223 | - * @param $options |
|
1224 | - * |
|
1225 | - * @return mixed |
|
1226 | - */ |
|
1227 | - private function add_name_from_key( $options, $arguments = false ) { |
|
1228 | - if ( ! empty( $options['arguments'] ) ) { |
|
1229 | - foreach ( $options['arguments'] as $key => $val ) { |
|
1230 | - $options['arguments'][ $key ]['name'] = $key; |
|
1231 | - } |
|
1232 | - } elseif ( $arguments && is_array( $options ) && ! empty( $options ) ) { |
|
1233 | - foreach ( $options as $key => $val ) { |
|
1234 | - $options[ $key ]['name'] = $key; |
|
1235 | - } |
|
1236 | - } |
|
1237 | - |
|
1238 | - return $options; |
|
1239 | - } |
|
1240 | - |
|
1241 | - /** |
|
1242 | - * Register the parent shortcode. |
|
1243 | - * |
|
1244 | - * @since 1.0.0 |
|
1245 | - */ |
|
1246 | - public function register_shortcode() { |
|
1247 | - add_shortcode( $this->base_id, array( $this, 'shortcode_output' ) ); |
|
1248 | - add_action( 'wp_ajax_super_duper_output_shortcode', array( __CLASS__, 'render_shortcode' ) ); |
|
1249 | - } |
|
1250 | - |
|
1251 | - /** |
|
1252 | - * Render the shortcode via ajax so we can return it to Gutenberg. |
|
1253 | - * |
|
1254 | - * @since 1.0.0 |
|
1255 | - */ |
|
1256 | - public static function render_shortcode() { |
|
1257 | - |
|
1258 | - check_ajax_referer( 'super_duper_output_shortcode', '_ajax_nonce', true ); |
|
1259 | - if ( ! current_user_can( 'manage_options' ) ) { |
|
1260 | - wp_die(); |
|
1261 | - } |
|
1262 | - |
|
1263 | - // we might need the $post value here so lets set it. |
|
1264 | - if ( isset( $_POST['post_id'] ) && $_POST['post_id'] ) { |
|
1265 | - $post_obj = get_post( absint( $_POST['post_id'] ) ); |
|
1266 | - if ( ! empty( $post_obj ) && empty( $post ) ) { |
|
1267 | - global $post; |
|
1268 | - $post = $post_obj; |
|
1269 | - } |
|
1270 | - } |
|
1271 | - |
|
1272 | - if ( isset( $_POST['shortcode'] ) && $_POST['shortcode'] ) { |
|
1273 | - $shortcode_name = sanitize_title_with_dashes( $_POST['shortcode'] ); |
|
1274 | - $attributes_array = isset( $_POST['attributes'] ) && $_POST['attributes'] ? $_POST['attributes'] : array(); |
|
1275 | - $attributes = ''; |
|
1276 | - if ( ! empty( $attributes_array ) ) { |
|
1277 | - foreach ( $attributes_array as $key => $value ) { |
|
1278 | - $attributes .= " " . sanitize_title_with_dashes( $key ) . "='" . wp_slash( $value ) . "' "; |
|
1279 | - } |
|
1280 | - } |
|
1281 | - |
|
1282 | - $shortcode = "[" . $shortcode_name . " " . $attributes . "]"; |
|
1283 | - |
|
1284 | - echo do_shortcode( $shortcode ); |
|
1285 | - |
|
1286 | - } |
|
1287 | - wp_die(); |
|
1288 | - } |
|
1289 | - |
|
1290 | - /** |
|
1291 | - * Output the shortcode. |
|
1292 | - * |
|
1293 | - * @param array $args |
|
1294 | - * @param string $content |
|
1295 | - * |
|
1296 | - * @return string |
|
1297 | - */ |
|
1298 | - public function shortcode_output( $args = array(), $content = '' ) { |
|
1299 | - $args = self::argument_values( $args ); |
|
1300 | - |
|
1301 | - // add extra argument so we know its a output to gutenberg |
|
1302 | - //$args |
|
1303 | - $args = $this->string_to_bool( $args ); |
|
1304 | - |
|
1305 | - // if we have a enclosed shortcode we add it to the special `html` argument |
|
1306 | - if(!empty($content)){ |
|
1307 | - $args['html'] = $content; |
|
1308 | - } |
|
1309 | - |
|
1310 | - $class = isset( $this->options['widget_ops']['classname'] ) ? esc_attr( $this->options['widget_ops']['classname'] ) : ''; |
|
1311 | - |
|
1312 | - $class = apply_filters( 'wp_super_duper_div_classname', $class, $args, $this ); |
|
1313 | - $class = apply_filters( 'wp_super_duper_div_classname_' . $this->base_id, $class, $args, $this ); |
|
1314 | - |
|
1315 | - $attrs = apply_filters( 'wp_super_duper_div_attrs', '', $args, $this ); |
|
1316 | - $attrs = apply_filters( 'wp_super_duper_div_attrs_' . $this->base_id, '', $args, $this ); //@todo this does not seem right @kiran? |
|
1317 | - |
|
1318 | - $shortcode_args = array(); |
|
1319 | - $output = ''; |
|
1320 | - $no_wrap = isset( $this->options['no_wrap'] ) && $this->options['no_wrap'] ? true : false; |
|
1321 | - if( isset( $args['no_wrap'] ) && $args['no_wrap'] ){ $no_wrap = true; } |
|
1322 | - $main_content = $this->output( $args, $shortcode_args, $content ); |
|
1323 | - if ( $main_content && ! $no_wrap ) { |
|
1324 | - // wrap the shortcode in a div with the same class as the widget |
|
1325 | - $output .= '<div class="' . $class . '" ' . $attrs . '>'; |
|
1326 | - if ( ! empty( $args['title'] ) ) { |
|
1327 | - // if its a shortcode and there is a title try to grab the title wrappers |
|
1328 | - $shortcode_args = array( 'before_title' => '', 'after_title' => '' ); |
|
1329 | - if ( empty( $instance ) ) { |
|
1330 | - global $wp_registered_sidebars; |
|
1331 | - if ( ! empty( $wp_registered_sidebars ) ) { |
|
1332 | - foreach ( $wp_registered_sidebars as $sidebar ) { |
|
1333 | - if ( ! empty( $sidebar['before_title'] ) ) { |
|
1334 | - $shortcode_args['before_title'] = $sidebar['before_title']; |
|
1335 | - $shortcode_args['after_title'] = $sidebar['after_title']; |
|
1336 | - break; |
|
1337 | - } |
|
1338 | - } |
|
1339 | - } |
|
1340 | - } |
|
1341 | - $output .= $this->output_title( $shortcode_args, $args ); |
|
1342 | - } |
|
1343 | - $output .= $main_content; |
|
1344 | - $output .= '</div>'; |
|
1345 | - } elseif ( $main_content && $no_wrap ) { |
|
1346 | - $output .= $main_content; |
|
1347 | - } |
|
1348 | - |
|
1349 | - // if preview show a placeholder if empty |
|
1350 | - if ( $this->is_preview() && $output == '' ) { |
|
1351 | - $output = $this->preview_placeholder_text( "[{" . $this->base_id . "}]" ); |
|
1352 | - } |
|
1353 | - |
|
1354 | - return apply_filters( 'wp_super_duper_widget_output', $output, $args, $shortcode_args, $this ); |
|
1355 | - } |
|
1356 | - |
|
1357 | - /** |
|
1358 | - * Placeholder text to show if output is empty and we are on a preview/builder page. |
|
1359 | - * |
|
1360 | - * @param string $name |
|
1361 | - * |
|
1362 | - * @return string |
|
1363 | - */ |
|
1364 | - public function preview_placeholder_text( $name = '' ) { |
|
1365 | - return "<div style='background:#0185ba33;padding: 10px;border: 4px #ccc dashed;'>" . sprintf( __( 'Placeholder for: %s' ), $name ) . "</div>"; |
|
1366 | - } |
|
1367 | - |
|
1368 | - /** |
|
1369 | - * Sometimes booleans values can be turned to strings, so we fix that. |
|
1370 | - * |
|
1371 | - * @param $options |
|
1372 | - * |
|
1373 | - * @return mixed |
|
1374 | - */ |
|
1375 | - public function string_to_bool( $options ) { |
|
1376 | - // convert bool strings to booleans |
|
1377 | - foreach ( $options as $key => $val ) { |
|
1378 | - if ( $val == 'false' ) { |
|
1379 | - $options[ $key ] = false; |
|
1380 | - } elseif ( $val == 'true' ) { |
|
1381 | - $options[ $key ] = true; |
|
1382 | - } |
|
1383 | - } |
|
1384 | - |
|
1385 | - return $options; |
|
1386 | - } |
|
1387 | - |
|
1388 | - /** |
|
1389 | - * Get the argument values that are also filterable. |
|
1390 | - * |
|
1391 | - * @param $instance |
|
1392 | - * |
|
1393 | - * @since 1.0.12 Don't set checkbox default value if the value is empty. |
|
1394 | - * |
|
1395 | - * @return array |
|
1396 | - */ |
|
1397 | - public function argument_values( $instance ) { |
|
1398 | - $argument_values = array(); |
|
1399 | - |
|
1400 | - // set widget instance |
|
1401 | - $this->instance = $instance; |
|
1402 | - |
|
1403 | - if ( empty( $this->arguments ) ) { |
|
1404 | - $this->arguments = $this->get_arguments(); |
|
1405 | - } |
|
1406 | - |
|
1407 | - if ( ! empty( $this->arguments ) ) { |
|
1408 | - foreach ( $this->arguments as $key => $args ) { |
|
1409 | - // set the input name from the key |
|
1410 | - $args['name'] = $key; |
|
1411 | - // |
|
1412 | - $argument_values[ $key ] = isset( $instance[ $key ] ) ? $instance[ $key ] : ''; |
|
1413 | - if($args['type']=='checkbox' && $argument_values[ $key ] == ''){ |
|
1414 | - // don't set default for an empty checkbox |
|
1415 | - } |
|
1416 | - elseif ( $argument_values[ $key ] == '' && isset( $args['default'] ) ) { |
|
1417 | - $argument_values[ $key ] = $args['default']; |
|
1418 | - } |
|
1419 | - } |
|
1420 | - } |
|
1421 | - |
|
1422 | - return $argument_values; |
|
1423 | - } |
|
1424 | - |
|
1425 | - /** |
|
1426 | - * Set arguments in super duper. |
|
1427 | - * |
|
1428 | - * @since 1.0.0 |
|
1429 | - * |
|
1430 | - * @return array Set arguments. |
|
1431 | - */ |
|
1432 | - public function set_arguments() { |
|
1433 | - return $this->arguments; |
|
1434 | - } |
|
1435 | - |
|
1436 | - /** |
|
1437 | - * Get arguments in super duper. |
|
1438 | - * |
|
1439 | - * @since 1.0.0 |
|
1440 | - * |
|
1441 | - * @return array Get arguments. |
|
1442 | - */ |
|
1443 | - public function get_arguments() { |
|
1444 | - if ( empty( $this->arguments ) ) { |
|
1445 | - $this->arguments = $this->set_arguments(); |
|
1446 | - } |
|
1447 | - |
|
1448 | - $this->arguments = apply_filters( 'wp_super_duper_arguments', $this->arguments, $this->options, $this->instance ); |
|
1449 | - $this->arguments = $this->add_name_from_key( $this->arguments, true ); |
|
1450 | - |
|
1451 | - return $this->arguments; |
|
1452 | - } |
|
1453 | - |
|
1454 | - /** |
|
1455 | - * This is the main output class for all 3 items, widget, shortcode and block, it is extended in the calling class. |
|
1456 | - * |
|
1457 | - * @param array $args |
|
1458 | - * @param array $widget_args |
|
1459 | - * @param string $content |
|
1460 | - */ |
|
1461 | - public function output( $args = array(), $widget_args = array(), $content = '' ) { |
|
1462 | - |
|
1463 | - } |
|
1464 | - |
|
1465 | - /** |
|
1466 | - * Add the dynamic block code inline when the wp-block in enqueued. |
|
1467 | - */ |
|
1468 | - public function register_block() { |
|
1469 | - wp_add_inline_script( 'wp-blocks', $this->block() ); |
|
1470 | - if ( class_exists( 'SiteOrigin_Panels' ) ) { |
|
1471 | - |
|
1472 | - wp_add_inline_script( 'wp-blocks', $this->siteorigin_js() ); |
|
1473 | - |
|
1474 | - } |
|
1475 | - } |
|
1476 | - |
|
1477 | - /** |
|
1478 | - * Check if we need to show advanced options. |
|
1479 | - * |
|
1480 | - * @return bool |
|
1481 | - */ |
|
1482 | - public function block_show_advanced() { |
|
1483 | - |
|
1484 | - $show = false; |
|
1485 | - $arguments = $this->arguments; |
|
1486 | - |
|
1487 | - if ( empty( $arguments ) ) { |
|
1488 | - $arguments = $this->get_arguments(); |
|
1489 | - } |
|
1490 | - |
|
1491 | - if ( ! empty( $arguments ) ) { |
|
1492 | - foreach ( $arguments as $argument ) { |
|
1493 | - if ( isset( $argument['advanced'] ) && $argument['advanced'] ) { |
|
1494 | - $show = true; |
|
1495 | - break; // no need to continue if we know we have it |
|
1496 | - } |
|
1497 | - } |
|
1498 | - } |
|
1499 | - |
|
1500 | - return $show; |
|
1501 | - } |
|
1502 | - |
|
1503 | - /** |
|
1504 | - * Get the url path to the current folder. |
|
1505 | - * |
|
1506 | - * @return string |
|
1507 | - */ |
|
1508 | - public function get_url() { |
|
1509 | - |
|
1510 | - $url = $this->url; |
|
1511 | - |
|
1512 | - if(!$url){ |
|
1513 | - // check if we are inside a plugin |
|
1514 | - $file_dir = str_replace("/includes","", dirname( __FILE__ )); |
|
1515 | - |
|
1516 | - $dir_parts = explode("/wp-content/",$file_dir); |
|
1517 | - $url_parts = explode("/wp-content/",plugins_url()); |
|
1518 | - |
|
1519 | - if(!empty($url_parts[0]) && !empty($dir_parts[1])){ |
|
1520 | - $url = trailingslashit( $url_parts[0]."/wp-content/".$dir_parts[1] ); |
|
1521 | - $this->url = $url; |
|
1522 | - } |
|
1523 | - } |
|
1524 | - |
|
1525 | - |
|
1526 | - return $url; |
|
1527 | - } |
|
1528 | - |
|
1529 | - /** |
|
1530 | - * Generate the block icon. |
|
1531 | - * |
|
1532 | - * Enables the use of Font Awesome icons. |
|
1533 | - * |
|
1534 | - * @note xlink:href is actually deprecated but href is not supported by all so we use both. |
|
1535 | - * @param $icon |
|
1536 | - * @since 1.1.0 |
|
1537 | - * @return string |
|
1538 | - */ |
|
1539 | - public function get_block_icon($icon){ |
|
1540 | - |
|
1541 | - // check if we have a Font Awesome icon |
|
1542 | - $fa_type = ''; |
|
1543 | - if(substr( $icon, 0, 7 ) === "fas fa-"){ |
|
1544 | - $fa_type = 'solid'; |
|
1545 | - }elseif(substr( $icon, 0, 7 ) === "far fa-"){ |
|
1546 | - $fa_type = 'regular'; |
|
1547 | - }elseif(substr( $icon, 0, 7 ) === "fab fa-"){ |
|
1548 | - $fa_type = 'brands'; |
|
1549 | - }else{ |
|
1550 | - $icon = "'".$icon."'"; |
|
1551 | - } |
|
1552 | - |
|
1553 | - // set the icon if we found one |
|
1554 | - if($fa_type){ |
|
1555 | - $fa_icon = str_replace(array("fas fa-","far fa-","fab fa-"),"",$icon); |
|
1556 | - $icon = "el('svg',{width: 20, height: 20, viewBox: '0 0 20 20'},el('use', {'xlink:href': '".$this->get_url()."icons/".$fa_type.".svg#".$fa_icon."','href': '".$this->get_url()."icons/".$fa_type.".svg#".$fa_icon."'}))"; |
|
1557 | - } |
|
1558 | - |
|
1559 | - return $icon; |
|
1560 | - } |
|
1561 | - |
|
1562 | - public function group_arguments($arguments){ |
|
1213 | + return str_replace( array( |
|
1214 | + '<script>', |
|
1215 | + '</script>' |
|
1216 | + ), '', $output ); |
|
1217 | + } |
|
1218 | + |
|
1219 | + |
|
1220 | + /** |
|
1221 | + * Set the name from the argument key. |
|
1222 | + * |
|
1223 | + * @param $options |
|
1224 | + * |
|
1225 | + * @return mixed |
|
1226 | + */ |
|
1227 | + private function add_name_from_key( $options, $arguments = false ) { |
|
1228 | + if ( ! empty( $options['arguments'] ) ) { |
|
1229 | + foreach ( $options['arguments'] as $key => $val ) { |
|
1230 | + $options['arguments'][ $key ]['name'] = $key; |
|
1231 | + } |
|
1232 | + } elseif ( $arguments && is_array( $options ) && ! empty( $options ) ) { |
|
1233 | + foreach ( $options as $key => $val ) { |
|
1234 | + $options[ $key ]['name'] = $key; |
|
1235 | + } |
|
1236 | + } |
|
1237 | + |
|
1238 | + return $options; |
|
1239 | + } |
|
1240 | + |
|
1241 | + /** |
|
1242 | + * Register the parent shortcode. |
|
1243 | + * |
|
1244 | + * @since 1.0.0 |
|
1245 | + */ |
|
1246 | + public function register_shortcode() { |
|
1247 | + add_shortcode( $this->base_id, array( $this, 'shortcode_output' ) ); |
|
1248 | + add_action( 'wp_ajax_super_duper_output_shortcode', array( __CLASS__, 'render_shortcode' ) ); |
|
1249 | + } |
|
1250 | + |
|
1251 | + /** |
|
1252 | + * Render the shortcode via ajax so we can return it to Gutenberg. |
|
1253 | + * |
|
1254 | + * @since 1.0.0 |
|
1255 | + */ |
|
1256 | + public static function render_shortcode() { |
|
1257 | + |
|
1258 | + check_ajax_referer( 'super_duper_output_shortcode', '_ajax_nonce', true ); |
|
1259 | + if ( ! current_user_can( 'manage_options' ) ) { |
|
1260 | + wp_die(); |
|
1261 | + } |
|
1262 | + |
|
1263 | + // we might need the $post value here so lets set it. |
|
1264 | + if ( isset( $_POST['post_id'] ) && $_POST['post_id'] ) { |
|
1265 | + $post_obj = get_post( absint( $_POST['post_id'] ) ); |
|
1266 | + if ( ! empty( $post_obj ) && empty( $post ) ) { |
|
1267 | + global $post; |
|
1268 | + $post = $post_obj; |
|
1269 | + } |
|
1270 | + } |
|
1271 | + |
|
1272 | + if ( isset( $_POST['shortcode'] ) && $_POST['shortcode'] ) { |
|
1273 | + $shortcode_name = sanitize_title_with_dashes( $_POST['shortcode'] ); |
|
1274 | + $attributes_array = isset( $_POST['attributes'] ) && $_POST['attributes'] ? $_POST['attributes'] : array(); |
|
1275 | + $attributes = ''; |
|
1276 | + if ( ! empty( $attributes_array ) ) { |
|
1277 | + foreach ( $attributes_array as $key => $value ) { |
|
1278 | + $attributes .= " " . sanitize_title_with_dashes( $key ) . "='" . wp_slash( $value ) . "' "; |
|
1279 | + } |
|
1280 | + } |
|
1281 | + |
|
1282 | + $shortcode = "[" . $shortcode_name . " " . $attributes . "]"; |
|
1283 | + |
|
1284 | + echo do_shortcode( $shortcode ); |
|
1285 | + |
|
1286 | + } |
|
1287 | + wp_die(); |
|
1288 | + } |
|
1289 | + |
|
1290 | + /** |
|
1291 | + * Output the shortcode. |
|
1292 | + * |
|
1293 | + * @param array $args |
|
1294 | + * @param string $content |
|
1295 | + * |
|
1296 | + * @return string |
|
1297 | + */ |
|
1298 | + public function shortcode_output( $args = array(), $content = '' ) { |
|
1299 | + $args = self::argument_values( $args ); |
|
1300 | + |
|
1301 | + // add extra argument so we know its a output to gutenberg |
|
1302 | + //$args |
|
1303 | + $args = $this->string_to_bool( $args ); |
|
1304 | + |
|
1305 | + // if we have a enclosed shortcode we add it to the special `html` argument |
|
1306 | + if(!empty($content)){ |
|
1307 | + $args['html'] = $content; |
|
1308 | + } |
|
1309 | + |
|
1310 | + $class = isset( $this->options['widget_ops']['classname'] ) ? esc_attr( $this->options['widget_ops']['classname'] ) : ''; |
|
1311 | + |
|
1312 | + $class = apply_filters( 'wp_super_duper_div_classname', $class, $args, $this ); |
|
1313 | + $class = apply_filters( 'wp_super_duper_div_classname_' . $this->base_id, $class, $args, $this ); |
|
1314 | + |
|
1315 | + $attrs = apply_filters( 'wp_super_duper_div_attrs', '', $args, $this ); |
|
1316 | + $attrs = apply_filters( 'wp_super_duper_div_attrs_' . $this->base_id, '', $args, $this ); //@todo this does not seem right @kiran? |
|
1317 | + |
|
1318 | + $shortcode_args = array(); |
|
1319 | + $output = ''; |
|
1320 | + $no_wrap = isset( $this->options['no_wrap'] ) && $this->options['no_wrap'] ? true : false; |
|
1321 | + if( isset( $args['no_wrap'] ) && $args['no_wrap'] ){ $no_wrap = true; } |
|
1322 | + $main_content = $this->output( $args, $shortcode_args, $content ); |
|
1323 | + if ( $main_content && ! $no_wrap ) { |
|
1324 | + // wrap the shortcode in a div with the same class as the widget |
|
1325 | + $output .= '<div class="' . $class . '" ' . $attrs . '>'; |
|
1326 | + if ( ! empty( $args['title'] ) ) { |
|
1327 | + // if its a shortcode and there is a title try to grab the title wrappers |
|
1328 | + $shortcode_args = array( 'before_title' => '', 'after_title' => '' ); |
|
1329 | + if ( empty( $instance ) ) { |
|
1330 | + global $wp_registered_sidebars; |
|
1331 | + if ( ! empty( $wp_registered_sidebars ) ) { |
|
1332 | + foreach ( $wp_registered_sidebars as $sidebar ) { |
|
1333 | + if ( ! empty( $sidebar['before_title'] ) ) { |
|
1334 | + $shortcode_args['before_title'] = $sidebar['before_title']; |
|
1335 | + $shortcode_args['after_title'] = $sidebar['after_title']; |
|
1336 | + break; |
|
1337 | + } |
|
1338 | + } |
|
1339 | + } |
|
1340 | + } |
|
1341 | + $output .= $this->output_title( $shortcode_args, $args ); |
|
1342 | + } |
|
1343 | + $output .= $main_content; |
|
1344 | + $output .= '</div>'; |
|
1345 | + } elseif ( $main_content && $no_wrap ) { |
|
1346 | + $output .= $main_content; |
|
1347 | + } |
|
1348 | + |
|
1349 | + // if preview show a placeholder if empty |
|
1350 | + if ( $this->is_preview() && $output == '' ) { |
|
1351 | + $output = $this->preview_placeholder_text( "[{" . $this->base_id . "}]" ); |
|
1352 | + } |
|
1353 | + |
|
1354 | + return apply_filters( 'wp_super_duper_widget_output', $output, $args, $shortcode_args, $this ); |
|
1355 | + } |
|
1356 | + |
|
1357 | + /** |
|
1358 | + * Placeholder text to show if output is empty and we are on a preview/builder page. |
|
1359 | + * |
|
1360 | + * @param string $name |
|
1361 | + * |
|
1362 | + * @return string |
|
1363 | + */ |
|
1364 | + public function preview_placeholder_text( $name = '' ) { |
|
1365 | + return "<div style='background:#0185ba33;padding: 10px;border: 4px #ccc dashed;'>" . sprintf( __( 'Placeholder for: %s' ), $name ) . "</div>"; |
|
1366 | + } |
|
1367 | + |
|
1368 | + /** |
|
1369 | + * Sometimes booleans values can be turned to strings, so we fix that. |
|
1370 | + * |
|
1371 | + * @param $options |
|
1372 | + * |
|
1373 | + * @return mixed |
|
1374 | + */ |
|
1375 | + public function string_to_bool( $options ) { |
|
1376 | + // convert bool strings to booleans |
|
1377 | + foreach ( $options as $key => $val ) { |
|
1378 | + if ( $val == 'false' ) { |
|
1379 | + $options[ $key ] = false; |
|
1380 | + } elseif ( $val == 'true' ) { |
|
1381 | + $options[ $key ] = true; |
|
1382 | + } |
|
1383 | + } |
|
1384 | + |
|
1385 | + return $options; |
|
1386 | + } |
|
1387 | + |
|
1388 | + /** |
|
1389 | + * Get the argument values that are also filterable. |
|
1390 | + * |
|
1391 | + * @param $instance |
|
1392 | + * |
|
1393 | + * @since 1.0.12 Don't set checkbox default value if the value is empty. |
|
1394 | + * |
|
1395 | + * @return array |
|
1396 | + */ |
|
1397 | + public function argument_values( $instance ) { |
|
1398 | + $argument_values = array(); |
|
1399 | + |
|
1400 | + // set widget instance |
|
1401 | + $this->instance = $instance; |
|
1402 | + |
|
1403 | + if ( empty( $this->arguments ) ) { |
|
1404 | + $this->arguments = $this->get_arguments(); |
|
1405 | + } |
|
1406 | + |
|
1407 | + if ( ! empty( $this->arguments ) ) { |
|
1408 | + foreach ( $this->arguments as $key => $args ) { |
|
1409 | + // set the input name from the key |
|
1410 | + $args['name'] = $key; |
|
1411 | + // |
|
1412 | + $argument_values[ $key ] = isset( $instance[ $key ] ) ? $instance[ $key ] : ''; |
|
1413 | + if($args['type']=='checkbox' && $argument_values[ $key ] == ''){ |
|
1414 | + // don't set default for an empty checkbox |
|
1415 | + } |
|
1416 | + elseif ( $argument_values[ $key ] == '' && isset( $args['default'] ) ) { |
|
1417 | + $argument_values[ $key ] = $args['default']; |
|
1418 | + } |
|
1419 | + } |
|
1420 | + } |
|
1421 | + |
|
1422 | + return $argument_values; |
|
1423 | + } |
|
1424 | + |
|
1425 | + /** |
|
1426 | + * Set arguments in super duper. |
|
1427 | + * |
|
1428 | + * @since 1.0.0 |
|
1429 | + * |
|
1430 | + * @return array Set arguments. |
|
1431 | + */ |
|
1432 | + public function set_arguments() { |
|
1433 | + return $this->arguments; |
|
1434 | + } |
|
1435 | + |
|
1436 | + /** |
|
1437 | + * Get arguments in super duper. |
|
1438 | + * |
|
1439 | + * @since 1.0.0 |
|
1440 | + * |
|
1441 | + * @return array Get arguments. |
|
1442 | + */ |
|
1443 | + public function get_arguments() { |
|
1444 | + if ( empty( $this->arguments ) ) { |
|
1445 | + $this->arguments = $this->set_arguments(); |
|
1446 | + } |
|
1447 | + |
|
1448 | + $this->arguments = apply_filters( 'wp_super_duper_arguments', $this->arguments, $this->options, $this->instance ); |
|
1449 | + $this->arguments = $this->add_name_from_key( $this->arguments, true ); |
|
1450 | + |
|
1451 | + return $this->arguments; |
|
1452 | + } |
|
1453 | + |
|
1454 | + /** |
|
1455 | + * This is the main output class for all 3 items, widget, shortcode and block, it is extended in the calling class. |
|
1456 | + * |
|
1457 | + * @param array $args |
|
1458 | + * @param array $widget_args |
|
1459 | + * @param string $content |
|
1460 | + */ |
|
1461 | + public function output( $args = array(), $widget_args = array(), $content = '' ) { |
|
1462 | + |
|
1463 | + } |
|
1464 | + |
|
1465 | + /** |
|
1466 | + * Add the dynamic block code inline when the wp-block in enqueued. |
|
1467 | + */ |
|
1468 | + public function register_block() { |
|
1469 | + wp_add_inline_script( 'wp-blocks', $this->block() ); |
|
1470 | + if ( class_exists( 'SiteOrigin_Panels' ) ) { |
|
1471 | + |
|
1472 | + wp_add_inline_script( 'wp-blocks', $this->siteorigin_js() ); |
|
1473 | + |
|
1474 | + } |
|
1475 | + } |
|
1476 | + |
|
1477 | + /** |
|
1478 | + * Check if we need to show advanced options. |
|
1479 | + * |
|
1480 | + * @return bool |
|
1481 | + */ |
|
1482 | + public function block_show_advanced() { |
|
1483 | + |
|
1484 | + $show = false; |
|
1485 | + $arguments = $this->arguments; |
|
1486 | + |
|
1487 | + if ( empty( $arguments ) ) { |
|
1488 | + $arguments = $this->get_arguments(); |
|
1489 | + } |
|
1490 | + |
|
1491 | + if ( ! empty( $arguments ) ) { |
|
1492 | + foreach ( $arguments as $argument ) { |
|
1493 | + if ( isset( $argument['advanced'] ) && $argument['advanced'] ) { |
|
1494 | + $show = true; |
|
1495 | + break; // no need to continue if we know we have it |
|
1496 | + } |
|
1497 | + } |
|
1498 | + } |
|
1499 | + |
|
1500 | + return $show; |
|
1501 | + } |
|
1502 | + |
|
1503 | + /** |
|
1504 | + * Get the url path to the current folder. |
|
1505 | + * |
|
1506 | + * @return string |
|
1507 | + */ |
|
1508 | + public function get_url() { |
|
1509 | + |
|
1510 | + $url = $this->url; |
|
1511 | + |
|
1512 | + if(!$url){ |
|
1513 | + // check if we are inside a plugin |
|
1514 | + $file_dir = str_replace("/includes","", dirname( __FILE__ )); |
|
1515 | + |
|
1516 | + $dir_parts = explode("/wp-content/",$file_dir); |
|
1517 | + $url_parts = explode("/wp-content/",plugins_url()); |
|
1518 | + |
|
1519 | + if(!empty($url_parts[0]) && !empty($dir_parts[1])){ |
|
1520 | + $url = trailingslashit( $url_parts[0]."/wp-content/".$dir_parts[1] ); |
|
1521 | + $this->url = $url; |
|
1522 | + } |
|
1523 | + } |
|
1524 | + |
|
1525 | + |
|
1526 | + return $url; |
|
1527 | + } |
|
1528 | + |
|
1529 | + /** |
|
1530 | + * Generate the block icon. |
|
1531 | + * |
|
1532 | + * Enables the use of Font Awesome icons. |
|
1533 | + * |
|
1534 | + * @note xlink:href is actually deprecated but href is not supported by all so we use both. |
|
1535 | + * @param $icon |
|
1536 | + * @since 1.1.0 |
|
1537 | + * @return string |
|
1538 | + */ |
|
1539 | + public function get_block_icon($icon){ |
|
1540 | + |
|
1541 | + // check if we have a Font Awesome icon |
|
1542 | + $fa_type = ''; |
|
1543 | + if(substr( $icon, 0, 7 ) === "fas fa-"){ |
|
1544 | + $fa_type = 'solid'; |
|
1545 | + }elseif(substr( $icon, 0, 7 ) === "far fa-"){ |
|
1546 | + $fa_type = 'regular'; |
|
1547 | + }elseif(substr( $icon, 0, 7 ) === "fab fa-"){ |
|
1548 | + $fa_type = 'brands'; |
|
1549 | + }else{ |
|
1550 | + $icon = "'".$icon."'"; |
|
1551 | + } |
|
1552 | + |
|
1553 | + // set the icon if we found one |
|
1554 | + if($fa_type){ |
|
1555 | + $fa_icon = str_replace(array("fas fa-","far fa-","fab fa-"),"",$icon); |
|
1556 | + $icon = "el('svg',{width: 20, height: 20, viewBox: '0 0 20 20'},el('use', {'xlink:href': '".$this->get_url()."icons/".$fa_type.".svg#".$fa_icon."','href': '".$this->get_url()."icons/".$fa_type.".svg#".$fa_icon."'}))"; |
|
1557 | + } |
|
1558 | + |
|
1559 | + return $icon; |
|
1560 | + } |
|
1561 | + |
|
1562 | + public function group_arguments($arguments){ |
|
1563 | 1563 | // echo '###';print_r($arguments); |
1564 | - if(!empty($arguments)){ |
|
1565 | - $temp_arguments = array(); |
|
1566 | - $general = __("General"); |
|
1567 | - $add_sections = false; |
|
1568 | - foreach($arguments as $key => $args){ |
|
1569 | - if(isset($args['group'])){ |
|
1570 | - $temp_arguments[$args['group']][$key] = $args; |
|
1571 | - $add_sections = true; |
|
1572 | - }else{ |
|
1573 | - $temp_arguments[$general][$key] = $args; |
|
1574 | - } |
|
1575 | - } |
|
1576 | - |
|
1577 | - // only add sections if more than one |
|
1578 | - if($add_sections){ |
|
1579 | - $arguments = $temp_arguments; |
|
1580 | - } |
|
1581 | - } |
|
1564 | + if(!empty($arguments)){ |
|
1565 | + $temp_arguments = array(); |
|
1566 | + $general = __("General"); |
|
1567 | + $add_sections = false; |
|
1568 | + foreach($arguments as $key => $args){ |
|
1569 | + if(isset($args['group'])){ |
|
1570 | + $temp_arguments[$args['group']][$key] = $args; |
|
1571 | + $add_sections = true; |
|
1572 | + }else{ |
|
1573 | + $temp_arguments[$general][$key] = $args; |
|
1574 | + } |
|
1575 | + } |
|
1576 | + |
|
1577 | + // only add sections if more than one |
|
1578 | + if($add_sections){ |
|
1579 | + $arguments = $temp_arguments; |
|
1580 | + } |
|
1581 | + } |
|
1582 | 1582 | // echo '###';print_r($arguments); |
1583 | - return $arguments; |
|
1584 | - } |
|
1585 | - |
|
1586 | - |
|
1587 | - /** |
|
1588 | - * Output the JS for building the dynamic Guntenberg block. |
|
1589 | - * |
|
1590 | - * @since 1.0.4 Added block_wrap property which will set the block wrapping output element ie: div, span, p or empty for no wrap. |
|
1591 | - * @since 1.0.9 Save numbers as numbers and not strings. |
|
1592 | - * @since 1.1.0 Font Awesome classes can be used for icons. |
|
1593 | - * @return mixed |
|
1594 | - */ |
|
1595 | - public function block() { |
|
1596 | - ob_start(); |
|
1597 | - ?> |
|
1583 | + return $arguments; |
|
1584 | + } |
|
1585 | + |
|
1586 | + |
|
1587 | + /** |
|
1588 | + * Output the JS for building the dynamic Guntenberg block. |
|
1589 | + * |
|
1590 | + * @since 1.0.4 Added block_wrap property which will set the block wrapping output element ie: div, span, p or empty for no wrap. |
|
1591 | + * @since 1.0.9 Save numbers as numbers and not strings. |
|
1592 | + * @since 1.1.0 Font Awesome classes can be used for icons. |
|
1593 | + * @return mixed |
|
1594 | + */ |
|
1595 | + public function block() { |
|
1596 | + ob_start(); |
|
1597 | + ?> |
|
1598 | 1598 | <script> |
1599 | 1599 | /** |
1600 | 1600 | * BLOCK: Basic |
@@ -1633,93 +1633,93 @@ discard block |
||
1633 | 1633 | icon: <?php echo $this->get_block_icon($this->options['block-icon']);?>,//'<?php echo isset( $this->options['block-icon'] ) ? esc_attr( $this->options['block-icon'] ) : 'shield-alt';?>', // Block icon from Dashicons → https://developer.wordpress.org/resource/dashicons/. |
1634 | 1634 | supports: { |
1635 | 1635 | <?php |
1636 | - if(isset($this->options['block-supports'])){ |
|
1637 | - echo $this->array_to_attributes( $this->options['block-supports'] ); |
|
1638 | - } |
|
1639 | - ?> |
|
1636 | + if(isset($this->options['block-supports'])){ |
|
1637 | + echo $this->array_to_attributes( $this->options['block-supports'] ); |
|
1638 | + } |
|
1639 | + ?> |
|
1640 | 1640 | }, |
1641 | 1641 | category: '<?php echo isset( $this->options['block-category'] ) ? esc_attr( $this->options['block-category'] ) : 'common';?>', // Block category — Group blocks together based on common traits E.g. common, formatting, layout widgets, embed. |
1642 | 1642 | <?php if ( isset( $this->options['block-keywords'] ) ) { |
1643 | - echo "keywords : " . $this->options['block-keywords'] . ","; |
|
1644 | - }?> |
|
1643 | + echo "keywords : " . $this->options['block-keywords'] . ","; |
|
1644 | + }?> |
|
1645 | 1645 | |
1646 | 1646 | <?php |
1647 | 1647 | |
1648 | - // maybe set no_wrap |
|
1649 | - $no_wrap = isset( $this->options['no_wrap'] ) && $this->options['no_wrap'] ? true : false; |
|
1650 | - if( isset( $this->arguments['no_wrap'] ) && $this->arguments['no_wrap'] ){ $no_wrap = true; } |
|
1651 | - if( $no_wrap ){ $this->options['block-wrap'] = ''; } |
|
1648 | + // maybe set no_wrap |
|
1649 | + $no_wrap = isset( $this->options['no_wrap'] ) && $this->options['no_wrap'] ? true : false; |
|
1650 | + if( isset( $this->arguments['no_wrap'] ) && $this->arguments['no_wrap'] ){ $no_wrap = true; } |
|
1651 | + if( $no_wrap ){ $this->options['block-wrap'] = ''; } |
|
1652 | 1652 | |
1653 | - $show_advanced = $this->block_show_advanced(); |
|
1653 | + $show_advanced = $this->block_show_advanced(); |
|
1654 | 1654 | |
1655 | - $show_alignment = false; |
|
1656 | - // align feature |
|
1657 | - /*echo "supports: {"; |
|
1655 | + $show_alignment = false; |
|
1656 | + // align feature |
|
1657 | + /*echo "supports: {"; |
|
1658 | 1658 | echo " align: true,"; |
1659 | 1659 | echo " html: false"; |
1660 | 1660 | echo "},";*/ |
1661 | 1661 | |
1662 | - if ( ! empty( $this->arguments ) ) { |
|
1663 | - echo "attributes : {"; |
|
1664 | - |
|
1665 | - if ( $show_advanced ) { |
|
1666 | - echo "show_advanced: {"; |
|
1667 | - echo " type: 'boolean',"; |
|
1668 | - echo " default: false,"; |
|
1669 | - echo "},"; |
|
1670 | - } |
|
1671 | - |
|
1672 | - // block wrap element |
|
1673 | - if ( !empty( $this->options['block-wrap'] ) ) { //@todo we should validate this? |
|
1674 | - echo "block_wrap: {"; |
|
1675 | - echo " type: 'string',"; |
|
1676 | - echo " default: '" . esc_attr( $this->options['block-wrap'] ) . "',"; |
|
1677 | - echo "},"; |
|
1678 | - } |
|
1679 | - |
|
1680 | - foreach ( $this->arguments as $key => $args ) { |
|
1681 | - |
|
1682 | - // set if we should show alignment |
|
1683 | - if ( $key == 'alignment' ) { |
|
1684 | - $show_alignment = true; |
|
1685 | - } |
|
1686 | - |
|
1687 | - $extra = ''; |
|
1688 | - |
|
1689 | - if ( $args['type'] == 'checkbox' ) { |
|
1690 | - $type = 'boolean'; |
|
1691 | - $default = isset( $args['default'] ) && $args['default'] ? 'true' : 'false'; |
|
1692 | - } elseif ( $args['type'] == 'number' ) { |
|
1693 | - $type = 'number'; |
|
1694 | - $default = isset( $args['default'] ) ? "'" . $args['default'] . "'" : "''"; |
|
1695 | - } elseif ( $args['type'] == 'select' && ! empty( $args['multiple'] ) ) { |
|
1696 | - $type = 'array'; |
|
1697 | - if ( is_array( $args['default'] ) ) { |
|
1698 | - $default = isset( $args['default'] ) ? "['" . implode( "','", $args['default'] ) . "']" : "[]"; |
|
1699 | - } else { |
|
1700 | - $default = isset( $args['default'] ) ? "'" . $args['default'] . "'" : "''"; |
|
1701 | - } |
|
1702 | - } elseif ( $args['type'] == 'multiselect' ) { |
|
1703 | - $type = 'array'; |
|
1704 | - $default = isset( $args['default'] ) ? "'" . $args['default'] . "'" : "''"; |
|
1705 | - } else { |
|
1706 | - $type = 'string'; |
|
1707 | - $default = isset( $args['default'] ) ? "'" . $args['default'] . "'" : "''"; |
|
1708 | - } |
|
1709 | - echo $key . " : {"; |
|
1710 | - echo "type : '$type',"; |
|
1711 | - echo "default : $default,"; |
|
1712 | - echo "},"; |
|
1713 | - } |
|
1714 | - |
|
1715 | - echo "content : {type : 'string',default: 'Please select the attributes in the block settings'},"; |
|
1716 | - echo "className: { type: 'string', default: '' },"; |
|
1717 | - |
|
1718 | - echo "},"; |
|
1719 | - |
|
1720 | - } |
|
1721 | - |
|
1722 | - ?> |
|
1662 | + if ( ! empty( $this->arguments ) ) { |
|
1663 | + echo "attributes : {"; |
|
1664 | + |
|
1665 | + if ( $show_advanced ) { |
|
1666 | + echo "show_advanced: {"; |
|
1667 | + echo " type: 'boolean',"; |
|
1668 | + echo " default: false,"; |
|
1669 | + echo "},"; |
|
1670 | + } |
|
1671 | + |
|
1672 | + // block wrap element |
|
1673 | + if ( !empty( $this->options['block-wrap'] ) ) { //@todo we should validate this? |
|
1674 | + echo "block_wrap: {"; |
|
1675 | + echo " type: 'string',"; |
|
1676 | + echo " default: '" . esc_attr( $this->options['block-wrap'] ) . "',"; |
|
1677 | + echo "},"; |
|
1678 | + } |
|
1679 | + |
|
1680 | + foreach ( $this->arguments as $key => $args ) { |
|
1681 | + |
|
1682 | + // set if we should show alignment |
|
1683 | + if ( $key == 'alignment' ) { |
|
1684 | + $show_alignment = true; |
|
1685 | + } |
|
1686 | + |
|
1687 | + $extra = ''; |
|
1688 | + |
|
1689 | + if ( $args['type'] == 'checkbox' ) { |
|
1690 | + $type = 'boolean'; |
|
1691 | + $default = isset( $args['default'] ) && $args['default'] ? 'true' : 'false'; |
|
1692 | + } elseif ( $args['type'] == 'number' ) { |
|
1693 | + $type = 'number'; |
|
1694 | + $default = isset( $args['default'] ) ? "'" . $args['default'] . "'" : "''"; |
|
1695 | + } elseif ( $args['type'] == 'select' && ! empty( $args['multiple'] ) ) { |
|
1696 | + $type = 'array'; |
|
1697 | + if ( is_array( $args['default'] ) ) { |
|
1698 | + $default = isset( $args['default'] ) ? "['" . implode( "','", $args['default'] ) . "']" : "[]"; |
|
1699 | + } else { |
|
1700 | + $default = isset( $args['default'] ) ? "'" . $args['default'] . "'" : "''"; |
|
1701 | + } |
|
1702 | + } elseif ( $args['type'] == 'multiselect' ) { |
|
1703 | + $type = 'array'; |
|
1704 | + $default = isset( $args['default'] ) ? "'" . $args['default'] . "'" : "''"; |
|
1705 | + } else { |
|
1706 | + $type = 'string'; |
|
1707 | + $default = isset( $args['default'] ) ? "'" . $args['default'] . "'" : "''"; |
|
1708 | + } |
|
1709 | + echo $key . " : {"; |
|
1710 | + echo "type : '$type',"; |
|
1711 | + echo "default : $default,"; |
|
1712 | + echo "},"; |
|
1713 | + } |
|
1714 | + |
|
1715 | + echo "content : {type : 'string',default: 'Please select the attributes in the block settings'},"; |
|
1716 | + echo "className: { type: 'string', default: '' },"; |
|
1717 | + |
|
1718 | + echo "},"; |
|
1719 | + |
|
1720 | + } |
|
1721 | + |
|
1722 | + ?> |
|
1723 | 1723 | |
1724 | 1724 | // The "edit" property must be a valid function. |
1725 | 1725 | edit: function (props) { |
@@ -1738,8 +1738,8 @@ discard block |
||
1738 | 1738 | 'shortcode': '<?php echo $this->options['base_id'];?>', |
1739 | 1739 | 'attributes': props.attributes, |
1740 | 1740 | 'post_id': <?php global $post; if ( isset( $post->ID ) ) { |
1741 | - echo $post->ID; |
|
1742 | - }?>, |
|
1741 | + echo $post->ID; |
|
1742 | + }?>, |
|
1743 | 1743 | '_ajax_nonce': '<?php echo wp_create_nonce( 'super_duper_output_shortcode' );?>' |
1744 | 1744 | }; |
1745 | 1745 | |
@@ -1786,10 +1786,10 @@ discard block |
||
1786 | 1786 | |
1787 | 1787 | <?php |
1788 | 1788 | |
1789 | - if(! empty( $this->arguments )){ |
|
1789 | + if(! empty( $this->arguments )){ |
|
1790 | 1790 | |
1791 | - if ( $show_advanced ) { |
|
1792 | - ?> |
|
1791 | + if ( $show_advanced ) { |
|
1792 | + ?> |
|
1793 | 1793 | el( |
1794 | 1794 | wp.components.ToggleControl, |
1795 | 1795 | { |
@@ -1802,61 +1802,61 @@ discard block |
||
1802 | 1802 | ), |
1803 | 1803 | <?php |
1804 | 1804 | |
1805 | - } |
|
1805 | + } |
|
1806 | 1806 | |
1807 | - $arguments = $this->group_arguments($this->arguments); |
|
1807 | + $arguments = $this->group_arguments($this->arguments); |
|
1808 | 1808 | |
1809 | - // Do we have sections? |
|
1810 | - $has_sections = $arguments == $this->arguments ? false : true; |
|
1809 | + // Do we have sections? |
|
1810 | + $has_sections = $arguments == $this->arguments ? false : true; |
|
1811 | 1811 | |
1812 | 1812 | |
1813 | - if($has_sections){ |
|
1814 | - $panel_count = 0; |
|
1815 | - foreach($arguments as $key => $args){ |
|
1816 | - ?> |
|
1813 | + if($has_sections){ |
|
1814 | + $panel_count = 0; |
|
1815 | + foreach($arguments as $key => $args){ |
|
1816 | + ?> |
|
1817 | 1817 | el(wp.components.PanelBody, { |
1818 | 1818 | title: '<?php esc_attr_e($key); ?>', |
1819 | 1819 | initialOpen: <?php if($panel_count){echo "false";}else{echo "true";}?> |
1820 | 1820 | }, |
1821 | 1821 | <?php |
1822 | 1822 | |
1823 | - foreach($args as $k => $a){ |
|
1824 | - $this->build_block_arguments($k, $a); |
|
1825 | - } |
|
1826 | - ?> |
|
1823 | + foreach($args as $k => $a){ |
|
1824 | + $this->build_block_arguments($k, $a); |
|
1825 | + } |
|
1826 | + ?> |
|
1827 | 1827 | ), |
1828 | 1828 | <?php |
1829 | - $panel_count++; |
|
1829 | + $panel_count++; |
|
1830 | 1830 | |
1831 | - } |
|
1832 | - }else{ |
|
1833 | - foreach($this->arguments as $key => $args){ |
|
1834 | - $this->build_block_arguments($key, $args); |
|
1835 | - } |
|
1836 | - } |
|
1831 | + } |
|
1832 | + }else{ |
|
1833 | + foreach($this->arguments as $key => $args){ |
|
1834 | + $this->build_block_arguments($key, $args); |
|
1835 | + } |
|
1836 | + } |
|
1837 | 1837 | |
1838 | 1838 | |
1839 | 1839 | |
1840 | - } |
|
1841 | - ?> |
|
1840 | + } |
|
1841 | + ?> |
|
1842 | 1842 | |
1843 | 1843 | ), |
1844 | 1844 | |
1845 | 1845 | <?php |
1846 | - // If the user sets block-output array then build it |
|
1847 | - if ( ! empty( $this->options['block-output'] ) ) { |
|
1848 | - $this->block_element( $this->options['block-output'] ); |
|
1849 | - }else{ |
|
1850 | - // if no block-output is set then we try and get the shortcode html output via ajax. |
|
1851 | - ?> |
|
1846 | + // If the user sets block-output array then build it |
|
1847 | + if ( ! empty( $this->options['block-output'] ) ) { |
|
1848 | + $this->block_element( $this->options['block-output'] ); |
|
1849 | + }else{ |
|
1850 | + // if no block-output is set then we try and get the shortcode html output via ajax. |
|
1851 | + ?> |
|
1852 | 1852 | el('div', { |
1853 | 1853 | dangerouslySetInnerHTML: {__html: onChangeContent()}, |
1854 | 1854 | className: props.className, |
1855 | 1855 | style: {'min-height': '30px'} |
1856 | 1856 | }) |
1857 | 1857 | <?php |
1858 | - } |
|
1859 | - ?> |
|
1858 | + } |
|
1859 | + ?> |
|
1860 | 1860 | ]; // end return |
1861 | 1861 | }, |
1862 | 1862 | |
@@ -1874,10 +1874,10 @@ discard block |
||
1874 | 1874 | $html = ''; |
1875 | 1875 | <?php |
1876 | 1876 | |
1877 | - if(! empty( $this->arguments )){ |
|
1877 | + if(! empty( $this->arguments )){ |
|
1878 | 1878 | |
1879 | - foreach($this->arguments as $key => $args){ |
|
1880 | - ?> |
|
1879 | + foreach($this->arguments as $key => $args){ |
|
1880 | + ?> |
|
1881 | 1881 | if (attr.hasOwnProperty("<?php echo esc_attr( $key );?>")) { |
1882 | 1882 | if('<?php echo esc_attr( $key );?>' == 'html'){ |
1883 | 1883 | $html = attr.<?php echo esc_attr( $key );?>; |
@@ -1886,10 +1886,10 @@ discard block |
||
1886 | 1886 | } |
1887 | 1887 | } |
1888 | 1888 | <?php |
1889 | - } |
|
1890 | - } |
|
1889 | + } |
|
1890 | + } |
|
1891 | 1891 | |
1892 | - ?> |
|
1892 | + ?> |
|
1893 | 1893 | content += "]"; |
1894 | 1894 | |
1895 | 1895 | // if has html element |
@@ -1912,20 +1912,20 @@ discard block |
||
1912 | 1912 | } |
1913 | 1913 | |
1914 | 1914 | <?php |
1915 | - if(isset( $this->options['block-wrap'] ) && $this->options['block-wrap'] == ''){ |
|
1916 | - ?> |
|
1915 | + if(isset( $this->options['block-wrap'] ) && $this->options['block-wrap'] == ''){ |
|
1916 | + ?> |
|
1917 | 1917 | return content; |
1918 | 1918 | <?php |
1919 | - }else{ |
|
1920 | - ?> |
|
1919 | + }else{ |
|
1920 | + ?> |
|
1921 | 1921 | var block_wrap = 'div'; |
1922 | 1922 | if (attr.hasOwnProperty("block_wrap")) { |
1923 | 1923 | block_wrap = attr.block_wrap; |
1924 | 1924 | } |
1925 | 1925 | return el(block_wrap, {dangerouslySetInnerHTML: {__html: content}, className: align}); |
1926 | 1926 | <?php |
1927 | - } |
|
1928 | - ?> |
|
1927 | + } |
|
1928 | + ?> |
|
1929 | 1929 | |
1930 | 1930 | |
1931 | 1931 | } |
@@ -1933,89 +1933,89 @@ discard block |
||
1933 | 1933 | })(); |
1934 | 1934 | </script> |
1935 | 1935 | <?php |
1936 | - $output = ob_get_clean(); |
|
1936 | + $output = ob_get_clean(); |
|
1937 | 1937 | |
1938 | - /* |
|
1938 | + /* |
|
1939 | 1939 | * We only add the <script> tags for code highlighting, so we strip them from the output. |
1940 | 1940 | */ |
1941 | 1941 | |
1942 | - return str_replace( array( |
|
1943 | - '<script>', |
|
1944 | - '</script>' |
|
1945 | - ), '', $output ); |
|
1946 | - } |
|
1947 | - |
|
1948 | - public function build_block_arguments($key,$args){ |
|
1949 | - $custom_attributes = ! empty( $args['custom_attributes'] ) ? $this->array_to_attributes( $args['custom_attributes'] ) : ''; |
|
1950 | - $options = ''; |
|
1951 | - $extra = ''; |
|
1952 | - $require = ''; |
|
1953 | - |
|
1954 | - // `content` is a protected and special argument |
|
1955 | - if($key == 'content'){return;} |
|
1956 | - |
|
1957 | - $onchange = "props.setAttributes({ $key: $key } )"; |
|
1958 | - $value = "props.attributes.$key"; |
|
1959 | - $text_type = array( 'text', 'password', 'number', 'email', 'tel', 'url', 'color' ); |
|
1960 | - if ( in_array( $args['type'], $text_type ) ) { |
|
1961 | - $type = 'TextControl'; |
|
1962 | - // Save numbers as numbers and not strings |
|
1963 | - if ( $args['type'] == 'number' ) { |
|
1964 | - $onchange = "props.setAttributes({ $key: Number($key) } )"; |
|
1965 | - } |
|
1966 | - } |
|
1967 | - // elseif ( $args['type'] == 'color' ) { //@todo ColorPicker labels are not shown yet, we use html5 color input for now https://github.com/WordPress/gutenberg/issues/14378 |
|
1968 | - // $type = 'ColorPicker'; |
|
1969 | - // } |
|
1970 | - elseif ( $args['type'] == 'checkbox' ) { |
|
1971 | - $type = 'CheckboxControl'; |
|
1972 | - $extra .= "checked: props.attributes.$key,"; |
|
1973 | - $onchange = "props.setAttributes({ $key: ! props.attributes.$key } )"; |
|
1974 | - }elseif ( $args['type'] == 'textarea' ) { |
|
1975 | - $type = 'TextareaControl'; |
|
1976 | - } elseif ( $args['type'] == 'select' || $args['type'] == 'multiselect' ) { |
|
1977 | - $type = 'SelectControl'; |
|
1978 | - if ( ! empty( $args['options'] ) ) { |
|
1979 | - $options .= "options : ["; |
|
1980 | - foreach ( $args['options'] as $option_val => $option_label ) { |
|
1981 | - $options .= "{ value : '" . esc_attr( $option_val ) . "', label : '" . esc_attr( $option_label ) . "' },"; |
|
1982 | - } |
|
1983 | - $options .= "],"; |
|
1984 | - } |
|
1985 | - if ( isset( $args['multiple'] ) && $args['multiple'] ) { //@todo multiselect does not work at the moment: https://github.com/WordPress/gutenberg/issues/5550 |
|
1986 | - $extra .= ' multiple: true, '; |
|
1987 | - //$onchange = "props.setAttributes({ $key: ['edit'] } )"; |
|
1988 | - //$value = "['edit', 'delete']"; |
|
1989 | - } |
|
1990 | - } elseif ( $args['type'] == 'alignment' ) { |
|
1991 | - $type = 'AlignmentToolbar'; // @todo this does not seem to work but cant find a example |
|
1992 | - } else { |
|
1993 | - return;// if we have not implemented the control then don't break the JS. |
|
1994 | - } |
|
1995 | - |
|
1996 | - // add show only if advanced |
|
1997 | - if ( ! empty( $args['advanced'] ) ) { |
|
1998 | - echo "props.attributes.show_advanced && "; |
|
1999 | - } |
|
2000 | - // add setting require if defined |
|
2001 | - if ( ! empty( $args['element_require'] ) ) { |
|
2002 | - echo $this->block_props_replace( $args['element_require'], true ) . " && "; |
|
2003 | - } |
|
2004 | - ?> |
|
1942 | + return str_replace( array( |
|
1943 | + '<script>', |
|
1944 | + '</script>' |
|
1945 | + ), '', $output ); |
|
1946 | + } |
|
1947 | + |
|
1948 | + public function build_block_arguments($key,$args){ |
|
1949 | + $custom_attributes = ! empty( $args['custom_attributes'] ) ? $this->array_to_attributes( $args['custom_attributes'] ) : ''; |
|
1950 | + $options = ''; |
|
1951 | + $extra = ''; |
|
1952 | + $require = ''; |
|
1953 | + |
|
1954 | + // `content` is a protected and special argument |
|
1955 | + if($key == 'content'){return;} |
|
1956 | + |
|
1957 | + $onchange = "props.setAttributes({ $key: $key } )"; |
|
1958 | + $value = "props.attributes.$key"; |
|
1959 | + $text_type = array( 'text', 'password', 'number', 'email', 'tel', 'url', 'color' ); |
|
1960 | + if ( in_array( $args['type'], $text_type ) ) { |
|
1961 | + $type = 'TextControl'; |
|
1962 | + // Save numbers as numbers and not strings |
|
1963 | + if ( $args['type'] == 'number' ) { |
|
1964 | + $onchange = "props.setAttributes({ $key: Number($key) } )"; |
|
1965 | + } |
|
1966 | + } |
|
1967 | + // elseif ( $args['type'] == 'color' ) { //@todo ColorPicker labels are not shown yet, we use html5 color input for now https://github.com/WordPress/gutenberg/issues/14378 |
|
1968 | + // $type = 'ColorPicker'; |
|
1969 | + // } |
|
1970 | + elseif ( $args['type'] == 'checkbox' ) { |
|
1971 | + $type = 'CheckboxControl'; |
|
1972 | + $extra .= "checked: props.attributes.$key,"; |
|
1973 | + $onchange = "props.setAttributes({ $key: ! props.attributes.$key } )"; |
|
1974 | + }elseif ( $args['type'] == 'textarea' ) { |
|
1975 | + $type = 'TextareaControl'; |
|
1976 | + } elseif ( $args['type'] == 'select' || $args['type'] == 'multiselect' ) { |
|
1977 | + $type = 'SelectControl'; |
|
1978 | + if ( ! empty( $args['options'] ) ) { |
|
1979 | + $options .= "options : ["; |
|
1980 | + foreach ( $args['options'] as $option_val => $option_label ) { |
|
1981 | + $options .= "{ value : '" . esc_attr( $option_val ) . "', label : '" . esc_attr( $option_label ) . "' },"; |
|
1982 | + } |
|
1983 | + $options .= "],"; |
|
1984 | + } |
|
1985 | + if ( isset( $args['multiple'] ) && $args['multiple'] ) { //@todo multiselect does not work at the moment: https://github.com/WordPress/gutenberg/issues/5550 |
|
1986 | + $extra .= ' multiple: true, '; |
|
1987 | + //$onchange = "props.setAttributes({ $key: ['edit'] } )"; |
|
1988 | + //$value = "['edit', 'delete']"; |
|
1989 | + } |
|
1990 | + } elseif ( $args['type'] == 'alignment' ) { |
|
1991 | + $type = 'AlignmentToolbar'; // @todo this does not seem to work but cant find a example |
|
1992 | + } else { |
|
1993 | + return;// if we have not implemented the control then don't break the JS. |
|
1994 | + } |
|
1995 | + |
|
1996 | + // add show only if advanced |
|
1997 | + if ( ! empty( $args['advanced'] ) ) { |
|
1998 | + echo "props.attributes.show_advanced && "; |
|
1999 | + } |
|
2000 | + // add setting require if defined |
|
2001 | + if ( ! empty( $args['element_require'] ) ) { |
|
2002 | + echo $this->block_props_replace( $args['element_require'], true ) . " && "; |
|
2003 | + } |
|
2004 | + ?> |
|
2005 | 2005 | el( |
2006 | 2006 | wp.components.<?php echo esc_attr( $type );?>, |
2007 | 2007 | { |
2008 | 2008 | label: '<?php echo esc_attr( $args['title'] );?>', |
2009 | 2009 | help: '<?php if ( isset( $args['desc'] ) ) { |
2010 | - echo esc_attr( $args['desc'] ); |
|
2011 | - }?>', |
|
2010 | + echo esc_attr( $args['desc'] ); |
|
2011 | + }?>', |
|
2012 | 2012 | value: <?php echo $value;?>, |
2013 | 2013 | <?php if ( $type == 'TextControl' && $args['type'] != 'text' ) { |
2014 | - echo "type: '" . esc_attr( $args['type'] ) . "',"; |
|
2015 | - }?> |
|
2014 | + echo "type: '" . esc_attr( $args['type'] ) . "',"; |
|
2015 | + }?> |
|
2016 | 2016 | <?php if ( ! empty( $args['placeholder'] ) ) { |
2017 | - echo "placeholder: '" . esc_attr( $args['placeholder'] ) . "',"; |
|
2018 | - }?> |
|
2017 | + echo "placeholder: '" . esc_attr( $args['placeholder'] ) . "',"; |
|
2018 | + }?> |
|
2019 | 2019 | <?php echo $options;?> |
2020 | 2020 | <?php echo $extra;?> |
2021 | 2021 | <?php echo $custom_attributes;?> |
@@ -2025,508 +2025,508 @@ discard block |
||
2025 | 2025 | } |
2026 | 2026 | ), |
2027 | 2027 | <?php |
2028 | - } |
|
2029 | - |
|
2030 | - /** |
|
2031 | - * Convert an array of attributes to block string. |
|
2032 | - * |
|
2033 | - * @todo there is prob a faster way to do this, also we could add some validation here. |
|
2034 | - * |
|
2035 | - * @param $custom_attributes |
|
2036 | - * |
|
2037 | - * @return string |
|
2038 | - */ |
|
2039 | - public function array_to_attributes( $custom_attributes, $html = false ) { |
|
2040 | - $attributes = ''; |
|
2041 | - if ( ! empty( $custom_attributes ) ) { |
|
2042 | - |
|
2043 | - if ( $html ) { |
|
2044 | - foreach ( $custom_attributes as $key => $val ) { |
|
2045 | - $attributes .= " $key='$val' "; |
|
2046 | - } |
|
2047 | - } else { |
|
2048 | - foreach ( $custom_attributes as $key => $val ) { |
|
2049 | - $attributes .= "'$key': '$val',"; |
|
2050 | - } |
|
2051 | - } |
|
2052 | - } |
|
2053 | - |
|
2054 | - return $attributes; |
|
2055 | - } |
|
2056 | - |
|
2057 | - /** |
|
2058 | - * A self looping function to create the output for JS block elements. |
|
2059 | - * |
|
2060 | - * This is what is output in the WP Editor visual view. |
|
2061 | - * |
|
2062 | - * @param $args |
|
2063 | - */ |
|
2064 | - public function block_element( $args ) { |
|
2065 | - |
|
2066 | - |
|
2067 | - if ( ! empty( $args ) ) { |
|
2068 | - foreach ( $args as $element => $new_args ) { |
|
2069 | - |
|
2070 | - if ( is_array( $new_args ) ) { // its an element |
|
2071 | - |
|
2072 | - |
|
2073 | - if ( isset( $new_args['element'] ) ) { |
|
2074 | - |
|
2075 | - if ( isset( $new_args['element_require'] ) ) { |
|
2076 | - echo str_replace( array( |
|
2077 | - "'+", |
|
2078 | - "+'" |
|
2079 | - ), '', $this->block_props_replace( $new_args['element_require'] ) ) . " && "; |
|
2080 | - unset( $new_args['element_require'] ); |
|
2081 | - } |
|
2082 | - |
|
2083 | - echo "\n el( '" . $new_args['element'] . "', {"; |
|
2084 | - |
|
2085 | - // get the attributes |
|
2086 | - foreach ( $new_args as $new_key => $new_value ) { |
|
2087 | - |
|
2088 | - |
|
2089 | - if ( $new_key == 'element' || $new_key == 'content' || $new_key == 'element_require' || $new_key == 'element_repeat' || is_array( $new_value ) ) { |
|
2090 | - // do nothing |
|
2091 | - } else { |
|
2092 | - echo $this->block_element( array( $new_key => $new_value ) ); |
|
2093 | - } |
|
2094 | - } |
|
2095 | - |
|
2096 | - echo "},";// end attributes |
|
2097 | - |
|
2098 | - // get the content |
|
2099 | - $first_item = 0; |
|
2100 | - foreach ( $new_args as $new_key => $new_value ) { |
|
2101 | - if ( $new_key === 'content' || is_array( $new_value ) ) { |
|
2102 | - |
|
2103 | - if ( $new_key === 'content' ) { |
|
2104 | - echo "'" . $this->block_props_replace( wp_slash( $new_value ) ) . "'"; |
|
2105 | - } |
|
2106 | - |
|
2107 | - if ( is_array( $new_value ) ) { |
|
2108 | - |
|
2109 | - if ( isset( $new_value['element_require'] ) ) { |
|
2110 | - echo str_replace( array( |
|
2111 | - "'+", |
|
2112 | - "+'" |
|
2113 | - ), '', $this->block_props_replace( $new_value['element_require'] ) ) . " && "; |
|
2114 | - unset( $new_value['element_require'] ); |
|
2115 | - } |
|
2116 | - |
|
2117 | - if ( isset( $new_value['element_repeat'] ) ) { |
|
2118 | - $x = 1; |
|
2119 | - while ( $x <= absint( $new_value['element_repeat'] ) ) { |
|
2120 | - $this->block_element( array( '' => $new_value ) ); |
|
2121 | - $x ++; |
|
2122 | - } |
|
2123 | - } else { |
|
2124 | - $this->block_element( array( '' => $new_value ) ); |
|
2125 | - } |
|
2126 | - } |
|
2127 | - $first_item ++; |
|
2128 | - } |
|
2129 | - } |
|
2130 | - |
|
2131 | - echo ")";// end content |
|
2132 | - |
|
2133 | - echo ", \n"; |
|
2134 | - |
|
2135 | - } |
|
2136 | - } else { |
|
2137 | - |
|
2138 | - if ( substr( $element, 0, 3 ) === "if_" ) { |
|
2139 | - echo str_replace( "if_", "", $element ) . ": " . $this->block_props_replace( $new_args, true ) . ","; |
|
2140 | - } elseif ( $element == 'style' ) { |
|
2141 | - echo $element . ": " . $this->block_props_replace( $new_args ) . ","; |
|
2142 | - } else { |
|
2143 | - echo $element . ": '" . $this->block_props_replace( $new_args ) . "',"; |
|
2144 | - } |
|
2145 | - |
|
2146 | - } |
|
2147 | - } |
|
2148 | - } |
|
2149 | - } |
|
2150 | - |
|
2151 | - /** |
|
2152 | - * Replace block attributes placeholders with the proper naming. |
|
2153 | - * |
|
2154 | - * @param $string |
|
2155 | - * |
|
2156 | - * @return mixed |
|
2157 | - */ |
|
2158 | - public function block_props_replace( $string, $no_wrap = false ) { |
|
2159 | - |
|
2160 | - if ( $no_wrap ) { |
|
2161 | - $string = str_replace( array( "[%", "%]" ), array( "props.attributes.", "" ), $string ); |
|
2162 | - } else { |
|
2163 | - $string = str_replace( array( "[%", "%]" ), array( "'+props.attributes.", "+'" ), $string ); |
|
2164 | - } |
|
2165 | - |
|
2166 | - return $string; |
|
2167 | - } |
|
2168 | - |
|
2169 | - /** |
|
2170 | - * Outputs the content of the widget |
|
2171 | - * |
|
2172 | - * @param array $args |
|
2173 | - * @param array $instance |
|
2174 | - */ |
|
2175 | - public function widget( $args, $instance ) { |
|
2176 | - |
|
2177 | - // get the filtered values |
|
2178 | - $argument_values = $this->argument_values( $instance ); |
|
2179 | - $argument_values = $this->string_to_bool( $argument_values ); |
|
2180 | - $output = $this->output( $argument_values, $args ); |
|
2181 | - |
|
2182 | - $no_wrap = false; |
|
2183 | - if( isset( $argument_values['no_wrap'] ) && $argument_values['no_wrap'] ){ $no_wrap = true; } |
|
2184 | - |
|
2185 | - ob_start(); |
|
2186 | - if ( $output && !$no_wrap) { |
|
2187 | - // Before widget |
|
2188 | - $before_widget = $args['before_widget']; |
|
2189 | - $before_widget = apply_filters( 'wp_super_duper_before_widget', $before_widget, $args, $instance, $this ); |
|
2190 | - $before_widget = apply_filters( 'wp_super_duper_before_widget_' . $this->base_id, $before_widget, $args, $instance, $this ); |
|
2191 | - |
|
2192 | - // After widget |
|
2193 | - $after_widget = $args['after_widget']; |
|
2194 | - $after_widget = apply_filters( 'wp_super_duper_after_widget', $after_widget, $args, $instance, $this ); |
|
2195 | - $after_widget = apply_filters( 'wp_super_duper_after_widget_' . $this->base_id, $after_widget, $args, $instance, $this ); |
|
2196 | - |
|
2197 | - echo $before_widget; |
|
2198 | - // elementor strips the widget wrapping div so we check for and add it back if needed |
|
2199 | - if ( $this->is_elementor_widget_output() ) { |
|
2200 | - echo ! empty( $this->options['widget_ops']['classname'] ) ? "<span class='" . esc_attr( $this->options['widget_ops']['classname'] ) . "'>" : ''; |
|
2201 | - } |
|
2202 | - echo $this->output_title( $args, $instance ); |
|
2203 | - echo $output; |
|
2204 | - if ( $this->is_elementor_widget_output() ) { |
|
2205 | - echo ! empty( $this->options['widget_ops']['classname'] ) ? "</span>" : ''; |
|
2206 | - } |
|
2207 | - echo $after_widget; |
|
2208 | - } elseif ( $this->is_preview() && $output == '' ) {// if preview show a placeholder if empty |
|
2209 | - $output = $this->preview_placeholder_text( "{{" . $this->base_id . "}}" ); |
|
2210 | - echo $output; |
|
2211 | - } elseif($output && $no_wrap){ |
|
2212 | - echo $output; |
|
2213 | - } |
|
2214 | - $output = ob_get_clean(); |
|
2215 | - |
|
2216 | - $output = apply_filters( 'wp_super_duper_widget_output', $output, $instance, $args, $this ); |
|
2217 | - |
|
2218 | - echo $output; |
|
2219 | - } |
|
2220 | - |
|
2221 | - /** |
|
2222 | - * Tests if the current output is inside a elementor container. |
|
2223 | - * |
|
2224 | - * @since 1.0.4 |
|
2225 | - * @return bool |
|
2226 | - */ |
|
2227 | - public function is_elementor_widget_output() { |
|
2228 | - $result = false; |
|
2229 | - if ( defined( 'ELEMENTOR_VERSION' ) && isset( $this->number ) && $this->number == 'REPLACE_TO_ID' ) { |
|
2230 | - $result = true; |
|
2231 | - } |
|
2232 | - |
|
2233 | - return $result; |
|
2234 | - } |
|
2235 | - |
|
2236 | - /** |
|
2237 | - * Tests if the current output is inside a elementor preview. |
|
2238 | - * |
|
2239 | - * @since 1.0.4 |
|
2240 | - * @return bool |
|
2241 | - */ |
|
2242 | - public function is_elementor_preview() { |
|
2243 | - $result = false; |
|
2244 | - if ( isset( $_REQUEST['elementor-preview'] ) || ( is_admin() && isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'elementor' ) || ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'elementor_ajax' ) ) { |
|
2245 | - $result = true; |
|
2246 | - } |
|
2247 | - |
|
2248 | - return $result; |
|
2249 | - } |
|
2250 | - |
|
2251 | - /** |
|
2252 | - * Tests if the current output is inside a Divi preview. |
|
2253 | - * |
|
2254 | - * @since 1.0.6 |
|
2255 | - * @return bool |
|
2256 | - */ |
|
2257 | - public function is_divi_preview() { |
|
2258 | - $result = false; |
|
2259 | - if ( isset( $_REQUEST['et_fb'] ) || isset( $_REQUEST['et_pb_preview'] ) || ( is_admin() && isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'elementor' ) ) { |
|
2260 | - $result = true; |
|
2261 | - } |
|
2262 | - |
|
2263 | - return $result; |
|
2264 | - } |
|
2265 | - |
|
2266 | - /** |
|
2267 | - * Tests if the current output is inside a Beaver builder preview. |
|
2268 | - * |
|
2269 | - * @since 1.0.6 |
|
2270 | - * @return bool |
|
2271 | - */ |
|
2272 | - public function is_beaver_preview() { |
|
2273 | - $result = false; |
|
2274 | - if ( isset( $_REQUEST['fl_builder'] ) ) { |
|
2275 | - $result = true; |
|
2276 | - } |
|
2277 | - |
|
2278 | - return $result; |
|
2279 | - } |
|
2280 | - |
|
2281 | - /** |
|
2282 | - * Tests if the current output is inside a siteorigin builder preview. |
|
2283 | - * |
|
2284 | - * @since 1.0.6 |
|
2285 | - * @return bool |
|
2286 | - */ |
|
2287 | - public function is_siteorigin_preview() { |
|
2288 | - $result = false; |
|
2289 | - if ( ! empty( $_REQUEST['siteorigin_panels_live_editor'] ) ) { |
|
2290 | - $result = true; |
|
2291 | - } |
|
2292 | - |
|
2293 | - return $result; |
|
2294 | - } |
|
2295 | - |
|
2296 | - /** |
|
2297 | - * Tests if the current output is inside a cornerstone builder preview. |
|
2298 | - * |
|
2299 | - * @since 1.0.8 |
|
2300 | - * @return bool |
|
2301 | - */ |
|
2302 | - public function is_cornerstone_preview() { |
|
2303 | - $result = false; |
|
2304 | - if ( ! empty( $_REQUEST['cornerstone_preview'] ) || basename( $_SERVER['REQUEST_URI'] ) == 'cornerstone-endpoint' ) { |
|
2305 | - $result = true; |
|
2306 | - } |
|
2307 | - |
|
2308 | - return $result; |
|
2309 | - } |
|
2310 | - |
|
2311 | - /** |
|
2312 | - * Tests if the current output is inside a fusion builder preview. |
|
2313 | - * |
|
2314 | - * @since 1.1.0 |
|
2315 | - * @return bool |
|
2316 | - */ |
|
2317 | - public function is_fusion_preview() { |
|
2318 | - $result = false; |
|
2319 | - if ( ! empty( $_REQUEST['fb-edit'] ) || ! empty( $_REQUEST['fusion_load_nonce'] ) ) { |
|
2320 | - $result = true; |
|
2321 | - } |
|
2322 | - |
|
2323 | - return $result; |
|
2324 | - } |
|
2325 | - |
|
2326 | - /** |
|
2327 | - * General function to check if we are in a preview situation. |
|
2328 | - * |
|
2329 | - * @since 1.0.6 |
|
2330 | - * @return bool |
|
2331 | - */ |
|
2332 | - public function is_preview() { |
|
2333 | - $preview = false; |
|
2334 | - if ( $this->is_divi_preview() ) { |
|
2335 | - $preview = true; |
|
2336 | - } elseif ( $this->is_elementor_preview() ) { |
|
2337 | - $preview = true; |
|
2338 | - } elseif ( $this->is_beaver_preview() ) { |
|
2339 | - $preview = true; |
|
2340 | - } elseif ( $this->is_siteorigin_preview() ) { |
|
2341 | - $preview = true; |
|
2342 | - } elseif ( $this->is_cornerstone_preview() ) { |
|
2343 | - $preview = true; |
|
2344 | - } elseif ( $this->is_fusion_preview() ) { |
|
2345 | - $preview = true; |
|
2346 | - } |
|
2347 | - |
|
2348 | - return $preview; |
|
2349 | - } |
|
2350 | - |
|
2351 | - /** |
|
2352 | - * Output the super title. |
|
2353 | - * |
|
2354 | - * @param $args |
|
2355 | - * @param array $instance |
|
2356 | - * |
|
2357 | - * @return string |
|
2358 | - */ |
|
2359 | - public function output_title( $args, $instance = array() ) { |
|
2360 | - $output = ''; |
|
2361 | - if ( ! empty( $instance['title'] ) ) { |
|
2362 | - /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ |
|
2363 | - $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ); |
|
2364 | - $output = $args['before_title'] . $title . $args['after_title']; |
|
2365 | - } |
|
2366 | - |
|
2367 | - return $output; |
|
2368 | - } |
|
2369 | - |
|
2370 | - /** |
|
2371 | - * Outputs the options form inputs for the widget. |
|
2372 | - * |
|
2373 | - * @param array $instance The widget options. |
|
2374 | - */ |
|
2375 | - public function form( $instance ) { |
|
2376 | - |
|
2377 | - // set widget instance |
|
2378 | - $this->instance = $instance; |
|
2379 | - |
|
2380 | - // set it as a SD widget |
|
2381 | - echo $this->widget_advanced_toggle(); |
|
2382 | - |
|
2383 | - echo "<p>" . esc_attr( $this->options['widget_ops']['description'] ) . "</p>"; |
|
2384 | - $arguments_raw = $this->get_arguments(); |
|
2385 | - |
|
2386 | - if ( is_array( $arguments_raw ) ) { |
|
2387 | - |
|
2388 | - $arguments = $this->group_arguments($arguments_raw); |
|
2389 | - |
|
2390 | - // Do we have sections? |
|
2391 | - $has_sections = $arguments == $arguments_raw ? false : true; |
|
2392 | - |
|
2393 | - |
|
2394 | - if($has_sections){ |
|
2395 | - $panel_count = 0; |
|
2396 | - foreach($arguments as $key => $args){ |
|
2397 | - |
|
2398 | - ?> |
|
2028 | + } |
|
2029 | + |
|
2030 | + /** |
|
2031 | + * Convert an array of attributes to block string. |
|
2032 | + * |
|
2033 | + * @todo there is prob a faster way to do this, also we could add some validation here. |
|
2034 | + * |
|
2035 | + * @param $custom_attributes |
|
2036 | + * |
|
2037 | + * @return string |
|
2038 | + */ |
|
2039 | + public function array_to_attributes( $custom_attributes, $html = false ) { |
|
2040 | + $attributes = ''; |
|
2041 | + if ( ! empty( $custom_attributes ) ) { |
|
2042 | + |
|
2043 | + if ( $html ) { |
|
2044 | + foreach ( $custom_attributes as $key => $val ) { |
|
2045 | + $attributes .= " $key='$val' "; |
|
2046 | + } |
|
2047 | + } else { |
|
2048 | + foreach ( $custom_attributes as $key => $val ) { |
|
2049 | + $attributes .= "'$key': '$val',"; |
|
2050 | + } |
|
2051 | + } |
|
2052 | + } |
|
2053 | + |
|
2054 | + return $attributes; |
|
2055 | + } |
|
2056 | + |
|
2057 | + /** |
|
2058 | + * A self looping function to create the output for JS block elements. |
|
2059 | + * |
|
2060 | + * This is what is output in the WP Editor visual view. |
|
2061 | + * |
|
2062 | + * @param $args |
|
2063 | + */ |
|
2064 | + public function block_element( $args ) { |
|
2065 | + |
|
2066 | + |
|
2067 | + if ( ! empty( $args ) ) { |
|
2068 | + foreach ( $args as $element => $new_args ) { |
|
2069 | + |
|
2070 | + if ( is_array( $new_args ) ) { // its an element |
|
2071 | + |
|
2072 | + |
|
2073 | + if ( isset( $new_args['element'] ) ) { |
|
2074 | + |
|
2075 | + if ( isset( $new_args['element_require'] ) ) { |
|
2076 | + echo str_replace( array( |
|
2077 | + "'+", |
|
2078 | + "+'" |
|
2079 | + ), '', $this->block_props_replace( $new_args['element_require'] ) ) . " && "; |
|
2080 | + unset( $new_args['element_require'] ); |
|
2081 | + } |
|
2082 | + |
|
2083 | + echo "\n el( '" . $new_args['element'] . "', {"; |
|
2084 | + |
|
2085 | + // get the attributes |
|
2086 | + foreach ( $new_args as $new_key => $new_value ) { |
|
2087 | + |
|
2088 | + |
|
2089 | + if ( $new_key == 'element' || $new_key == 'content' || $new_key == 'element_require' || $new_key == 'element_repeat' || is_array( $new_value ) ) { |
|
2090 | + // do nothing |
|
2091 | + } else { |
|
2092 | + echo $this->block_element( array( $new_key => $new_value ) ); |
|
2093 | + } |
|
2094 | + } |
|
2095 | + |
|
2096 | + echo "},";// end attributes |
|
2097 | + |
|
2098 | + // get the content |
|
2099 | + $first_item = 0; |
|
2100 | + foreach ( $new_args as $new_key => $new_value ) { |
|
2101 | + if ( $new_key === 'content' || is_array( $new_value ) ) { |
|
2102 | + |
|
2103 | + if ( $new_key === 'content' ) { |
|
2104 | + echo "'" . $this->block_props_replace( wp_slash( $new_value ) ) . "'"; |
|
2105 | + } |
|
2106 | + |
|
2107 | + if ( is_array( $new_value ) ) { |
|
2108 | + |
|
2109 | + if ( isset( $new_value['element_require'] ) ) { |
|
2110 | + echo str_replace( array( |
|
2111 | + "'+", |
|
2112 | + "+'" |
|
2113 | + ), '', $this->block_props_replace( $new_value['element_require'] ) ) . " && "; |
|
2114 | + unset( $new_value['element_require'] ); |
|
2115 | + } |
|
2116 | + |
|
2117 | + if ( isset( $new_value['element_repeat'] ) ) { |
|
2118 | + $x = 1; |
|
2119 | + while ( $x <= absint( $new_value['element_repeat'] ) ) { |
|
2120 | + $this->block_element( array( '' => $new_value ) ); |
|
2121 | + $x ++; |
|
2122 | + } |
|
2123 | + } else { |
|
2124 | + $this->block_element( array( '' => $new_value ) ); |
|
2125 | + } |
|
2126 | + } |
|
2127 | + $first_item ++; |
|
2128 | + } |
|
2129 | + } |
|
2130 | + |
|
2131 | + echo ")";// end content |
|
2132 | + |
|
2133 | + echo ", \n"; |
|
2134 | + |
|
2135 | + } |
|
2136 | + } else { |
|
2137 | + |
|
2138 | + if ( substr( $element, 0, 3 ) === "if_" ) { |
|
2139 | + echo str_replace( "if_", "", $element ) . ": " . $this->block_props_replace( $new_args, true ) . ","; |
|
2140 | + } elseif ( $element == 'style' ) { |
|
2141 | + echo $element . ": " . $this->block_props_replace( $new_args ) . ","; |
|
2142 | + } else { |
|
2143 | + echo $element . ": '" . $this->block_props_replace( $new_args ) . "',"; |
|
2144 | + } |
|
2145 | + |
|
2146 | + } |
|
2147 | + } |
|
2148 | + } |
|
2149 | + } |
|
2150 | + |
|
2151 | + /** |
|
2152 | + * Replace block attributes placeholders with the proper naming. |
|
2153 | + * |
|
2154 | + * @param $string |
|
2155 | + * |
|
2156 | + * @return mixed |
|
2157 | + */ |
|
2158 | + public function block_props_replace( $string, $no_wrap = false ) { |
|
2159 | + |
|
2160 | + if ( $no_wrap ) { |
|
2161 | + $string = str_replace( array( "[%", "%]" ), array( "props.attributes.", "" ), $string ); |
|
2162 | + } else { |
|
2163 | + $string = str_replace( array( "[%", "%]" ), array( "'+props.attributes.", "+'" ), $string ); |
|
2164 | + } |
|
2165 | + |
|
2166 | + return $string; |
|
2167 | + } |
|
2168 | + |
|
2169 | + /** |
|
2170 | + * Outputs the content of the widget |
|
2171 | + * |
|
2172 | + * @param array $args |
|
2173 | + * @param array $instance |
|
2174 | + */ |
|
2175 | + public function widget( $args, $instance ) { |
|
2176 | + |
|
2177 | + // get the filtered values |
|
2178 | + $argument_values = $this->argument_values( $instance ); |
|
2179 | + $argument_values = $this->string_to_bool( $argument_values ); |
|
2180 | + $output = $this->output( $argument_values, $args ); |
|
2181 | + |
|
2182 | + $no_wrap = false; |
|
2183 | + if( isset( $argument_values['no_wrap'] ) && $argument_values['no_wrap'] ){ $no_wrap = true; } |
|
2184 | + |
|
2185 | + ob_start(); |
|
2186 | + if ( $output && !$no_wrap) { |
|
2187 | + // Before widget |
|
2188 | + $before_widget = $args['before_widget']; |
|
2189 | + $before_widget = apply_filters( 'wp_super_duper_before_widget', $before_widget, $args, $instance, $this ); |
|
2190 | + $before_widget = apply_filters( 'wp_super_duper_before_widget_' . $this->base_id, $before_widget, $args, $instance, $this ); |
|
2191 | + |
|
2192 | + // After widget |
|
2193 | + $after_widget = $args['after_widget']; |
|
2194 | + $after_widget = apply_filters( 'wp_super_duper_after_widget', $after_widget, $args, $instance, $this ); |
|
2195 | + $after_widget = apply_filters( 'wp_super_duper_after_widget_' . $this->base_id, $after_widget, $args, $instance, $this ); |
|
2196 | + |
|
2197 | + echo $before_widget; |
|
2198 | + // elementor strips the widget wrapping div so we check for and add it back if needed |
|
2199 | + if ( $this->is_elementor_widget_output() ) { |
|
2200 | + echo ! empty( $this->options['widget_ops']['classname'] ) ? "<span class='" . esc_attr( $this->options['widget_ops']['classname'] ) . "'>" : ''; |
|
2201 | + } |
|
2202 | + echo $this->output_title( $args, $instance ); |
|
2203 | + echo $output; |
|
2204 | + if ( $this->is_elementor_widget_output() ) { |
|
2205 | + echo ! empty( $this->options['widget_ops']['classname'] ) ? "</span>" : ''; |
|
2206 | + } |
|
2207 | + echo $after_widget; |
|
2208 | + } elseif ( $this->is_preview() && $output == '' ) {// if preview show a placeholder if empty |
|
2209 | + $output = $this->preview_placeholder_text( "{{" . $this->base_id . "}}" ); |
|
2210 | + echo $output; |
|
2211 | + } elseif($output && $no_wrap){ |
|
2212 | + echo $output; |
|
2213 | + } |
|
2214 | + $output = ob_get_clean(); |
|
2215 | + |
|
2216 | + $output = apply_filters( 'wp_super_duper_widget_output', $output, $instance, $args, $this ); |
|
2217 | + |
|
2218 | + echo $output; |
|
2219 | + } |
|
2220 | + |
|
2221 | + /** |
|
2222 | + * Tests if the current output is inside a elementor container. |
|
2223 | + * |
|
2224 | + * @since 1.0.4 |
|
2225 | + * @return bool |
|
2226 | + */ |
|
2227 | + public function is_elementor_widget_output() { |
|
2228 | + $result = false; |
|
2229 | + if ( defined( 'ELEMENTOR_VERSION' ) && isset( $this->number ) && $this->number == 'REPLACE_TO_ID' ) { |
|
2230 | + $result = true; |
|
2231 | + } |
|
2232 | + |
|
2233 | + return $result; |
|
2234 | + } |
|
2235 | + |
|
2236 | + /** |
|
2237 | + * Tests if the current output is inside a elementor preview. |
|
2238 | + * |
|
2239 | + * @since 1.0.4 |
|
2240 | + * @return bool |
|
2241 | + */ |
|
2242 | + public function is_elementor_preview() { |
|
2243 | + $result = false; |
|
2244 | + if ( isset( $_REQUEST['elementor-preview'] ) || ( is_admin() && isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'elementor' ) || ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'elementor_ajax' ) ) { |
|
2245 | + $result = true; |
|
2246 | + } |
|
2247 | + |
|
2248 | + return $result; |
|
2249 | + } |
|
2250 | + |
|
2251 | + /** |
|
2252 | + * Tests if the current output is inside a Divi preview. |
|
2253 | + * |
|
2254 | + * @since 1.0.6 |
|
2255 | + * @return bool |
|
2256 | + */ |
|
2257 | + public function is_divi_preview() { |
|
2258 | + $result = false; |
|
2259 | + if ( isset( $_REQUEST['et_fb'] ) || isset( $_REQUEST['et_pb_preview'] ) || ( is_admin() && isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'elementor' ) ) { |
|
2260 | + $result = true; |
|
2261 | + } |
|
2262 | + |
|
2263 | + return $result; |
|
2264 | + } |
|
2265 | + |
|
2266 | + /** |
|
2267 | + * Tests if the current output is inside a Beaver builder preview. |
|
2268 | + * |
|
2269 | + * @since 1.0.6 |
|
2270 | + * @return bool |
|
2271 | + */ |
|
2272 | + public function is_beaver_preview() { |
|
2273 | + $result = false; |
|
2274 | + if ( isset( $_REQUEST['fl_builder'] ) ) { |
|
2275 | + $result = true; |
|
2276 | + } |
|
2277 | + |
|
2278 | + return $result; |
|
2279 | + } |
|
2280 | + |
|
2281 | + /** |
|
2282 | + * Tests if the current output is inside a siteorigin builder preview. |
|
2283 | + * |
|
2284 | + * @since 1.0.6 |
|
2285 | + * @return bool |
|
2286 | + */ |
|
2287 | + public function is_siteorigin_preview() { |
|
2288 | + $result = false; |
|
2289 | + if ( ! empty( $_REQUEST['siteorigin_panels_live_editor'] ) ) { |
|
2290 | + $result = true; |
|
2291 | + } |
|
2292 | + |
|
2293 | + return $result; |
|
2294 | + } |
|
2295 | + |
|
2296 | + /** |
|
2297 | + * Tests if the current output is inside a cornerstone builder preview. |
|
2298 | + * |
|
2299 | + * @since 1.0.8 |
|
2300 | + * @return bool |
|
2301 | + */ |
|
2302 | + public function is_cornerstone_preview() { |
|
2303 | + $result = false; |
|
2304 | + if ( ! empty( $_REQUEST['cornerstone_preview'] ) || basename( $_SERVER['REQUEST_URI'] ) == 'cornerstone-endpoint' ) { |
|
2305 | + $result = true; |
|
2306 | + } |
|
2307 | + |
|
2308 | + return $result; |
|
2309 | + } |
|
2310 | + |
|
2311 | + /** |
|
2312 | + * Tests if the current output is inside a fusion builder preview. |
|
2313 | + * |
|
2314 | + * @since 1.1.0 |
|
2315 | + * @return bool |
|
2316 | + */ |
|
2317 | + public function is_fusion_preview() { |
|
2318 | + $result = false; |
|
2319 | + if ( ! empty( $_REQUEST['fb-edit'] ) || ! empty( $_REQUEST['fusion_load_nonce'] ) ) { |
|
2320 | + $result = true; |
|
2321 | + } |
|
2322 | + |
|
2323 | + return $result; |
|
2324 | + } |
|
2325 | + |
|
2326 | + /** |
|
2327 | + * General function to check if we are in a preview situation. |
|
2328 | + * |
|
2329 | + * @since 1.0.6 |
|
2330 | + * @return bool |
|
2331 | + */ |
|
2332 | + public function is_preview() { |
|
2333 | + $preview = false; |
|
2334 | + if ( $this->is_divi_preview() ) { |
|
2335 | + $preview = true; |
|
2336 | + } elseif ( $this->is_elementor_preview() ) { |
|
2337 | + $preview = true; |
|
2338 | + } elseif ( $this->is_beaver_preview() ) { |
|
2339 | + $preview = true; |
|
2340 | + } elseif ( $this->is_siteorigin_preview() ) { |
|
2341 | + $preview = true; |
|
2342 | + } elseif ( $this->is_cornerstone_preview() ) { |
|
2343 | + $preview = true; |
|
2344 | + } elseif ( $this->is_fusion_preview() ) { |
|
2345 | + $preview = true; |
|
2346 | + } |
|
2347 | + |
|
2348 | + return $preview; |
|
2349 | + } |
|
2350 | + |
|
2351 | + /** |
|
2352 | + * Output the super title. |
|
2353 | + * |
|
2354 | + * @param $args |
|
2355 | + * @param array $instance |
|
2356 | + * |
|
2357 | + * @return string |
|
2358 | + */ |
|
2359 | + public function output_title( $args, $instance = array() ) { |
|
2360 | + $output = ''; |
|
2361 | + if ( ! empty( $instance['title'] ) ) { |
|
2362 | + /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ |
|
2363 | + $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ); |
|
2364 | + $output = $args['before_title'] . $title . $args['after_title']; |
|
2365 | + } |
|
2366 | + |
|
2367 | + return $output; |
|
2368 | + } |
|
2369 | + |
|
2370 | + /** |
|
2371 | + * Outputs the options form inputs for the widget. |
|
2372 | + * |
|
2373 | + * @param array $instance The widget options. |
|
2374 | + */ |
|
2375 | + public function form( $instance ) { |
|
2376 | + |
|
2377 | + // set widget instance |
|
2378 | + $this->instance = $instance; |
|
2379 | + |
|
2380 | + // set it as a SD widget |
|
2381 | + echo $this->widget_advanced_toggle(); |
|
2382 | + |
|
2383 | + echo "<p>" . esc_attr( $this->options['widget_ops']['description'] ) . "</p>"; |
|
2384 | + $arguments_raw = $this->get_arguments(); |
|
2385 | + |
|
2386 | + if ( is_array( $arguments_raw ) ) { |
|
2387 | + |
|
2388 | + $arguments = $this->group_arguments($arguments_raw); |
|
2389 | + |
|
2390 | + // Do we have sections? |
|
2391 | + $has_sections = $arguments == $arguments_raw ? false : true; |
|
2392 | + |
|
2393 | + |
|
2394 | + if($has_sections){ |
|
2395 | + $panel_count = 0; |
|
2396 | + foreach($arguments as $key => $args){ |
|
2397 | + |
|
2398 | + ?> |
|
2399 | 2399 | <script> |
2400 | 2400 | // jQuery(this).find("i").toggleClass("fas fa-chevron-up fas fa-chevron-down");jQuery(this).next().toggle(); |
2401 | 2401 | </script> |
2402 | 2402 | <?php |
2403 | 2403 | |
2404 | - $hide = $panel_count ? ' style="display:none;" ' : ''; |
|
2405 | - $icon_class = $panel_count ? 'fas fa-chevron-up' : 'fas fa-chevron-down'; |
|
2406 | - echo "<button onclick='jQuery(this).find(\"i\").toggleClass(\"fas fa-chevron-up fas fa-chevron-down\");jQuery(this).next().slideToggle();' type='button' class='sd-toggle-group-button sd-input-group-toggle".sanitize_title_with_dashes($key)."'>".esc_attr($key)." <i style='float:right;' class='".$icon_class."'></i></button>"; |
|
2407 | - echo "<div class='sd-toggle-group sd-input-group-".sanitize_title_with_dashes($key)."' $hide>"; |
|
2408 | - |
|
2409 | - foreach($args as $k => $a){ |
|
2410 | - $this->widget_inputs($a, $instance); |
|
2411 | - } |
|
2412 | - |
|
2413 | - echo "</div>"; |
|
2414 | - |
|
2415 | - $panel_count++; |
|
2416 | - |
|
2417 | - } |
|
2418 | - }else{ |
|
2419 | - foreach ( $arguments as $key => $args ) { |
|
2420 | - $this->widget_inputs( $args, $instance ); |
|
2421 | - } |
|
2422 | - } |
|
2423 | - |
|
2424 | - } |
|
2425 | - } |
|
2426 | - |
|
2427 | - /** |
|
2428 | - * Get the hidden input that when added makes the advanced button show on widget settings. |
|
2429 | - * |
|
2430 | - * @return string |
|
2431 | - */ |
|
2432 | - public function widget_advanced_toggle() { |
|
2433 | - |
|
2434 | - $output = ''; |
|
2435 | - if ( $this->block_show_advanced() ) { |
|
2436 | - $val = 1; |
|
2437 | - } else { |
|
2438 | - $val = 0; |
|
2439 | - } |
|
2440 | - |
|
2441 | - $output .= "<input type='hidden' class='sd-show-advanced' value='$val' />"; |
|
2442 | - |
|
2443 | - return $output; |
|
2444 | - } |
|
2445 | - |
|
2446 | - /** |
|
2447 | - * Convert require element. |
|
2448 | - * |
|
2449 | - * @since 1.0.0 |
|
2450 | - * |
|
2451 | - * @param string $input Input element. |
|
2452 | - * |
|
2453 | - * @return string $output |
|
2454 | - */ |
|
2455 | - public function convert_element_require( $input ) { |
|
2456 | - |
|
2457 | - $input = str_replace( "'", '"', $input );// we only want double quotes |
|
2458 | - |
|
2459 | - $output = esc_attr( str_replace( array( "[%", "%]" ), array( |
|
2460 | - "jQuery(form).find('[data-argument=\"", |
|
2461 | - "\"]').find('input,select,textarea').val()" |
|
2462 | - ), $input ) ); |
|
2463 | - |
|
2464 | - return $output; |
|
2465 | - } |
|
2466 | - |
|
2467 | - /** |
|
2468 | - * Builds the inputs for the widget options. |
|
2469 | - * |
|
2470 | - * @param $args |
|
2471 | - * @param $instance |
|
2472 | - */ |
|
2473 | - public function widget_inputs( $args, $instance ) { |
|
2474 | - |
|
2475 | - $class = ""; |
|
2476 | - $element_require = ""; |
|
2477 | - $custom_attributes = ""; |
|
2478 | - |
|
2479 | - // get value |
|
2480 | - if ( isset( $instance[ $args['name'] ] ) ) { |
|
2481 | - $value = $instance[ $args['name'] ]; |
|
2482 | - } elseif ( ! isset( $instance[ $args['name'] ] ) && ! empty( $args['default'] ) ) { |
|
2483 | - $value = is_array( $args['default'] ) ? array_map( "esc_html", $args['default'] ) : esc_html( $args['default'] ); |
|
2484 | - } else { |
|
2485 | - $value = ''; |
|
2486 | - } |
|
2487 | - |
|
2488 | - // get placeholder |
|
2489 | - if ( ! empty( $args['placeholder'] ) ) { |
|
2490 | - $placeholder = "placeholder='" . esc_html( $args['placeholder'] ) . "'"; |
|
2491 | - } else { |
|
2492 | - $placeholder = ''; |
|
2493 | - } |
|
2494 | - |
|
2495 | - // get if advanced |
|
2496 | - if ( isset( $args['advanced'] ) && $args['advanced'] ) { |
|
2497 | - $class .= " sd-advanced-setting "; |
|
2498 | - } |
|
2499 | - |
|
2500 | - // element_require |
|
2501 | - if ( isset( $args['element_require'] ) && $args['element_require'] ) { |
|
2502 | - $element_require = $args['element_require']; |
|
2503 | - } |
|
2504 | - |
|
2505 | - // custom_attributes |
|
2506 | - if ( isset( $args['custom_attributes'] ) && $args['custom_attributes'] ) { |
|
2507 | - $custom_attributes = $this->array_to_attributes( $args['custom_attributes'], true ); |
|
2508 | - } |
|
2509 | - |
|
2510 | - // before wrapper |
|
2511 | - ?> |
|
2404 | + $hide = $panel_count ? ' style="display:none;" ' : ''; |
|
2405 | + $icon_class = $panel_count ? 'fas fa-chevron-up' : 'fas fa-chevron-down'; |
|
2406 | + echo "<button onclick='jQuery(this).find(\"i\").toggleClass(\"fas fa-chevron-up fas fa-chevron-down\");jQuery(this).next().slideToggle();' type='button' class='sd-toggle-group-button sd-input-group-toggle".sanitize_title_with_dashes($key)."'>".esc_attr($key)." <i style='float:right;' class='".$icon_class."'></i></button>"; |
|
2407 | + echo "<div class='sd-toggle-group sd-input-group-".sanitize_title_with_dashes($key)."' $hide>"; |
|
2408 | + |
|
2409 | + foreach($args as $k => $a){ |
|
2410 | + $this->widget_inputs($a, $instance); |
|
2411 | + } |
|
2412 | + |
|
2413 | + echo "</div>"; |
|
2414 | + |
|
2415 | + $panel_count++; |
|
2416 | + |
|
2417 | + } |
|
2418 | + }else{ |
|
2419 | + foreach ( $arguments as $key => $args ) { |
|
2420 | + $this->widget_inputs( $args, $instance ); |
|
2421 | + } |
|
2422 | + } |
|
2423 | + |
|
2424 | + } |
|
2425 | + } |
|
2426 | + |
|
2427 | + /** |
|
2428 | + * Get the hidden input that when added makes the advanced button show on widget settings. |
|
2429 | + * |
|
2430 | + * @return string |
|
2431 | + */ |
|
2432 | + public function widget_advanced_toggle() { |
|
2433 | + |
|
2434 | + $output = ''; |
|
2435 | + if ( $this->block_show_advanced() ) { |
|
2436 | + $val = 1; |
|
2437 | + } else { |
|
2438 | + $val = 0; |
|
2439 | + } |
|
2440 | + |
|
2441 | + $output .= "<input type='hidden' class='sd-show-advanced' value='$val' />"; |
|
2442 | + |
|
2443 | + return $output; |
|
2444 | + } |
|
2445 | + |
|
2446 | + /** |
|
2447 | + * Convert require element. |
|
2448 | + * |
|
2449 | + * @since 1.0.0 |
|
2450 | + * |
|
2451 | + * @param string $input Input element. |
|
2452 | + * |
|
2453 | + * @return string $output |
|
2454 | + */ |
|
2455 | + public function convert_element_require( $input ) { |
|
2456 | + |
|
2457 | + $input = str_replace( "'", '"', $input );// we only want double quotes |
|
2458 | + |
|
2459 | + $output = esc_attr( str_replace( array( "[%", "%]" ), array( |
|
2460 | + "jQuery(form).find('[data-argument=\"", |
|
2461 | + "\"]').find('input,select,textarea').val()" |
|
2462 | + ), $input ) ); |
|
2463 | + |
|
2464 | + return $output; |
|
2465 | + } |
|
2466 | + |
|
2467 | + /** |
|
2468 | + * Builds the inputs for the widget options. |
|
2469 | + * |
|
2470 | + * @param $args |
|
2471 | + * @param $instance |
|
2472 | + */ |
|
2473 | + public function widget_inputs( $args, $instance ) { |
|
2474 | + |
|
2475 | + $class = ""; |
|
2476 | + $element_require = ""; |
|
2477 | + $custom_attributes = ""; |
|
2478 | + |
|
2479 | + // get value |
|
2480 | + if ( isset( $instance[ $args['name'] ] ) ) { |
|
2481 | + $value = $instance[ $args['name'] ]; |
|
2482 | + } elseif ( ! isset( $instance[ $args['name'] ] ) && ! empty( $args['default'] ) ) { |
|
2483 | + $value = is_array( $args['default'] ) ? array_map( "esc_html", $args['default'] ) : esc_html( $args['default'] ); |
|
2484 | + } else { |
|
2485 | + $value = ''; |
|
2486 | + } |
|
2487 | + |
|
2488 | + // get placeholder |
|
2489 | + if ( ! empty( $args['placeholder'] ) ) { |
|
2490 | + $placeholder = "placeholder='" . esc_html( $args['placeholder'] ) . "'"; |
|
2491 | + } else { |
|
2492 | + $placeholder = ''; |
|
2493 | + } |
|
2494 | + |
|
2495 | + // get if advanced |
|
2496 | + if ( isset( $args['advanced'] ) && $args['advanced'] ) { |
|
2497 | + $class .= " sd-advanced-setting "; |
|
2498 | + } |
|
2499 | + |
|
2500 | + // element_require |
|
2501 | + if ( isset( $args['element_require'] ) && $args['element_require'] ) { |
|
2502 | + $element_require = $args['element_require']; |
|
2503 | + } |
|
2504 | + |
|
2505 | + // custom_attributes |
|
2506 | + if ( isset( $args['custom_attributes'] ) && $args['custom_attributes'] ) { |
|
2507 | + $custom_attributes = $this->array_to_attributes( $args['custom_attributes'], true ); |
|
2508 | + } |
|
2509 | + |
|
2510 | + // before wrapper |
|
2511 | + ?> |
|
2512 | 2512 | <p class="sd-argument <?php echo esc_attr( $class ); ?>" |
2513 | 2513 | data-argument='<?php echo esc_attr( $args['name'] ); ?>' |
2514 | 2514 | data-element_require='<?php if ( $element_require ) { |
2515 | - echo $this->convert_element_require( $element_require ); |
|
2516 | - } ?>' |
|
2515 | + echo $this->convert_element_require( $element_require ); |
|
2516 | + } ?>' |
|
2517 | 2517 | > |
2518 | 2518 | <?php |
2519 | 2519 | |
2520 | - switch ( $args['type'] ) { |
|
2521 | - //array('text','password','number','email','tel','url','color') |
|
2522 | - case "text": |
|
2523 | - case "password": |
|
2524 | - case "number": |
|
2525 | - case "email": |
|
2526 | - case "tel": |
|
2527 | - case "url": |
|
2528 | - case "color": |
|
2529 | - ?> |
|
2520 | + switch ( $args['type'] ) { |
|
2521 | + //array('text','password','number','email','tel','url','color') |
|
2522 | + case "text": |
|
2523 | + case "password": |
|
2524 | + case "number": |
|
2525 | + case "email": |
|
2526 | + case "tel": |
|
2527 | + case "url": |
|
2528 | + case "color": |
|
2529 | + ?> |
|
2530 | 2530 | <label |
2531 | 2531 | for="<?php echo esc_attr( $this->get_field_id( $args['name'] ) ); ?>"><?php echo esc_attr( $args['title'] ); ?><?php echo $this->widget_field_desc( $args ); ?></label> |
2532 | 2532 | <input <?php echo $placeholder; ?> class="widefat" |
@@ -2537,47 +2537,47 @@ discard block |
||
2537 | 2537 | value="<?php echo esc_attr( $value ); ?>"> |
2538 | 2538 | <?php |
2539 | 2539 | |
2540 | - break; |
|
2541 | - case "select": |
|
2542 | - $multiple = isset( $args['multiple'] ) && $args['multiple'] ? true : false; |
|
2543 | - if ( $multiple ) { |
|
2544 | - if ( empty( $value ) ) { |
|
2545 | - $value = array(); |
|
2546 | - } |
|
2547 | - } |
|
2548 | - ?> |
|
2540 | + break; |
|
2541 | + case "select": |
|
2542 | + $multiple = isset( $args['multiple'] ) && $args['multiple'] ? true : false; |
|
2543 | + if ( $multiple ) { |
|
2544 | + if ( empty( $value ) ) { |
|
2545 | + $value = array(); |
|
2546 | + } |
|
2547 | + } |
|
2548 | + ?> |
|
2549 | 2549 | <label |
2550 | 2550 | for="<?php echo esc_attr( $this->get_field_id( $args['name'] ) ); ?>"><?php echo esc_attr( $args['title'] ); ?><?php echo $this->widget_field_desc( $args ); ?></label> |
2551 | 2551 | <select <?php echo $placeholder; ?> class="widefat" |
2552 | 2552 | <?php echo $custom_attributes; ?> |
2553 | 2553 | id="<?php echo esc_attr( $this->get_field_id( $args['name'] ) ); ?>" |
2554 | 2554 | name="<?php echo esc_attr( $this->get_field_name( $args['name'] ) ); |
2555 | - if ( $multiple ) { |
|
2556 | - echo "[]"; |
|
2557 | - } ?>" |
|
2555 | + if ( $multiple ) { |
|
2556 | + echo "[]"; |
|
2557 | + } ?>" |
|
2558 | 2558 | <?php if ( $multiple ) { |
2559 | - echo "multiple"; |
|
2560 | - } //@todo not implemented yet due to gutenberg not supporting it |
|
2561 | - ?> |
|
2559 | + echo "multiple"; |
|
2560 | + } //@todo not implemented yet due to gutenberg not supporting it |
|
2561 | + ?> |
|
2562 | 2562 | > |
2563 | 2563 | <?php |
2564 | 2564 | |
2565 | - if ( ! empty( $args['options'] ) ) { |
|
2566 | - foreach ( $args['options'] as $val => $label ) { |
|
2567 | - if ( $multiple ) { |
|
2568 | - $selected = in_array( $val, $value ) ? 'selected="selected"' : ''; |
|
2569 | - } else { |
|
2570 | - $selected = selected( $value, $val, false ); |
|
2571 | - } |
|
2572 | - echo "<option value='$val' " . $selected . ">$label</option>"; |
|
2573 | - } |
|
2574 | - } |
|
2575 | - ?> |
|
2565 | + if ( ! empty( $args['options'] ) ) { |
|
2566 | + foreach ( $args['options'] as $val => $label ) { |
|
2567 | + if ( $multiple ) { |
|
2568 | + $selected = in_array( $val, $value ) ? 'selected="selected"' : ''; |
|
2569 | + } else { |
|
2570 | + $selected = selected( $value, $val, false ); |
|
2571 | + } |
|
2572 | + echo "<option value='$val' " . $selected . ">$label</option>"; |
|
2573 | + } |
|
2574 | + } |
|
2575 | + ?> |
|
2576 | 2576 | </select> |
2577 | 2577 | <?php |
2578 | - break; |
|
2579 | - case "checkbox": |
|
2580 | - ?> |
|
2578 | + break; |
|
2579 | + case "checkbox": |
|
2580 | + ?> |
|
2581 | 2581 | <input <?php echo $placeholder; ?> |
2582 | 2582 | <?php checked( 1, $value, true ) ?> |
2583 | 2583 | <?php echo $custom_attributes; ?> |
@@ -2587,9 +2587,9 @@ discard block |
||
2587 | 2587 | <label |
2588 | 2588 | for="<?php echo esc_attr( $this->get_field_id( $args['name'] ) ); ?>"><?php echo esc_attr( $args['title'] ); ?><?php echo $this->widget_field_desc( $args ); ?></label> |
2589 | 2589 | <?php |
2590 | - break; |
|
2591 | - case "textarea": |
|
2592 | - ?> |
|
2590 | + break; |
|
2591 | + case "textarea": |
|
2592 | + ?> |
|
2593 | 2593 | <label |
2594 | 2594 | for="<?php echo esc_attr( $this->get_field_id( $args['name'] ) ); ?>"><?php echo esc_attr( $args['title'] ); ?><?php echo $this->widget_field_desc( $args ); ?></label> |
2595 | 2595 | <textarea <?php echo $placeholder; ?> class="widefat" |
@@ -2599,136 +2599,136 @@ discard block |
||
2599 | 2599 | ><?php echo esc_attr( $value ); ?></textarea> |
2600 | 2600 | <?php |
2601 | 2601 | |
2602 | - break; |
|
2603 | - case "hidden": |
|
2604 | - ?> |
|
2602 | + break; |
|
2603 | + case "hidden": |
|
2604 | + ?> |
|
2605 | 2605 | <input id="<?php echo esc_attr( $this->get_field_id( $args['name'] ) ); ?>" |
2606 | 2606 | name="<?php echo esc_attr( $this->get_field_name( $args['name'] ) ); ?>" type="hidden" |
2607 | 2607 | value="<?php echo esc_attr( $value ); ?>"> |
2608 | 2608 | <?php |
2609 | - break; |
|
2610 | - default: |
|
2611 | - echo "No input type found!"; // @todo we need to add more input types. |
|
2612 | - } |
|
2609 | + break; |
|
2610 | + default: |
|
2611 | + echo "No input type found!"; // @todo we need to add more input types. |
|
2612 | + } |
|
2613 | 2613 | |
2614 | - // after wrapper |
|
2615 | - ?> |
|
2614 | + // after wrapper |
|
2615 | + ?> |
|
2616 | 2616 | </p> |
2617 | 2617 | <?php |
2618 | 2618 | |
2619 | - } |
|
2620 | - |
|
2621 | - /** |
|
2622 | - * Get the widget input description html. |
|
2623 | - * |
|
2624 | - * @param $args |
|
2625 | - * |
|
2626 | - * @return string |
|
2627 | - * @todo, need to make its own tooltip script |
|
2628 | - */ |
|
2629 | - public function widget_field_desc( $args ) { |
|
2630 | - |
|
2631 | - $description = ''; |
|
2632 | - if ( isset( $args['desc'] ) && $args['desc'] ) { |
|
2633 | - if ( isset( $args['desc_tip'] ) && $args['desc_tip'] ) { |
|
2634 | - $description = $this->desc_tip( $args['desc'] ); |
|
2635 | - } else { |
|
2636 | - $description = '<span class="description">' . wp_kses_post( $args['desc'] ) . '</span>'; |
|
2637 | - } |
|
2638 | - } |
|
2639 | - |
|
2640 | - return $description; |
|
2641 | - } |
|
2642 | - |
|
2643 | - /** |
|
2644 | - * Get the tool tip html. |
|
2645 | - * |
|
2646 | - * @param $tip |
|
2647 | - * @param bool $allow_html |
|
2648 | - * |
|
2649 | - * @return string |
|
2650 | - */ |
|
2651 | - function desc_tip( $tip, $allow_html = false ) { |
|
2652 | - if ( $allow_html ) { |
|
2653 | - $tip = $this->sanitize_tooltip( $tip ); |
|
2654 | - } else { |
|
2655 | - $tip = esc_attr( $tip ); |
|
2656 | - } |
|
2657 | - |
|
2658 | - return '<span class="gd-help-tip dashicons dashicons-editor-help" title="' . $tip . '"></span>'; |
|
2659 | - } |
|
2660 | - |
|
2661 | - /** |
|
2662 | - * Sanitize a string destined to be a tooltip. |
|
2663 | - * |
|
2664 | - * @param string $var |
|
2665 | - * |
|
2666 | - * @return string |
|
2667 | - */ |
|
2668 | - public function sanitize_tooltip( $var ) { |
|
2669 | - return htmlspecialchars( wp_kses( html_entity_decode( $var ), array( |
|
2670 | - 'br' => array(), |
|
2671 | - 'em' => array(), |
|
2672 | - 'strong' => array(), |
|
2673 | - 'small' => array(), |
|
2674 | - 'span' => array(), |
|
2675 | - 'ul' => array(), |
|
2676 | - 'li' => array(), |
|
2677 | - 'ol' => array(), |
|
2678 | - 'p' => array(), |
|
2679 | - ) ) ); |
|
2680 | - } |
|
2681 | - |
|
2682 | - /** |
|
2683 | - * Processing widget options on save |
|
2684 | - * |
|
2685 | - * @param array $new_instance The new options |
|
2686 | - * @param array $old_instance The previous options |
|
2687 | - * |
|
2688 | - * @return array |
|
2689 | - * @todo we should add some sanitation here. |
|
2690 | - */ |
|
2691 | - public function update( $new_instance, $old_instance ) { |
|
2692 | - |
|
2693 | - //save the widget |
|
2694 | - $instance = array_merge( (array) $old_instance, (array) $new_instance ); |
|
2695 | - |
|
2696 | - // set widget instance |
|
2697 | - $this->instance = $instance; |
|
2698 | - |
|
2699 | - if ( empty( $this->arguments ) ) { |
|
2700 | - $this->get_arguments(); |
|
2701 | - } |
|
2702 | - |
|
2703 | - // check for checkboxes |
|
2704 | - if ( ! empty( $this->arguments ) ) { |
|
2705 | - foreach ( $this->arguments as $argument ) { |
|
2706 | - if ( isset( $argument['type'] ) && $argument['type'] == 'checkbox' && ! isset( $new_instance[ $argument['name'] ] ) ) { |
|
2707 | - $instance[ $argument['name'] ] = '0'; |
|
2708 | - } |
|
2709 | - } |
|
2710 | - } |
|
2711 | - |
|
2712 | - return $instance; |
|
2713 | - } |
|
2714 | - |
|
2715 | - /** |
|
2716 | - * Checks if the current call is a ajax call to get the block content. |
|
2717 | - * |
|
2718 | - * This can be used in your widget to return different content as the block content. |
|
2719 | - * |
|
2720 | - * @since 1.0.3 |
|
2721 | - * @return bool |
|
2722 | - */ |
|
2723 | - public function is_block_content_call() { |
|
2724 | - $result = false; |
|
2725 | - if ( wp_doing_ajax() && isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'super_duper_output_shortcode' ) { |
|
2726 | - $result = true; |
|
2727 | - } |
|
2728 | - |
|
2729 | - return $result; |
|
2730 | - } |
|
2731 | - |
|
2732 | - } |
|
2619 | + } |
|
2620 | + |
|
2621 | + /** |
|
2622 | + * Get the widget input description html. |
|
2623 | + * |
|
2624 | + * @param $args |
|
2625 | + * |
|
2626 | + * @return string |
|
2627 | + * @todo, need to make its own tooltip script |
|
2628 | + */ |
|
2629 | + public function widget_field_desc( $args ) { |
|
2630 | + |
|
2631 | + $description = ''; |
|
2632 | + if ( isset( $args['desc'] ) && $args['desc'] ) { |
|
2633 | + if ( isset( $args['desc_tip'] ) && $args['desc_tip'] ) { |
|
2634 | + $description = $this->desc_tip( $args['desc'] ); |
|
2635 | + } else { |
|
2636 | + $description = '<span class="description">' . wp_kses_post( $args['desc'] ) . '</span>'; |
|
2637 | + } |
|
2638 | + } |
|
2639 | + |
|
2640 | + return $description; |
|
2641 | + } |
|
2642 | + |
|
2643 | + /** |
|
2644 | + * Get the tool tip html. |
|
2645 | + * |
|
2646 | + * @param $tip |
|
2647 | + * @param bool $allow_html |
|
2648 | + * |
|
2649 | + * @return string |
|
2650 | + */ |
|
2651 | + function desc_tip( $tip, $allow_html = false ) { |
|
2652 | + if ( $allow_html ) { |
|
2653 | + $tip = $this->sanitize_tooltip( $tip ); |
|
2654 | + } else { |
|
2655 | + $tip = esc_attr( $tip ); |
|
2656 | + } |
|
2657 | + |
|
2658 | + return '<span class="gd-help-tip dashicons dashicons-editor-help" title="' . $tip . '"></span>'; |
|
2659 | + } |
|
2660 | + |
|
2661 | + /** |
|
2662 | + * Sanitize a string destined to be a tooltip. |
|
2663 | + * |
|
2664 | + * @param string $var |
|
2665 | + * |
|
2666 | + * @return string |
|
2667 | + */ |
|
2668 | + public function sanitize_tooltip( $var ) { |
|
2669 | + return htmlspecialchars( wp_kses( html_entity_decode( $var ), array( |
|
2670 | + 'br' => array(), |
|
2671 | + 'em' => array(), |
|
2672 | + 'strong' => array(), |
|
2673 | + 'small' => array(), |
|
2674 | + 'span' => array(), |
|
2675 | + 'ul' => array(), |
|
2676 | + 'li' => array(), |
|
2677 | + 'ol' => array(), |
|
2678 | + 'p' => array(), |
|
2679 | + ) ) ); |
|
2680 | + } |
|
2681 | + |
|
2682 | + /** |
|
2683 | + * Processing widget options on save |
|
2684 | + * |
|
2685 | + * @param array $new_instance The new options |
|
2686 | + * @param array $old_instance The previous options |
|
2687 | + * |
|
2688 | + * @return array |
|
2689 | + * @todo we should add some sanitation here. |
|
2690 | + */ |
|
2691 | + public function update( $new_instance, $old_instance ) { |
|
2692 | + |
|
2693 | + //save the widget |
|
2694 | + $instance = array_merge( (array) $old_instance, (array) $new_instance ); |
|
2695 | + |
|
2696 | + // set widget instance |
|
2697 | + $this->instance = $instance; |
|
2698 | + |
|
2699 | + if ( empty( $this->arguments ) ) { |
|
2700 | + $this->get_arguments(); |
|
2701 | + } |
|
2702 | + |
|
2703 | + // check for checkboxes |
|
2704 | + if ( ! empty( $this->arguments ) ) { |
|
2705 | + foreach ( $this->arguments as $argument ) { |
|
2706 | + if ( isset( $argument['type'] ) && $argument['type'] == 'checkbox' && ! isset( $new_instance[ $argument['name'] ] ) ) { |
|
2707 | + $instance[ $argument['name'] ] = '0'; |
|
2708 | + } |
|
2709 | + } |
|
2710 | + } |
|
2711 | + |
|
2712 | + return $instance; |
|
2713 | + } |
|
2714 | + |
|
2715 | + /** |
|
2716 | + * Checks if the current call is a ajax call to get the block content. |
|
2717 | + * |
|
2718 | + * This can be used in your widget to return different content as the block content. |
|
2719 | + * |
|
2720 | + * @since 1.0.3 |
|
2721 | + * @return bool |
|
2722 | + */ |
|
2723 | + public function is_block_content_call() { |
|
2724 | + $result = false; |
|
2725 | + if ( wp_doing_ajax() && isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'super_duper_output_shortcode' ) { |
|
2726 | + $result = true; |
|
2727 | + } |
|
2728 | + |
|
2729 | + return $result; |
|
2730 | + } |
|
2731 | + |
|
2732 | + } |
|
2733 | 2733 | |
2734 | 2734 | } |
2735 | 2735 | \ No newline at end of file |