| 1 |
<?php |
| 2 |
/** |
| 3 |
* Admin help: Documentation context |
| 4 |
* |
| 5 |
* Helps return relevant documentation for the current context. |
| 6 |
* |
| 7 |
* @package SimplePay |
| 8 |
* @subpackage Core |
| 9 |
* @copyright Copyright (c) 2022, Sandhills Development, LLC |
| 10 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 11 |
* @since 4.4.6 |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace SimplePay\Core\Help; |
| 15 |
|
| 16 |
use SimplePay\Core\License\LicenseAwareInterface; |
| 17 |
use SimplePay\Core\License\LicenseAwareTrait; |
| 18 |
use SimplePay\Core\Settings; |
| 19 |
|
| 20 |
/** |
| 21 |
* HelpDocsContext class. |
| 22 |
* |
| 23 |
* @since 4.4.6 |
| 24 |
*/ |
| 25 |
class HelpDocsContext implements LicenseAwareInterface { |
| 26 |
|
| 27 |
use LicenseAwareTrait; |
| 28 |
|
| 29 |
/** |
| 30 |
* Returns all documentation articles. |
| 31 |
* |
| 32 |
* @since 4.4.6 |
| 33 |
* |
| 34 |
* @return array<array<string, string>> |
| 35 |
*/ |
| 36 |
public function get_all_docs() { |
| 37 |
$cache_dir = $this->get_cache_dir(); |
| 38 |
$cache_file = $cache_dir . 'wpsp-docs.json'; |
| 39 |
|
| 40 |
if ( ! file_exists( $cache_file ) ) { |
| 41 |
return array(); |
| 42 |
} |
| 43 |
|
| 44 |
$cache_file_contents = file_get_contents( $cache_file ); |
| 45 |
|
| 46 |
if ( false === $cache_file_contents ) { |
| 47 |
return array(); |
| 48 |
} |
| 49 |
|
| 50 |
$docs = json_decode( $cache_file_contents, true ); |
| 51 |
|
| 52 |
if ( ! is_array( $docs ) ) { |
| 53 |
return array(); |
| 54 |
} |
| 55 |
|
| 56 |
return $docs; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Returns a list of documentation categories. |
| 61 |
* |
| 62 |
* @since 4.4.6 |
| 63 |
* |
| 64 |
* @return array<string, string> |
| 65 |
*/ |
| 66 |
public function get_search_categories() { |
| 67 |
return array( |
| 68 |
'getting-started' => __( 'Getting Started', 'stripe' ), |
| 69 |
'common-problems' => __( 'Common Problems', 'stripe' ), |
| 70 |
'stripe-dashboard' => __( 'Stripe Dashboard', 'stripe' ), |
| 71 |
'integrations' => __( 'Integrations', 'stripe' ), |
| 72 |
'faqs' => __( 'Frequently Asked Questions', 'stripe' ), |
| 73 |
'functionality' => __( 'Functionality', 'stripe' ), |
| 74 |
'payment-methods' => __( 'Payment Methods', 'stripe' ), |
| 75 |
'walkthroughs' => __( 'Walkthroughs', 'stripe' ), |
| 76 |
); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Returns a search term for the given context. |
| 81 |
* |
| 82 |
* @return string |
| 83 |
*/ |
| 84 |
public function get_search_term() { |
| 85 |
$context = $this->get_context(); |
| 86 |
|
| 87 |
$map = array( |
| 88 |
// Coupons. |
| 89 |
'coupons' => 'coupons', |
| 90 |
'coupons/add' => 'add coupons', |
| 91 |
|
| 92 |
// Settings. |
| 93 |
'settings/general/license' => 'activate license', |
| 94 |
'settings/general/currency' => 'currency locale', |
| 95 |
'settings/general/taxes' => 'taxes', |
| 96 |
'settings/general/recaptcha' => 'recaptcha', |
| 97 |
'settings/stripe/account' => 'stripe account', |
| 98 |
'settings/stripe/locale' => 'currency locale', |
| 99 |
'settings/stripe/webhooks' => 'webhooks', |
| 100 |
'settings/payment-confirmations/pages' => 'confirmation', |
| 101 |
'settings/payment-confirmations/one-time' => 'confirmation', |
| 102 |
'settings/payment-confirmations/subscription' => 'confirmation', |
| 103 |
'settings/payment-confirmations/subscription-with-trial' => |
| 104 |
'confirmation', |
| 105 |
'settings/emails/general' => 'email settings', |
| 106 |
'settings/emails/payment-confirmation' => 'email settings', |
| 107 |
'settings/emails/payment-notification' => 'email settings', |
| 108 |
'settings/emails/upcoming-invoice' => 'email settings', |
| 109 |
'settings/emails/invoice-confirmation' => 'email settings', |
| 110 |
'settings/customers/subscription-management' => 'subscription management', |
| 111 |
|
| 112 |
// Form builder. |
| 113 |
'form-builder/add' => 'first form', |
| 114 |
'form-builder/edit' => 'form settings', |
| 115 |
); |
| 116 |
|
| 117 |
return isset( $map[ $context ] ) ? $map[ $context ] : ''; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Returns the current page context. |
| 122 |
* |
| 123 |
* @since 4.4.6 |
| 124 |
* |
| 125 |
* @return string |
| 126 |
*/ |
| 127 |
private function get_context() { |
| 128 |
$screen = null; |
| 129 |
$context = ''; |
| 130 |
|
| 131 |
if ( function_exists( 'get_current_screen' ) ) { |
| 132 |
$screen = get_current_screen(); |
| 133 |
} |
| 134 |
|
| 135 |
if ( null !== $screen ) { |
| 136 |
switch ( $screen->base ) { |
| 137 |
case 'simple-pay_page_simpay_coupons': |
| 138 |
$context = $this->get_coupon_context(); |
| 139 |
break; |
| 140 |
case 'simple-pay_page_simpay_settings': |
| 141 |
$context = $this->get_settings_context(); |
| 142 |
break; |
| 143 |
} |
| 144 |
|
| 145 |
switch ( $screen->id ) { |
| 146 |
case 'edit-simple-pay': |
| 147 |
$context = 'form-builder/list'; |
| 148 |
break; |
| 149 |
case 'simple-pay': |
| 150 |
$context = isset( $_GET['action'] ) && 'edit' === $_GET['action'] |
| 151 |
? 'form-builder/edit' |
| 152 |
: 'form-builder/add'; |
| 153 |
break; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
return $context; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Returns context for coupons. |
| 162 |
* |
| 163 |
* @return string |
| 164 |
*/ |
| 165 |
private function get_coupon_context() { |
| 166 |
$context = 'coupons'; |
| 167 |
|
| 168 |
if ( |
| 169 |
isset( $_GET['simpay-action'] ) && |
| 170 |
'add-coupon' === $_GET['simpay-action'] |
| 171 |
) { |
| 172 |
$context = 'coupons/add'; |
| 173 |
} |
| 174 |
|
| 175 |
return $context; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Returns context for settings. |
| 180 |
* |
| 181 |
* @return string |
| 182 |
*/ |
| 183 |
private function get_settings_context() { |
| 184 |
$context = 'settings'; |
| 185 |
|
| 186 |
// Default tab. |
| 187 |
if ( ! isset( $_GET['tab'] ) ) { |
| 188 |
if ( $this->license->is_lite() ) { |
| 189 |
$context = 'settings/general/currency'; |
| 190 |
} else { |
| 191 |
$context = 'settings/general/license'; |
| 192 |
} |
| 193 |
} else { |
| 194 |
$section = sanitize_text_field( $_GET['tab'] ); |
| 195 |
$subsection = isset( $_GET['subsection'] ) |
| 196 |
? sanitize_text_field( $_GET['subsection'] ) |
| 197 |
: ''; |
| 198 |
|
| 199 |
if ( empty( $subsection ) ) { |
| 200 |
$subsection = Settings\get_main_subsection_id( $section ); |
| 201 |
} |
| 202 |
|
| 203 |
$context = "settings/$section/$subsection"; |
| 204 |
} |
| 205 |
|
| 206 |
return $context; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Returns the path to the cache directory. |
| 211 |
* |
| 212 |
* @since 4.4.6 |
| 213 |
* |
| 214 |
* @return string |
| 215 |
*/ |
| 216 |
private function get_cache_dir() { |
| 217 |
$upload_dir = wp_upload_dir(); |
| 218 |
|
| 219 |
return trailingslashit( $upload_dir['basedir'] ); |
| 220 |
} |
| 221 |
|
| 222 |
} |
| 223 |
|