Skip to content

Commit

Permalink
Updating the create content model.
Browse files Browse the repository at this point in the history
  • Loading branch information
krugazul committed Jan 30, 2025
1 parent 1135165 commit 22bce91
Show file tree
Hide file tree
Showing 22 changed files with 1,999 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
28 changes: 28 additions & 0 deletions create-content-model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
declare( strict_types = 1 );

if ( defined( 'CONTENT_MODEL_PLUGIN_FILE' ) ) {
return;
}

define( 'CONTENT_MODEL_PLUGIN_FILE', __FILE__ );
define( 'CONTENT_MODEL_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
define( 'CONTENT_MODEL_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'CONTENT_MODEL_PLUGIN_VER', '1.0.1' );

if ( ! function_exists( 'content_model_require_if_exists' ) ) {
/**
* Requires a file if it exists.
*
* @param string $file The file to require.
*/
function content_model_require_if_exists( string $file ) {
if ( file_exists( $file ) ) {
require_once $file;
}
}
}

content_model_require_if_exists( __DIR__ . '/includes/json-initializer/0-load.php' );
content_model_require_if_exists( __DIR__ . '/includes/runtime/0-load.php' );
content_model_require_if_exists( __DIR__ . '/includes/taxonomies/0-load.php' );
Binary file added vendors/.DS_Store
Binary file not shown.
Binary file added vendors/create-content-model/.DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions vendors/create-content-model/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# create-content-modal
A stripped down version of the create content modal found in the TO plugin.
28 changes: 28 additions & 0 deletions vendors/create-content-model/create-content-model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
declare( strict_types = 1 );

if ( defined( 'CONTENT_MODEL_PLUGIN_FILE' ) ) {
return;
}

define( 'CONTENT_MODEL_PLUGIN_FILE', __FILE__ );
define( 'CONTENT_MODEL_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
define( 'CONTENT_MODEL_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'CONTENT_MODEL_PLUGIN_VER', '1.0.1' );

if ( ! function_exists( 'content_model_require_if_exists' ) ) {
/**
* Requires a file if it exists.
*
* @param string $file The file to require.
*/
function content_model_require_if_exists( string $file ) {
if ( file_exists( $file ) ) {
require_once $file;
}
}
}

content_model_require_if_exists( __DIR__ . '/includes/json-initializer/0-load.php' );
content_model_require_if_exists( __DIR__ . '/includes/runtime/0-load.php' );
content_model_require_if_exists( __DIR__ . '/includes/taxonomies/0-load.php' );
Binary file not shown.
15 changes: 15 additions & 0 deletions vendors/create-content-model/includes/json-initializer/0-load.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* Loads the JSON initializer.
*
* @package create-content-model
*/

declare( strict_types = 1 );

require_once __DIR__ . '/class-content-model-json-initializer.php';

add_action(
'init',
array( Content_Model_Json_Initializer::class, 'maybe_register_content_models_from_json' )
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php
/**
* Initializes the content models from JSON files.
*
* @package content-model
*/

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Initializes the content models from JSON files.
*/
class Content_Model_Json_Initializer {
private const CREATE_CONTENT_MODEL_OPTION = 'create-content-model';

/**
* Register the content models from JSON files if the current version is not the latest.
*/
public static function maybe_register_content_models_from_json() {

$option = get_option( self::CREATE_CONTENT_MODEL_OPTION, array() );

$version = $option['version'] ?? null;

if ( ! function_exists( 'get_plugin_data' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}

$plugin_data = get_plugin_data( CONTENT_MODEL_PLUGIN_FILE );

/**
* TODO: Make sure this uses the plugin version.
*/
if ( ! isset( $plugin_data['Version'] ) || empty( $plugin_data['Version'] ) && defined( 'CONTENT_MODEL_PLUGIN_VER' ) ) {
//CONTENT_MODEL_PLUGIN_VER
$plugin_data['Version'] = time();
}

if ( $plugin_data['Version'] === $version ) {
return;
}

global $CONTENT_MODEL_JSON_PATH;
if ( ! isset( $CONTENT_MODEL_JSON_PATH ) ) {
return;
}

$post_types = [];

foreach ( $CONTENT_MODEL_JSON_PATH as $json_path ) {
$types = glob( $json_path . '/post-types/*.json' );
$types = array_map(
fn( $file ) => json_decode( file_get_contents( $file ), true ),
$types
);
$post_types = array_merge( $post_types, $types );
}

self::register_content_models_from_json( $post_types );
self::delete_dangling_content_models( $post_types );
$option['version'] = $plugin_data['Version'];
update_option( self::CREATE_CONTENT_MODEL_OPTION, $option );
}

/**
* Register the content models from JSON files.
*
* @param array $post_types The post types from the JSON files.
*/
private static function register_content_models_from_json( $post_types ) {
$content_models = self::group_content_models_by_slug();

foreach ( $post_types as $post_type ) {

$content_model_post = array(
'post_name' => $post_type['slug'],
'post_title' => $post_type['label'],
'post_status' => 'publish',
'post_type' => Content_Model_Manager::POST_TYPE_NAME,
'post_content' => serialize_blocks( $post_type['template'] ),
);

$existing_content_model = $content_models[ $post_type['slug'] ] ?? null;

if ( $existing_content_model ) {
$content_model_post['ID'] = $existing_content_model->ID;
}

$post_id = wp_insert_post( $content_model_post );

update_post_meta( $post_id, 'plural_label', $post_type['pluralLabel'] );
update_post_meta( $post_id, 'icon', $post_type['icon'] );
update_post_meta( $post_id, 'fields', wp_json_encode( $post_type['fields'] ) );
}
}

/**
* Deletes content models not included in the JSON files.
*
* @param array $post_types The post types from the JSON files.
*
* @return void
*/
private static function delete_dangling_content_models( $post_types ) {
$content_models = self::group_content_models_by_slug();
$post_types = self::group_post_types_by_slug( $post_types );

foreach ( $content_models as $model_slug => $model ) {
if ( ! isset( $post_types[ $model_slug ] ) ) {
wp_delete_post( $model->ID, false );
}
}
}

/**
* Groups existing content models by their slug.
*
* @return array An array of content models, keyed by their slug.
*/
private static function group_content_models_by_slug() {
$models = Content_Model_Manager::get_content_models_from_database();

return array_reduce(
$models,
function ( $carry, $model ) {
$carry[ $model->post_name ] = $model;
return $carry;
},
array()
);
}

/**
* Groups existing post types by their slug.
*
* @param array $post_types The post types from the JSON files.
*
* @return array An array of post types, keyed by their slug.
*/
private static function group_post_types_by_slug( $post_types ) {
return array_reduce(
$post_types,
function ( $carry, $post_type ) {
$carry[ $post_type['slug'] ] = $post_type;
return $carry;
},
array()
);
}
}
17 changes: 17 additions & 0 deletions vendors/create-content-model/includes/runtime/0-load.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* Loads the runtime.
*
* @package create-content-model
*/

declare( strict_types = 1 );

require_once __DIR__ . '/class-content-model-manager.php';
require_once __DIR__ . '/class-content-model.php';
require_once __DIR__ . '/class-content-model-block.php';
require_once __DIR__ . '/class-content-model-data-hydrator.php';
require_once __DIR__ . '/class-content-model-html-manipulator.php';
require_once __DIR__ . '/helpers.php';

add_action( 'init', array( Content_Model_Manager::class, 'get_instance' ) );
Loading

0 comments on commit 22bce91

Please sign in to comment.