Form.php
185 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Handle Embed Donation Form Route |
| 5 | * |
| 6 | * @package Give |
| 7 | * @since 2.7.0 |
| 8 | */ |
| 9 | |
| 10 | namespace Give\Route; |
| 11 | |
| 12 | use Give\Controller\Form as Controller; |
| 13 | use WP_Post; |
| 14 | |
| 15 | defined( 'ABSPATH' ) || exit; |
| 16 | |
| 17 | /** |
| 18 | * Template class. |
| 19 | * |
| 20 | * @since 2.7.0 |
| 21 | */ |
| 22 | class Form { |
| 23 | /** |
| 24 | * Option name |
| 25 | * |
| 26 | * @since 2.7.0 |
| 27 | * @var string |
| 28 | */ |
| 29 | private $optionName = 'form_page_url_prefix'; |
| 30 | |
| 31 | /** |
| 32 | * Route base |
| 33 | * |
| 34 | * @since 2.7.0 |
| 35 | * @var string |
| 36 | */ |
| 37 | private $defaultBase = 'give'; |
| 38 | |
| 39 | /** |
| 40 | * Route base |
| 41 | * |
| 42 | * @since 2.7.0 |
| 43 | * @var string |
| 44 | */ |
| 45 | private $base; |
| 46 | |
| 47 | /** |
| 48 | * Form constructor. |
| 49 | * |
| 50 | * @param Controller $controller |
| 51 | */ |
| 52 | public function init( $controller ) { |
| 53 | $this->setBasePrefix(); |
| 54 | $controller->init(); |
| 55 | |
| 56 | add_action( 'init', [ $this, 'addRule' ] ); |
| 57 | add_action( 'query_vars', [ $this, 'addQueryVar' ] ); |
| 58 | add_action( 'give-settings_save_advanced', [ $this, 'updateRule' ], 11 ); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Setup base prefix |
| 63 | * |
| 64 | * @since 2.7.0 |
| 65 | */ |
| 66 | public function setBasePrefix() { |
| 67 | $this->base = give_get_option( $this->optionName, $this->defaultBase ); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | /** |
| 72 | * Add rewrite rule |
| 73 | * |
| 74 | * @since 2.7.0 |
| 75 | */ |
| 76 | public function addRule() { |
| 77 | add_rewrite_rule( |
| 78 | "{$this->base}/(.+?)/?$", |
| 79 | sprintf( |
| 80 | 'index.php?url_prefix=%1$s&give_form_id=$matches[1]', |
| 81 | $this->base |
| 82 | ), |
| 83 | 'top' |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | |
| 88 | /** |
| 89 | * Add query var |
| 90 | * |
| 91 | * @since 2.7.0 |
| 92 | * @param array $queryVars |
| 93 | * |
| 94 | * @return array |
| 95 | */ |
| 96 | public function addQueryVar( $queryVars ) { |
| 97 | $queryVars[] = 'give_form_id'; |
| 98 | $queryVars[] = 'url_prefix'; |
| 99 | |
| 100 | return $queryVars; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Get form URL. |
| 105 | * |
| 106 | * @since 2.7.0 |
| 107 | * @param int $form_id |
| 108 | * |
| 109 | * @return string |
| 110 | */ |
| 111 | public function getURL( $form_id ) { |
| 112 | return home_url( "/{$this->base}/{$form_id}" ); |
| 113 | } |
| 114 | |
| 115 | |
| 116 | /** |
| 117 | * Get url base. |
| 118 | * |
| 119 | * @since 2.7.0 |
| 120 | * @return string |
| 121 | */ |
| 122 | public function getBase() { |
| 123 | return $this->base; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Get url base. |
| 128 | * |
| 129 | * @since 2.7.0 |
| 130 | * @return string |
| 131 | */ |
| 132 | public function getOptionName() { |
| 133 | return $this->optionName; |
| 134 | } |
| 135 | |
| 136 | |
| 137 | /** |
| 138 | * Update route rule |
| 139 | * |
| 140 | * @since 2.7.0 |
| 141 | */ |
| 142 | public function updateRule() { |
| 143 | global $wp_rewrite; |
| 144 | |
| 145 | $updateBase = give_get_option( $this->optionName, $this->defaultBase ); |
| 146 | |
| 147 | if ( $updateBase !== $this->base ) { |
| 148 | $this->base = $updateBase; |
| 149 | |
| 150 | // Save rewrite rule manually. |
| 151 | $this->addRule(); |
| 152 | flush_rewrite_rules(); |
| 153 | $wp_rewrite->wp_rewrite_rules(); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Get queried form ID. |
| 159 | * |
| 160 | * @since 2.7.0 |
| 161 | * @return int |
| 162 | */ |
| 163 | public function getQueriedFormID() { |
| 164 | $formId = 0; |
| 165 | |
| 166 | if ( $queryVar = get_query_var( 'give_form_id' ) ) { |
| 167 | $form = current( |
| 168 | get_posts( |
| 169 | [ |
| 170 | 'name' => $queryVar, |
| 171 | 'numberposts' => 1, |
| 172 | 'post_type' => 'give_forms', |
| 173 | ] |
| 174 | ) |
| 175 | ); |
| 176 | |
| 177 | if ( $form instanceof WP_Post ) { |
| 178 | $formId = $form->ID; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | return $formId; |
| 183 | } |
| 184 | } |
| 185 |