| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | if ( ! defined( 'ABSPATH' ) ) { |
| 18: | exit; |
| 19: | } |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | class EPL_Elementor_Template_Seeder { |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | const STATE_OPTION = 'epl_elementor_bundled_templates'; |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | const SLUG_META = '_epl_bundled_template'; |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | public function __construct() { |
| 63: | |
| 64: | |
| 65: | |
| 66: | add_action( 'admin_init', array( $this, 'maybe_seed' ), 20 ); |
| 67: | } |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | |
| 79: | public static function get_bundled_templates() { |
| 80: | return array( |
| 81: | 'epl-card' => array( |
| 82: | 'title' => __( 'EPL Card', 'easy-property-listings' ), |
| 83: | 'type' => EPL_Elementor_Loop_Card_Document::TYPE, |
| 84: | 'context' => 'listing', |
| 85: | 'file' => EPL_PATH_LIB . 'page-builders/elementor/templates/epl-card.json', |
| 86: | 'version' => '1.0', |
| 87: | ), |
| 88: | ); |
| 89: | } |
| 90: | |
| 91: | |
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | |
| 98: | |
| 99: | public function maybe_seed() { |
| 100: | if ( ! did_action( 'elementor/loaded' ) ) { |
| 101: | return; |
| 102: | } |
| 103: | |
| 104: | if ( ! current_user_can( 'edit_theme_options' ) ) { |
| 105: | return; |
| 106: | } |
| 107: | |
| 108: | $state = (array) get_option( self::STATE_OPTION, array() ); |
| 109: | $changed = false; |
| 110: | |
| 111: | |
| 112: | $current = true; |
| 113: | foreach ( self::get_bundled_templates() as $slug => $template ) { |
| 114: | if ( ! isset( $state[ $slug ]['version'] ) || $state[ $slug ]['version'] !== $template['version'] ) { |
| 115: | $current = false; |
| 116: | break; |
| 117: | } |
| 118: | } |
| 119: | if ( $current ) { |
| 120: | return; |
| 121: | } |
| 122: | |
| 123: | |
| 124: | if ( ! add_option( 'epl_elementor_seeding', time(), '', false ) ) { |
| 125: | if ( (int) get_option( 'epl_elementor_seeding' ) > time() - 60 ) { |
| 126: | return; |
| 127: | } |
| 128: | update_option( 'epl_elementor_seeding', time(), false ); |
| 129: | } |
| 130: | |
| 131: | foreach ( self::get_bundled_templates() as $slug => $template ) { |
| 132: | $result = $this->seed_template( $slug, $template, $state ); |
| 133: | |
| 134: | if ( null !== $result ) { |
| 135: | $state[ $slug ] = $result; |
| 136: | $changed = true; |
| 137: | } |
| 138: | } |
| 139: | |
| 140: | if ( $changed ) { |
| 141: | update_option( self::STATE_OPTION, $state ); |
| 142: | } |
| 143: | |
| 144: | delete_option( 'epl_elementor_seeding' ); |
| 145: | } |
| 146: | |
| 147: | |
| 148: | |
| 149: | |
| 150: | |
| 151: | |
| 152: | |
| 153: | |
| 154: | |
| 155: | |
| 156: | private function seed_template( $slug, $template, $state ) { |
| 157: | $json = $this->read_template_file( $template['file'] ); |
| 158: | |
| 159: | if ( '' === $json ) { |
| 160: | return null; |
| 161: | } |
| 162: | |
| 163: | $checksum = md5( $json ); |
| 164: | $existing = isset( $state[ $slug ] ) ? $state[ $slug ] : array(); |
| 165: | $post_id = isset( $existing['post_id'] ) ? absint( $existing['post_id'] ) : 0; |
| 166: | |
| 167: | |
| 168: | |
| 169: | |
| 170: | if ( $post_id ) { |
| 171: | if ( 'publish' !== get_post_status( $post_id ) ) { |
| 172: | return null; |
| 173: | } |
| 174: | |
| 175: | $installed_checksum = isset( $existing['checksum'] ) ? $existing['checksum'] : ''; |
| 176: | $current_checksum = md5( (string) get_post_meta( $post_id, '_elementor_data', true ) ); |
| 177: | |
| 178: | |
| 179: | if ( $checksum === $installed_checksum || $current_checksum !== $installed_checksum ) { |
| 180: | return null; |
| 181: | } |
| 182: | |
| 183: | $this->write_template_data( $post_id, $json ); |
| 184: | |
| 185: | return array( |
| 186: | 'post_id' => $post_id, |
| 187: | 'checksum' => $checksum, |
| 188: | 'version' => $template['version'], |
| 189: | ); |
| 190: | } |
| 191: | |
| 192: | if ( ! empty( $existing ) ) { |
| 193: | return null; |
| 194: | } |
| 195: | |
| 196: | |
| 197: | |
| 198: | |
| 199: | |
| 200: | $orphan = $this->find_seeded_post( $slug ); |
| 201: | |
| 202: | if ( $orphan ) { |
| 203: | return array( |
| 204: | 'post_id' => $orphan, |
| 205: | 'checksum' => md5( (string) get_post_meta( $orphan, '_elementor_data', true ) ), |
| 206: | 'version' => $template['version'], |
| 207: | ); |
| 208: | } |
| 209: | |
| 210: | $post_id = wp_insert_post( |
| 211: | array( |
| 212: | 'post_title' => $template['title'], |
| 213: | 'post_type' => 'elementor_library', |
| 214: | 'post_status' => 'publish', |
| 215: | ), |
| 216: | true |
| 217: | ); |
| 218: | |
| 219: | if ( is_wp_error( $post_id ) || ! $post_id ) { |
| 220: | return null; |
| 221: | } |
| 222: | |
| 223: | update_metadata( 'post', $post_id, '_elementor_edit_mode', 'builder' ); |
| 224: | update_metadata( 'post', $post_id, '_elementor_template_type', $template['type'] ); |
| 225: | update_metadata( 'post', $post_id, '_epl_elementor_template_context', $template['context'] ); |
| 226: | update_metadata( 'post', $post_id, self::SLUG_META, $slug ); |
| 227: | |
| 228: | if ( defined( 'ELEMENTOR_VERSION' ) ) { |
| 229: | update_metadata( 'post', $post_id, '_elementor_version', ELEMENTOR_VERSION ); |
| 230: | } |
| 231: | |
| 232: | $this->write_template_data( $post_id, $json ); |
| 233: | |
| 234: | return array( |
| 235: | 'post_id' => $post_id, |
| 236: | 'checksum' => $checksum, |
| 237: | 'version' => $template['version'], |
| 238: | ); |
| 239: | } |
| 240: | |
| 241: | |
| 242: | |
| 243: | |
| 244: | |
| 245: | |
| 246: | |
| 247: | |
| 248: | |
| 249: | private function find_seeded_post( $slug ) { |
| 250: | $found = get_posts( |
| 251: | array( |
| 252: | 'post_type' => 'elementor_library', |
| 253: | 'post_status' => 'publish', |
| 254: | 'posts_per_page' => 1, |
| 255: | 'orderby' => 'ID', |
| 256: | 'order' => 'ASC', |
| 257: | 'fields' => 'ids', |
| 258: | 'no_found_rows' => true, |
| 259: | 'meta_query' => array( |
| 260: | array( |
| 261: | 'key' => self::SLUG_META, |
| 262: | 'value' => $slug, |
| 263: | ), |
| 264: | ), |
| 265: | ) |
| 266: | ); |
| 267: | |
| 268: | return empty( $found ) ? 0 : absint( $found[0] ); |
| 269: | } |
| 270: | |
| 271: | |
| 272: | |
| 273: | |
| 274: | |
| 275: | |
| 276: | |
| 277: | |
| 278: | |
| 279: | |
| 280: | private function write_template_data( $post_id, $json ) { |
| 281: | |
| 282: | update_metadata( 'post', $post_id, '_elementor_data', wp_slash( $json ) ); |
| 283: | delete_metadata( 'post', $post_id, '_elementor_css' ); |
| 284: | delete_metadata( 'post', $post_id, '_elementor_element_cache' ); |
| 285: | |
| 286: | if ( class_exists( '\Elementor\Plugin' ) && isset( \Elementor\Plugin::$instance->files_manager ) ) { |
| 287: | \Elementor\Plugin::$instance->files_manager->clear_cache(); |
| 288: | } |
| 289: | } |
| 290: | |
| 291: | |
| 292: | |
| 293: | |
| 294: | |
| 295: | |
| 296: | |
| 297: | |
| 298: | private function read_template_file( $file ) { |
| 299: | if ( ! is_readable( $file ) ) { |
| 300: | return ''; |
| 301: | } |
| 302: | |
| 303: | $raw = file_get_contents( $file ); |
| 304: | |
| 305: | if ( false === $raw ) { |
| 306: | return ''; |
| 307: | } |
| 308: | |
| 309: | $decoded = json_decode( $raw, true ); |
| 310: | |
| 311: | if ( ! is_array( $decoded ) || empty( $decoded ) ) { |
| 312: | return ''; |
| 313: | } |
| 314: | |
| 315: | return wp_json_encode( $decoded ); |
| 316: | } |
| 317: | |
| 318: | |
| 319: | |
| 320: | |
| 321: | |
| 322: | |
| 323: | |
| 324: | |
| 325: | public static function get_template_id( $slug ) { |
| 326: | $state = (array) get_option( self::STATE_OPTION, array() ); |
| 327: | |
| 328: | if ( empty( $state[ $slug ]['post_id'] ) ) { |
| 329: | return 0; |
| 330: | } |
| 331: | |
| 332: | $post_id = absint( $state[ $slug ]['post_id'] ); |
| 333: | |
| 334: | return ( 'publish' === get_post_status( $post_id ) ) ? $post_id : 0; |
| 335: | } |
| 336: | |
| 337: | |
| 338: | |
| 339: | |
| 340: | |
| 341: | |
| 342: | |
| 343: | public static function get_default_card_id() { |
| 344: | return (int) apply_filters( 'epl_elementor_default_card_id', self::get_template_id( 'epl-card' ) ); |
| 345: | } |
| 346: | } |
| 347: | |