| @@ -1,14 +1,13 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace UltimateStoreKit\Includes\Builder; |
| 4 | 4 | |
| 5 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 5 | +if (! defined('ABSPATH')) { | |
| 6 | 6 | exit; |
| 7 | 7 | } // Exit if accessed directly |
| 8 | 8 | |
| 9 | -use UltimateStoreKit\Includes\Builder\Builder_Template_Helper; | |
| 10 | -use \UltimateStoreKit\Base\Singleton; | |
| 9 | +use UltimateStoreKit\Base\Singleton; | |
| 11 | 10 | |
| 12 | 11 | class Meta { |
| 13 | 12 | |
| 14 | 13 | use Singleton; |
| @@ -18,8 +17,83 @@ | ||
| 18 | 17 | const EDIT_WITH = '_ultimate_store_kit_edit_with'; |
| 19 | 18 | |
| 20 | 19 | const TEMPLATE_TYPE = '_ultimate_store_kit_template_type'; |
| 21 | 20 | |
| 22 | - const TEMPLATE_ID = '_usk_template_'; | |
| 21 | + /** | |
| 22 | + * Prefix for the wp_options keys that record which builder template is | |
| 23 | + * assigned to each template type. | |
| 24 | + * | |
| 25 | + * These are options, not post meta, so the leading-underscore "protected" | |
| 26 | + * convention does not apply and a reserved "_" prefix is not permitted. | |
| 27 | + */ | |
| 28 | + const TEMPLATE_ID = 'ultimate_store_kit_template_'; | |
| 23 | 29 | |
| 24 | - const SAMPLE_POST_ID = 'sample_post_id'; | |
| 30 | + /** | |
| 31 | + * The pre-3.1.2 prefix. Still read so existing sites keep their assigned | |
| 32 | + * templates; see Meta::get_template_option(). | |
| 33 | + */ | |
| 34 | + const TEMPLATE_ID_LEGACY = '_usk_template_'; | |
| 35 | + | |
| 36 | + /** | |
| 37 | + * Read a template-assignment option, migrating it forward from the legacy | |
| 38 | + * "_usk_template_" key the first time it is seen. | |
| 39 | + * | |
| 40 | + * Historically some call sites lowercased the key and others did not, so a | |
| 41 | + * legacy site can hold two rows for the same template in different cases. | |
| 42 | + * Both spellings are tried before giving up. | |
| 43 | + * | |
| 44 | + * @param string $suffix Template type, e.g. "product|shop". | |
| 45 | + * @return mixed Option value, or false when unset. | |
| 46 | + */ | |
| 47 | + public static function get_template_option($suffix) { | |
| 48 | + $key = strtolower(self::TEMPLATE_ID . $suffix); | |
| 49 | + $value = get_option($key, null); | |
| 50 | + | |
| 51 | + if (null !== $value) { | |
| 52 | + return $value; | |
| 53 | + } | |
| 54 | + | |
| 55 | + foreach ([self::TEMPLATE_ID_LEGACY . $suffix, strtolower(self::TEMPLATE_ID_LEGACY . $suffix)] as $legacy_key) { | |
| 56 | + $legacy = get_option($legacy_key, null); | |
| 57 | + | |
| 58 | + if (null !== $legacy) { | |
| 59 | + update_option($key, $legacy); | |
| 60 | + delete_option($legacy_key); | |
| 61 | + return $legacy; | |
| 62 | + } | |
| 63 | + } | |
| 64 | + | |
| 65 | + return false; | |
| 66 | + } | |
| 67 | + | |
| 68 | + /** | |
| 69 | + * Write a template-assignment option, clearing any legacy row. | |
| 70 | + * | |
| 71 | + * @param string $suffix Template type. | |
| 72 | + * @param mixed $value Template post ID. | |
| 73 | + * @return void | |
| 74 | + */ | |
| 75 | + public static function update_template_option($suffix, $value) { | |
| 76 | + update_option(strtolower(self::TEMPLATE_ID . $suffix), $value); | |
| 77 | + self::delete_legacy_template_option($suffix); | |
| 78 | + } | |
| 79 | + | |
| 80 | + /** | |
| 81 | + * Delete a template-assignment option in both the current and legacy keys. | |
| 82 | + * | |
| 83 | + * @param string $suffix Template type. | |
| 84 | + * @return void | |
| 85 | + */ | |
| 86 | + public static function delete_template_option($suffix) { | |
| 87 | + delete_option(strtolower(self::TEMPLATE_ID . $suffix)); | |
| 88 | + self::delete_legacy_template_option($suffix); | |
| 89 | + } | |
| 90 | + | |
| 91 | + /** | |
| 92 | + * @param string $suffix Template type. | |
| 93 | + * @return void | |
| 94 | + */ | |
| 95 | + private static function delete_legacy_template_option($suffix) { | |
| 96 | + delete_option(self::TEMPLATE_ID_LEGACY . $suffix); | |
| 97 | + delete_option(strtolower(self::TEMPLATE_ID_LEGACY . $suffix)); | |
| 98 | + } | |
| 25 | 99 | } |