base.php
3 weeks ago
exporter.php
3 weeks ago
helpers.php
3 weeks ago
resolver.php
3 weeks ago
updater.php
3 weeks ago
view.php
3 weeks ago
helpers.php
211 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Wpae\AddonAPI; |
| 4 | |
| 5 | if ( ! defined( 'ABSPATH' ) ) exit; |
| 6 | |
| 7 | /** |
| 8 | * Renders a view. |
| 9 | * |
| 10 | * @param string $viewPath |
| 11 | * @param array $data |
| 12 | * @param string|null $defaultView |
| 13 | * @param bool $echo |
| 14 | * |
| 15 | * @return string|null |
| 16 | * @throws \Exception |
| 17 | */ |
| 18 | function view( |
| 19 | string $viewPath, |
| 20 | array $data, |
| 21 | $defaultView = null, |
| 22 | $echo = true |
| 23 | ) { |
| 24 | $path = PMXE_ROOT_DIR . '/addon-api' . '/templates/'; |
| 25 | $filePath = $path . $viewPath . '.php'; |
| 26 | $defaultFilePath = $path . $defaultView . '.php'; |
| 27 | |
| 28 | extract( $data ); |
| 29 | |
| 30 | $view = $filePath; |
| 31 | |
| 32 | if ( ! is_file( $view ) ) { |
| 33 | if ( $defaultView ) { |
| 34 | $view = $defaultFilePath; |
| 35 | } else { |
| 36 | throw new \Exception( esc_html( "The requested template file $filePath was not found." ) ); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | if ( $echo ) { |
| 41 | include $view; |
| 42 | } else { |
| 43 | ob_start(); |
| 44 | include $view; |
| 45 | |
| 46 | return ob_get_clean(); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | function isGroupVisible( $group ) { |
| 51 | $rules_array = array(); |
| 52 | $is_group_visible = false; |
| 53 | $rules = false; |
| 54 | |
| 55 | if ( ! empty( $group['location'] ) && is_array( $group['location'] ) && ! isset( $group['location']['rules'] ) ) { |
| 56 | $rules_array = $group['location']; |
| 57 | } elseif ( ! empty( $group['location'] ) && is_array( $group['location'] ) && isset( $group['location']['rules'] ) && is_array( $group['location']['rules'] ) ) { |
| 58 | $rules_array = $group['location']['rules']; |
| 59 | $rules = true; |
| 60 | } |
| 61 | |
| 62 | if ( ! empty( $rules_array ) ) { |
| 63 | |
| 64 | foreach ( $rules_array as $locationRule ) { |
| 65 | if ( $rules === false ) { |
| 66 | $rule = array_shift( $locationRule ); |
| 67 | } else { |
| 68 | $rule = $locationRule; |
| 69 | } |
| 70 | |
| 71 | if ( \XmlExportEngine::$is_user_export && $rule['param'] == 'user_form' ) { |
| 72 | $is_group_visible = true; |
| 73 | break; |
| 74 | } elseif ( \XmlExportEngine::$is_taxonomy_export && $rule['param'] == 'taxonomy' ) { |
| 75 | $is_group_visible = true; |
| 76 | break; |
| 77 | } elseif ( 'specific' == \XmlExportEngine::$exportOptions['export_type'] && $rule['param'] == 'post_type' ) { |
| 78 | if ( $rule['operator'] == '==' && in_array( $rule['value'], \XmlExportEngine::$post_types ) ) { |
| 79 | $is_group_visible = true; |
| 80 | break; |
| 81 | } elseif ( $rule['operator'] != '==' && ! in_array( $rule['value'], \XmlExportEngine::$post_types ) ) { |
| 82 | $is_group_visible = true; |
| 83 | break; |
| 84 | } |
| 85 | } elseif ( 'advanced' == \XmlExportEngine::$exportOptions['export_type'] ) { |
| 86 | $is_group_visible = true; |
| 87 | break; |
| 88 | } // Include local blocks field groups except when exporting Users. |
| 89 | elseif ( ! \XmlExportEngine::$is_user_export && $rule['param'] == 'block' ) { |
| 90 | $is_group_visible = true; |
| 91 | break; |
| 92 | } |
| 93 | } |
| 94 | } else { |
| 95 | $is_group_visible = true; |
| 96 | } |
| 97 | |
| 98 | return $is_group_visible; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @param array $field |
| 103 | * |
| 104 | * @return string |
| 105 | */ |
| 106 | function getFieldClass( $field, $extras = [] ) { |
| 107 | $class = 'Wpae\AddonAPI\PMXE_Addon_' . ucfirst( $field['type'] ) . '_Field'; |
| 108 | |
| 109 | if (isset($extras[$field['type']])) { |
| 110 | $class = $extras[$field['type']]; |
| 111 | } |
| 112 | |
| 113 | return class_exists( $class ) ? $class : PMXE_Addon_Field::class; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Make a string safe for use as an XML element name. |
| 118 | * |
| 119 | * @param string $elName |
| 120 | * |
| 121 | * @return string |
| 122 | */ |
| 123 | function normalizeElementName( $elName ) { |
| 124 | $elName = str_replace( ' ', '_', $elName ); |
| 125 | |
| 126 | // TODO: Add more replacements here. |
| 127 | return $elName; |
| 128 | } |
| 129 | |
| 130 | // Class Helpers |
| 131 | |
| 132 | trait Singleton { |
| 133 | /** @var self|null */ |
| 134 | private static $instance = null; |
| 135 | |
| 136 | /** |
| 137 | * @return self |
| 138 | */ |
| 139 | final public static function getInstance(): self { |
| 140 | if ( ! self::$instance ) { |
| 141 | self::$instance = new self(); |
| 142 | } |
| 143 | |
| 144 | return self::$instance; |
| 145 | } |
| 146 | |
| 147 | // Prevent cloning of the instance |
| 148 | public function __clone() { |
| 149 | } |
| 150 | |
| 151 | // Prevent deserialization of the instance |
| 152 | public function __wakeup() { |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | |
| 157 | trait Updatable { |
| 158 | public ?PMXE_Addon_Updater $updater; |
| 159 | |
| 160 | public function initEed() { |
| 161 | $api_url = $this->getApiUrl(); |
| 162 | $plugin_file = $this->getPluginFile(); |
| 163 | |
| 164 | if ( empty( $api_url ) ) { |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | $this->updater = new PMXE_Addon_Updater( |
| 169 | $api_url, |
| 170 | $plugin_file, |
| 171 | [ |
| 172 | 'version' => $this->version, // current version number |
| 173 | 'license' => false, // license key (used get_option above to retrieve from DB) |
| 174 | 'item_name' => $this->getEddName(), // name of this plugin |
| 175 | 'author' => $this->getEddAuthor() // author of this plugin |
| 176 | ] |
| 177 | ); |
| 178 | } |
| 179 | |
| 180 | public function getEddName() { |
| 181 | return $this->name(); |
| 182 | } |
| 183 | |
| 184 | public function getEddAuthor() { |
| 185 | return 'Soflyy'; |
| 186 | } |
| 187 | |
| 188 | public function getPluginFile() { |
| 189 | return $this->rootDir . '/plugin.php'; |
| 190 | } |
| 191 | |
| 192 | public function getApiUrl() { |
| 193 | $options = get_option( 'PMXE_Plugin_Options' ); |
| 194 | $api_url = null; |
| 195 | |
| 196 | if ( ! empty( $options['info_api_url_new'] ) ) { |
| 197 | $api_url = $options['info_api_url_new']; |
| 198 | } elseif ( ! empty( $options['info_api_url'] ) ) { |
| 199 | $api_url = $options['info_api_url']; |
| 200 | } |
| 201 | |
| 202 | return $api_url; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | trait NotAString { |
| 207 | public function toString() { |
| 208 | return ''; |
| 209 | } |
| 210 | } |
| 211 |