Start
This commit is contained in:
476
src/Extenders/Submissions.php
Normal file
476
src/Extenders/Submissions.php
Normal file
@@ -0,0 +1,476 @@
|
||||
<?php
|
||||
|
||||
namespace RomhackPlaza\Extenders;
|
||||
defined( '\ABSPATH' ) || exit;
|
||||
|
||||
use RomhackPlaza\API\File_Server;
|
||||
use RomhackPlaza\Extenders\Post\Properties;
|
||||
use RomhackPlaza\Format;
|
||||
use RomhackPlaza\Overrides\Roles;
|
||||
use RomhackPlaza\Script;
|
||||
use RomhackPlaza\Script_Type;
|
||||
|
||||
class Submissions extends Abstract_Extender {
|
||||
|
||||
/**
|
||||
* All Children from submissions.
|
||||
* @var array
|
||||
*/
|
||||
public array $children = [];
|
||||
|
||||
protected function can_extend(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function extend(): void {
|
||||
|
||||
$this->add_action( 'wp_enqueue_scripts', [ $this, 'load_assets' ] );
|
||||
$this->add_filter( 'timber/context', [ $this, 'fill_fields_for_timber' ], 5 );
|
||||
$this->add_action( 'RomhackPlaza\\Extenders\\Submissions\\Get_A_File_Server', [ $this, 'get_file_server' ] );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Return list of IDs for submissions.
|
||||
* @param string $type 'simple', 'complete' or 'all' ( Default 'all' )
|
||||
* @return int[]
|
||||
*/
|
||||
public function get_all_forms_page_ids( string $type = 'all' ): array {
|
||||
|
||||
$option_name = '';
|
||||
|
||||
switch ( $type ) {
|
||||
case 'simple':
|
||||
$option_name = 'simple_submissions_form_page_ids';
|
||||
break;
|
||||
case 'complete':
|
||||
$option_name = 'complete_submissions_form_page_ids';
|
||||
break;
|
||||
default:
|
||||
$type = 'all'; // Make sure it's the affected value.
|
||||
break;
|
||||
}
|
||||
|
||||
global $_romhackplaza;
|
||||
|
||||
if( $type === 'simple' || $type === 'complete' ) {
|
||||
$string_list = $_romhackplaza->settings->get( $option_name );
|
||||
|
||||
$arr = explode( ';', $string_list );
|
||||
if( $arr[0] == '' )
|
||||
$arr = [];
|
||||
return array_map( 'intval', $arr );
|
||||
|
||||
}
|
||||
|
||||
$first_string_list = $_romhackplaza->settings->get( 'simple_submissions_form_page_ids' );
|
||||
$first_arr = explode( ';', $first_string_list );
|
||||
if( $first_arr[0] == '' )
|
||||
$first_arr = [];
|
||||
|
||||
$second_string_list = $_romhackplaza->settings->get( 'complete_submissions_form_page_ids' );
|
||||
$second_arr = explode( ';', $second_string_list );
|
||||
if( $second_arr[0] == '' )
|
||||
$second_arr = [];
|
||||
|
||||
$arr = array_merge( $first_arr, $second_arr );
|
||||
return array_map( 'intval', $arr );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Load CSS and JS scripts only on the good pages.
|
||||
* @return void
|
||||
*/
|
||||
public function load_assets(): void {
|
||||
|
||||
global $post;
|
||||
if( !isset( $post->ID ) )
|
||||
return;
|
||||
|
||||
$simple_page_ids = $this->get_all_forms_page_ids( 'simple' );
|
||||
$complete_page_ids = $this->get_all_forms_page_ids( 'complete' );
|
||||
|
||||
if( in_array( $post->ID, $complete_page_ids ) ){ // Translations, Romhacks, Homebrew, Utilities, Documents, LUA Scripts, Tutorials
|
||||
|
||||
$this->children['script_hash_calc'] = new Script(
|
||||
Script_Type::JS,
|
||||
'hash-calculator',
|
||||
ROMHACKPLAZA_PLUGIN_URI . '/assets/js/submissions/hashes.js',
|
||||
[],
|
||||
'20251106',
|
||||
args: [ 'defer' => true, 'in_footer' => true ]
|
||||
)->enqueue();
|
||||
|
||||
$this->children['script_uploader'] = new Script(
|
||||
Script_Type::JS,
|
||||
'submissions-uploader',
|
||||
ROMHACKPLAZA_PLUGIN_URI . '/assets/js/submissions/uploader.js',
|
||||
[],
|
||||
'20251106',
|
||||
args: [ 'defer' => true, 'in_footer' => true ]
|
||||
)->enqueue()->add_localize(
|
||||
'_romhackplaza_script_uploader',
|
||||
[ 'submit_url' => admin_url( 'admin-ajax.php' ) ],
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
if( in_array( $post->ID, $simple_page_ids ) ){ // News
|
||||
|
||||
$this->children['script_simple_uploader'] = new Script(
|
||||
Script_Type::JS,
|
||||
'submissions-simple-uploader',
|
||||
ROMHACKPLAZA_PLUGIN_URI . '/assets/js/submissions/simple-uploader.js',
|
||||
[],
|
||||
'20251106',
|
||||
args: [ 'defer' => true, 'in_footer' => true ]
|
||||
)->enqueue()->add_localize(
|
||||
'_romhackplaza_script_uploader',
|
||||
[ 'submit_url' => admin_url( 'admin-ajax.php' ) ],
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
if( in_array( $post->ID, $simple_page_ids ) || in_array( $post->ID, $complete_page_ids ) ){
|
||||
|
||||
$this->children['script_css'] = new Script(
|
||||
Script_Type::CSS,
|
||||
'submissions-css',
|
||||
ROMHACKPLAZA_PLUGIN_URI . '/assets/css/submissions.css',
|
||||
[],
|
||||
'20251106',
|
||||
)->enqueue();
|
||||
|
||||
$this->children['script_select2_css'] = new Script(
|
||||
Script_Type::CSS,
|
||||
'select2',
|
||||
'https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css'
|
||||
)->enqueue();
|
||||
|
||||
$this->children['script_select2_js'] = new Script(
|
||||
Script_Type::JS,
|
||||
'select2',
|
||||
'https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js',
|
||||
[ 'jquery' ],
|
||||
'',
|
||||
args: [ 'defer' => true, 'in_footer' => true ]
|
||||
)->enqueue();
|
||||
|
||||
wp_enqueue_media(); // WP Media Library scripts.
|
||||
|
||||
// ACF / ACF Pro Scripts.
|
||||
if( function_exists( 'acf_form_head' ) )
|
||||
acf_form_head();
|
||||
if( function_exists( 'acf_enqueue_scripts' ) )
|
||||
acf_enqueue_scripts();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected function test_terms_array( mixed $terms_array ){
|
||||
|
||||
if( $terms_array && !is_wp_error( $terms_array ) )
|
||||
return $terms_array;
|
||||
return [];
|
||||
}
|
||||
|
||||
protected function priority_language_names(){
|
||||
|
||||
return [ 'English', 'French', 'German', 'Spanish', 'Italian', 'Japanese' ];
|
||||
|
||||
}
|
||||
|
||||
protected function priority_language_terms_obj( array $terms_array ): array{
|
||||
|
||||
$priority = $this->priority_language_names();
|
||||
return array_filter( $terms_array, function( $term ) use ( $priority ){
|
||||
return in_array( $term->name, $priority );
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
protected function add_and_format_language( &$context )
|
||||
{
|
||||
$terms = \get_terms([
|
||||
'taxonomy' => 'language',
|
||||
'hide_empty' => false,
|
||||
]);
|
||||
|
||||
$priority_names = $this->priority_language_names();
|
||||
$priority_terms = $this->priority_language_terms_obj( $terms );
|
||||
$other_terms = array_udiff( $terms, $priority_terms, function( $a, $b ) {
|
||||
return strcmp( $a->term_id, $b->term_id );
|
||||
});
|
||||
|
||||
usort( $priority_terms, function( $a, $b ) use( $priority_names ){
|
||||
$pos_a = array_search( $a->name, $priority_names );
|
||||
$pos_b = array_search( $b->name, $priority_names );
|
||||
return $pos_a - $pos_b;
|
||||
});
|
||||
|
||||
$context['terms_language'] = array_merge( $priority_terms, $other_terms );
|
||||
|
||||
$context['value_terms_language'] = $context['submission_post_id'] != 0 ? wp_get_post_terms( $context['submission_post_id'], 'language', [ 'fields' => 'ids' ] ) |> $this->test_terms_array(...) : [];
|
||||
|
||||
return $context;
|
||||
}
|
||||
|
||||
protected function add_and_format_game( &$context ){
|
||||
|
||||
$game_term = $context['submission_post_id'] != 0 ? get_the_terms( $context['submission_post_id'], 'game' ) : 0;
|
||||
$context['value_game_id'] = $game_term != 0 && !is_wp_error( $game_term ) ? $game_term[0]->term_id : '';
|
||||
$context['value_game_name'] = $game_term != 0 && !is_wp_error( $game_term ) ? $game_term[0]->name : '';
|
||||
|
||||
return $context;
|
||||
|
||||
}
|
||||
|
||||
protected function add_and_format_platform( &$context ){
|
||||
|
||||
$context['terms_platform'] = \Timber\Timber::get_terms([
|
||||
'taxonomy' => 'platform',
|
||||
'hide_empty' => false,
|
||||
]);
|
||||
|
||||
$platform_term = $context['submission_post_id'] != 0 ? wp_get_post_terms( $context['submission_post_id'], 'platform', ['fields' => 'ids'] ) : 0;
|
||||
$context['value_platform_id'] = $platform_term != 0 && !empty( $platform_term ) ? $platform_term[0] : '';
|
||||
|
||||
return $context;
|
||||
}
|
||||
|
||||
protected function add_and_format_hashes( &$context )
|
||||
{
|
||||
$context['value_hashes'] = $context['submission_post_id'] != 0 ? esc_textarea( get_post_meta( $context['submission_post_id'], 'hashes', true ) ) : '';
|
||||
return $context;
|
||||
}
|
||||
|
||||
protected function add_and_format_genre( &$context ){
|
||||
|
||||
$context['terms_genre'] = \Timber\Timber::get_terms([
|
||||
'taxonomy' => 'genre',
|
||||
'hide_empty' => false,
|
||||
]);
|
||||
|
||||
$genre_term = $context['submission_post_id'] != 0 ? wp_get_post_terms( $context['submission_post_id'], 'genre', ['fields' => 'ids'] ) : 0;
|
||||
$context['value_genre_id'] = $genre_term != 0 && !empty( $genre_term ) ? $genre_term[0] : '';
|
||||
|
||||
return $context;
|
||||
}
|
||||
|
||||
protected function add_and_format_status( &$context ){
|
||||
|
||||
$context['list_status'] = \Timber\Timber::get_terms([
|
||||
'taxonomy' => 'hack-status',
|
||||
'hide_empty' => false,
|
||||
]);
|
||||
$context['value_terms_status'] = $context['submission_post_id'] != 0 ? wp_get_post_terms( $context['submission_post_id'], 'hack-status', [ 'fields' => 'ids' ] ) |> $this->test_terms_array(...) : [];
|
||||
|
||||
return $context;
|
||||
}
|
||||
|
||||
protected function add_and_format_main_image_gallery( &$context ){
|
||||
|
||||
if( !function_exists( 'acf_form' ) ){
|
||||
$context['can_acf'] = false;
|
||||
return $context;
|
||||
}
|
||||
|
||||
$context['can_acf'] = true;
|
||||
$context['attachments_settings'] = [
|
||||
'post_id' => $context['submission_post_id'] != 0 ? $context['submission_post_id'] : 'new-post',
|
||||
'fields_groups' => "REPLACED",
|
||||
'fields' => [ 'custom_featured_image', 'my_gallery' ],
|
||||
'submit_value' => __( 'Add Images', 'romhackplaza' ),
|
||||
'updated_message' => __( "Images added!", 'romhackplaza' ),
|
||||
'form' => false
|
||||
];
|
||||
|
||||
return $context;
|
||||
|
||||
}
|
||||
|
||||
protected function add_and_format_author( &$context ){
|
||||
|
||||
$context['value_authors'] = $context['submission_post_id'] != 0 ? get_the_terms( $context['submission_post_id'], 'author' ) |> $this->test_terms_array(...) : [];
|
||||
return $context;
|
||||
|
||||
}
|
||||
|
||||
protected function add_and_format_credits( &$context ){
|
||||
|
||||
$context['value_credits'] = $context['submission_post_id'] != 0 ? esc_textarea( get_post_meta( $context['submission_post_id'], 'staff', true ) ) : '';
|
||||
return $context;
|
||||
|
||||
}
|
||||
|
||||
public function fill_fields_for_timber( $context ){
|
||||
|
||||
global $post;
|
||||
if( !isset( $post->ID ) )
|
||||
return $context;
|
||||
|
||||
$simple_page_ids = $this->get_all_forms_page_ids( 'simple' );
|
||||
$complete_page_ids = $this->get_all_forms_page_ids( 'complete' );
|
||||
|
||||
if( !in_array( $post->ID, $simple_page_ids ) && !in_array( $post->ID, $complete_page_ids ) )
|
||||
return $context;
|
||||
|
||||
/*
|
||||
* The filter is removed to avoid duplications.
|
||||
*/
|
||||
global $_romhackplaza_theme, $_romhackplaza;
|
||||
remove_filter( 'timber/context', [ $_romhackplaza_theme->children['template_context'], 'timber_context_handler_by_page' ] );
|
||||
|
||||
// General ones.
|
||||
|
||||
// If we are on the result page and why.
|
||||
$context['submission_result'] = isset( $_GET['result'] ) ? sanitize_text_field( $_GET['result'] ) : '';
|
||||
|
||||
// If we edit an entry, the post ID.
|
||||
$context['submission_post_id'] = isset( $_GET['edit_entry'] ) ? intval( $_GET['edit_entry'] ) : 0;
|
||||
|
||||
// If we edit an entry on the result page. URL.
|
||||
$context['submission_edit_entry'] = isset( $_GET['edit_entry'] ) ? add_query_arg( [ 'edit_entry' => intval( $_GET['edit_entry'] ) ], get_permalink() ) : '';
|
||||
|
||||
// If the current user is verified or staff.
|
||||
$context['submission_author_is_verified'] = $_romhackplaza->user_roles->current_is( Roles::Verified ) || $_romhackplaza->user_roles->current_is_staff();
|
||||
|
||||
// If the current user can edit this entry, if it already exist.
|
||||
$context['submission_can_edit'] = !isset($context['submission_post_id']) || $_romhackplaza->user_roles->current_can_edit_entry($context['submission_post_id']);
|
||||
|
||||
// If the current user can bypass a locked entry. Only if it is a staff member.
|
||||
$context['submission_can_bypass_locked'] = $_romhackplaza->user_roles->current_is_staff();
|
||||
|
||||
// If the current user can bypass a trashed entry and a private entry. Only an administrator can.
|
||||
$context['submission_can_bypass_trashed_private'] = $_romhackplaza->user_roles->current_is(Roles::Administrator);
|
||||
|
||||
// If the public edit is disabled or not.
|
||||
$context['submission_public_edit_disabled'] = Properties::public_edit_disabled();
|
||||
|
||||
// Submission team message, like queue status. Private message. Locked reason, etc...
|
||||
$context['submission_queue_status'] = isset( $context['submission_post_id'] ) ? get_field( 'queue_status', $context['submission_post_id'] ) : "";
|
||||
if( $context['submission_queue_status'] === "not checked" ) // Default message.
|
||||
$context['submission_queue_status'] = "";
|
||||
|
||||
// Save Nonce
|
||||
$context['submission_save_nonce'] = wp_nonce_field( Format::format_nonce_name( 'submissions_save_entry' ), display: false );
|
||||
|
||||
// Entry title field default name.
|
||||
$context['entry_title_field_name'] = __( "Name:", "romhackplaza" );
|
||||
|
||||
// Entry title value.
|
||||
$context['value_entry_title'] = $context['submission_post_id'] != 0 ? esc_attr( get_post_meta( $context['submission_post_id'], 'entry_title', true ) ) : "";
|
||||
|
||||
// Patch version label.
|
||||
$context['version_number_field_name'] = __( "Version:", "romhackplaza" );
|
||||
|
||||
// Patch version value
|
||||
$context['value_version_number'] = $context['submission_post_id'] != 0 ? esc_attr( get_post_meta( $context['submission_post_id'], 'versionNumber', true ) ) : "";
|
||||
|
||||
// Release date value
|
||||
$context['value_release_date'] = "";
|
||||
$release_date = $context['submission_post_id'] != 0 ? get_post_meta( $context['submission_post_id'], 'release_date', true ) : false;
|
||||
if( $release_date ) {
|
||||
$date = \DateTime::createFromFormat( 'Ymd', $release_date );
|
||||
if( $date )
|
||||
$context['value_release_date'] = $date->format( 'Y-m-d' );
|
||||
}
|
||||
|
||||
// Editor settings and value
|
||||
$context['value_post_content'] = $context['submission_post_id'] != 0 ? get_post_field( 'post_content', $context['submission_post_id'], 'edit' ) : "";
|
||||
$context['post_content_settings'] = [
|
||||
'textarea_name' => 'postContent',
|
||||
'media_buttons' => false,
|
||||
'textarea_rows' => 7,
|
||||
'tinymce' => array(
|
||||
'theme_advanced_buttons1' => 'formatselect,|,bold,italic,underline,|,' .
|
||||
'bullist,numlist,|,link,unlink,|,undo,redo,|,' .
|
||||
'removeformat,|,charmap,|,outdent,indent,|,cut,copy,paste',
|
||||
'content_css' => get_stylesheet_directory_uri() . '/editor.css'
|
||||
),
|
||||
'quicktags' => array('buttons' => 'strong,em,link,block,del,ins,img,ul,ol,li,code,close'),
|
||||
];
|
||||
|
||||
$post_type_rhpz_obj = null;
|
||||
|
||||
if( str_contains( $post->post_name, 'translation' ) ){
|
||||
|
||||
$post_type_rhpz_obj = &$_romhackplaza->cpt->select->translations;
|
||||
$context['entry_title_field_name'] = __( "Custom Name:", "romhackplaza" );
|
||||
$context['version_number_field_name'] = __( "Patch Version:", "romhackplaza" );
|
||||
|
||||
$context = $this->add_and_format_status( $context );
|
||||
$context = $this->add_and_format_language( $context );
|
||||
$context = $this->add_and_format_game( $context );
|
||||
$context = $this->add_and_format_platform( $context );
|
||||
$context = $this->add_and_format_genre( $context );
|
||||
$context = $this->add_and_format_hashes( $context );
|
||||
$context = $this->add_and_format_main_image_gallery( $context );
|
||||
$context = $this->add_and_format_author( $context );
|
||||
$context = $this->add_and_format_credits( $context );
|
||||
|
||||
}
|
||||
|
||||
else if( str_contains( $post->post_name, 'romhack' ) ){
|
||||
|
||||
$post_type_rhpz_obj = &$_romhackplaza->cpt->select->romhacks;
|
||||
$context['version_number_field_name'] = __( "Patch Version:", "romhackplaza" );
|
||||
|
||||
$context = $this->add_and_format_status( $context );
|
||||
$context = $this->add_and_format_language( $context );
|
||||
$context = $this->add_and_format_game( $context );
|
||||
$context = $this->add_and_format_platform( $context );
|
||||
$context = $this->add_and_format_genre( $context );
|
||||
$context = $this->add_and_format_hashes( $context );
|
||||
$context = $this->add_and_format_main_image_gallery( $context );
|
||||
$context = $this->add_and_format_author( $context );
|
||||
$context = $this->add_and_format_credits( $context );
|
||||
|
||||
}
|
||||
|
||||
else if( str_contains( $post->post_name, 'news' ) ){
|
||||
|
||||
$post_type_rhpz_obj = &$_romhackplaza->cpt->select->news;
|
||||
$context['entry_title_field_name'] = __( "Title:", "romhackplaza" );
|
||||
|
||||
}
|
||||
|
||||
// General ones sequel.
|
||||
if( $post_type_rhpz_obj ) {
|
||||
|
||||
// Post type name.
|
||||
$context['post_type'] = $post_type_rhpz_obj->name;
|
||||
|
||||
// Post type singular name.
|
||||
$context['post_type_singular'] = $post_type_rhpz_obj->singular_name;
|
||||
|
||||
// ACF Field group.
|
||||
$context['post_type_acf'] = $post_type_rhpz_obj->getAcfFieldGroupKey();
|
||||
|
||||
// Add for main image and gallery
|
||||
if( isset( $context['attachments_settings'] ) )
|
||||
$context['attachments_settings']['field_groups'] = [ $context['post_type_acf'] ];
|
||||
|
||||
}
|
||||
|
||||
return $context;
|
||||
}
|
||||
|
||||
public function get_file_server( int $post_id = 0 ){
|
||||
|
||||
global $post;
|
||||
if( !isset( $post->ID ) )
|
||||
return;
|
||||
|
||||
$complete_page_ids = $this->get_all_forms_page_ids( 'complete' );
|
||||
|
||||
if( in_array( $post->ID, $complete_page_ids ) ) { // Translations, Romhacks, Homebrew, Utilities, Documents, LUA Scripts, Tutorials
|
||||
new File_Server($post_id)->print_js();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user