self::SCHEMA, 'exported_at' => gmdate( 'c' ), 'from' => (string) wp_parse_url( home_url(), PHP_URL_HOST ), 'shape' => $shape ? array( 'sell' => $shape['sell'], 'where' => $shape['where'], 'first' => $shape['first'] ) : array(), 'modules' => array(), 'overlaps' => array(), ); if ( class_exists( 'AIEO_DMM_Settings' ) ) { foreach ( (array) AIEO_DMM_Settings::get( 'enabled_modules', array() ) as $id => $on ) { $out['modules'][ sanitize_key( (string) $id ) ] = (bool) $on; } if ( class_exists( 'AIEO_DMM_Brand_Gifts' ) && method_exists( 'AIEO_DMM_Brand_Gifts', 'toggle_is_on' ) ) { $out['modules']['brand_gifts'] = AIEO_DMM_Brand_Gifts::toggle_is_on(); } } if ( class_exists( 'AIEO_Overlap' ) ) { foreach ( AIEO_Overlap::decisions() as $id => $row ) { $d = isset( $row['decision'] ) ? (string) $row['decision'] : ''; if ( in_array( $d, AIEO_Overlap::DECISIONS, true ) ) { $out['overlaps'][ sanitize_key( (string) $id ) ] = $d; } } } return $out; } public static function export_json() { return (string) wp_json_encode( self::export(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); } public static function import( $json, $uid ) { $out = array( 'ok' => false, 'shape' => false, 'modules' => 0, 'overlaps' => 0, 'code' => '', 'error' => '' ); $data = json_decode( (string) $json, true ); if ( ! is_array( $data ) ) { $out['code'] = 'json'; $out['error'] = self::message( 'json' ); return $out; } if ( self::SCHEMA !== (int) ( $data['schema'] ?? 0 ) ) { $out['code'] = 'schema'; $out['error'] = self::message( 'schema' ); return $out; } if ( class_exists( 'AIEO_Shop_Shape' ) && ! empty( $data['shape'] ) && is_array( $data['shape'] ) ) { if ( AIEO_Shop_Shape::save( $data['shape'], (int) $uid ) ) { $out['shape'] = true; AIEO_Shop_Shape::seed_pins( (int) $uid ); } } if ( class_exists( 'AIEO_DMM_Settings' ) && ! empty( $data['modules'] ) && is_array( $data['modules'] ) ) { $known = (array) ( AIEO_DMM_Settings::defaults()['enabled_modules'] ?? array() ); $patch = array(); $gifts = null; foreach ( $data['modules'] as $id => $on ) { $id = sanitize_key( (string) $id ); if ( 'brand_gifts' === $id && class_exists( 'AIEO_DMM_Brand_Gifts' ) && method_exists( 'AIEO_DMM_Brand_Gifts', 'set_toggle' ) ) { $gifts = (bool) $on; continue; } if ( array_key_exists( $id, $known ) ) { $patch[ $id ] = (bool) $on; } } if ( $patch ) { AIEO_DMM_Settings::update( array( 'enabled_modules' => $patch ) ); $out['modules'] = count( $patch ); } if ( null !== $gifts ) { AIEO_DMM_Brand_Gifts::set_toggle( $gifts ); ++$out['modules']; } } if ( class_exists( 'AIEO_Overlap' ) && ! empty( $data['overlaps'] ) && is_array( $data['overlaps'] ) ) { foreach ( $data['overlaps'] as $id => $d ) { if ( AIEO_Overlap::decide( (string) $id, (string) $d, (int) $uid ) ) { $out['overlaps']++; } } } $out['ok'] = $out['shape'] || $out['modules'] > 0 || $out['overlaps'] > 0; if ( ! $out['ok'] ) { $out['code'] = 'empty'; $out['error'] = self::message( 'empty' ); } return $out; } public static function message( $code ) { switch ( (string) $code ) { case 'json': return __( 'That is not a preset: the text could not be read as JSON.', 'ai-eshop-optimizer' ); case 'schema': return __( 'That preset was written by a different version and cannot be applied here.', 'ai-eshop-optimizer' ); case 'empty': return __( 'The preset was read but carried nothing this build could apply.', 'ai-eshop-optimizer' ); } return ''; } }