diff --git a/src/Extenders/Discord_Notification.php b/src/Extenders/Discord_Notification.php index 9e48d7e..95bbb0d 100644 --- a/src/Extenders/Discord_Notification.php +++ b/src/Extenders/Discord_Notification.php @@ -1,6 +1,7 @@ add_action( 'RomhackPlaza\\Extenders\\Discord_Notification\\Send_Notification_After_Submission', [ $this, 'send_notification_after_submission' ], 10, 4 ); + $this->add_action( 'RomhackPlaza\\Extenders\\Discord_Notification\\Send_Notification_After_Submission', + [ $this, 'send_notification_after_submission' ], 10, 4 ); } protected function get_webhook_url( string $type = 'global' ): string { @@ -93,7 +95,7 @@ class Discord_Notification extends Abstract_Extender { if( $webhook == null || $content == null ) return; $this->send_to_discord( $webhook, $content ); - update_post_meta( $post_id, 'discord_noticed', "yes" ); + update_post_meta( $post_id, Post_Fields::DISCORD_NOTICED->value, "yes" ); } } \ No newline at end of file diff --git a/src/Extenders/Submissions.php b/src/Extenders/Submissions.php index 9f2248a..c4a6eef 100644 --- a/src/Extenders/Submissions.php +++ b/src/Extenders/Submissions.php @@ -6,6 +6,9 @@ defined( '\ABSPATH' ) || exit; use RomhackPlaza\API\File_Server; use RomhackPlaza\Extenders\Post\Properties; use RomhackPlaza\Format; +use RomhackPlaza\Identifiers\Post_Fields; +use RomhackPlaza\Identifiers\Taxonomies; +use RomhackPlaza\Modal; use RomhackPlaza\Overrides\Roles; use RomhackPlaza\Script; use RomhackPlaza\Script_Type; @@ -213,14 +216,16 @@ class Submissions extends Abstract_Extender { $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(...) : []; + $context['value_terms_language'] = $context['submission_post_id'] != 0 + ? wp_get_post_terms( $context['submission_post_id'], Taxonomies::LANG->value, [ '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; + $game_term = $context['submission_post_id'] != 0 ? get_the_terms( $context['submission_post_id'], Taxonomies::GAME->value ) : 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 : ''; @@ -231,11 +236,11 @@ class Submissions extends Abstract_Extender { protected function add_and_format_platform( &$context ){ $context['terms_platform'] = \Timber\Timber::get_terms([ - 'taxonomy' => 'platform', + 'taxonomy' => Taxonomies::PLATFORM->value, 'hide_empty' => false, ]); - $platform_term = $context['submission_post_id'] != 0 ? wp_get_post_terms( $context['submission_post_id'], 'platform', ['fields' => 'ids'] ) : 0; + $platform_term = $context['submission_post_id'] != 0 ? wp_get_post_terms( $context['submission_post_id'], Taxonomies::PLATFORM->value , ['fields' => 'ids'] ) : 0; $context['value_platform_id'] = $platform_term != 0 && !empty( $platform_term ) ? $platform_term[0] : ''; return $context; @@ -243,18 +248,18 @@ class Submissions extends Abstract_Extender { 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 ) ) : ''; + $context['value_hashes'] = $context['submission_post_id'] != 0 ? esc_textarea( get_post_meta( $context['submission_post_id'], Post_Fields::HASHES->value, true ) ) : ''; return $context; } protected function add_and_format_genre( &$context ){ $context['terms_genre'] = \Timber\Timber::get_terms([ - 'taxonomy' => 'genre', + 'taxonomy' => Taxonomies::GENRE->value, 'hide_empty' => false, ]); - $genre_term = $context['submission_post_id'] != 0 ? wp_get_post_terms( $context['submission_post_id'], 'genre', ['fields' => 'ids'] ) : 0; + $genre_term = $context['submission_post_id'] != 0 ? wp_get_post_terms( $context['submission_post_id'], Taxonomies::GENRE->value, ['fields' => 'ids'] ) : 0; $context['value_genre_id'] = $genre_term != 0 && !empty( $genre_term ) ? $genre_term[0] : ''; return $context; @@ -262,11 +267,11 @@ class Submissions extends Abstract_Extender { protected function add_and_format_status( &$context ){ - $context['list_status'] = \Timber\Timber::get_terms([ - 'taxonomy' => 'hack-status', + $context['terms_status'] = \Timber\Timber::get_terms([ + 'taxonomy' => Taxonomies::STATUS->value, '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(...) : []; + $context['value_terms_status'] = $context['submission_post_id'] != 0 ? wp_get_post_terms( $context['submission_post_id'], Taxonomies::STATUS->value, [ 'fields' => 'ids' ] ) |> $this->test_terms_array(...) : []; return $context; } @@ -282,7 +287,7 @@ class Submissions extends Abstract_Extender { $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' ], + 'fields' => [ Post_Fields::MAIN_IMAGE->value, Post_Fields::GALLERY->value ], 'submit_value' => __( 'Add Images', 'romhackplaza' ), 'updated_message' => __( "Images added!", 'romhackplaza' ), 'form' => false @@ -294,18 +299,25 @@ class Submissions extends Abstract_Extender { 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(...) : []; + $context['value_authors'] = $context['submission_post_id'] != 0 ? get_the_terms( $context['submission_post_id'], Taxonomies::AUTHORS->value ) |> $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 ) ) : ''; + $context['value_credits'] = $context['submission_post_id'] != 0 ? esc_textarea( get_post_meta( $context['submission_post_id'], Post_Fields::STAFF_CREDITS->value, true ) ) : ''; return $context; } + protected function add_and_format_extra_links( &$context ){ + + $context['value_release_site'] = $context['submission_post_id'] != 0 ? esc_attr( get_post_meta( $context['submission_post_id'], Post_Fields::RELEASE_SITE->value, true ) ) : ''; + $context['value_youtube_video'] = $context['submission_post_id'] != 0 ? esc_attr( get_post_meta( $context['submission_post_id'], Post_Fields::YOUTUBE_VIDEO->value, true ) ) : ''; + return $context; + } + public function fill_fields_for_timber( $context ){ global $post; @@ -362,17 +374,17 @@ class Submissions extends Abstract_Extender { $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 ) ) : ""; + $context['value_entry_title'] = $context['submission_post_id'] != 0 ? esc_attr( get_post_meta( $context['submission_post_id'], Post_Fields::ENTRY_TITLE->value, 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 ) ) : ""; + $context['value_version_number'] = $context['submission_post_id'] != 0 ? esc_attr( get_post_meta( $context['submission_post_id'], Post_Fields::VERSION_NUMBER->value, 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; + $release_date = $context['submission_post_id'] != 0 ? get_post_meta( $context['submission_post_id'], Post_Fields::LAST_UPDATED->value, true ) : false; if( $release_date ) { $date = \DateTime::createFromFormat( 'Ymd', $release_date ); if( $date ) @@ -394,6 +406,8 @@ class Submissions extends Abstract_Extender { 'quicktags' => array('buttons' => 'strong,em,link,block,del,ins,img,ul,ol,li,code,close'), ]; + $context['submission_save_as_draft_checkbox'] = $context['submission_post_id'] != 0 && get_post_status($context['submission_post_id']) === 'draft'; + $post_type_rhpz_obj = null; if( str_contains( $post->post_name, 'translation' ) ){ @@ -411,6 +425,7 @@ class Submissions extends Abstract_Extender { $context = $this->add_and_format_main_image_gallery( $context ); $context = $this->add_and_format_author( $context ); $context = $this->add_and_format_credits( $context ); + $context = $this->add_and_format_extra_links( $context ); } @@ -456,6 +471,10 @@ class Submissions extends Abstract_Extender { } + new Modal( + 'submissions', "", "" + )->render(); + return $context; } diff --git a/src/Extenders/Timber/SubmissionAccordionNode.php b/src/Extenders/Timber/SubmissionAccordionNode.php index dc08044..38b4ff7 100644 --- a/src/Extenders/Timber/SubmissionAccordionNode.php +++ b/src/Extenders/Timber/SubmissionAccordionNode.php @@ -28,6 +28,9 @@ class SubmissionAccordionNode extends Node { case 'team-information': return __('Team Information', 'romhackplaza'); break; + case 'extra-links': + return __( 'Related Links', 'romhackplaza'); + break; } return __( "Unknown", 'romhackplaza' ); @@ -51,6 +54,9 @@ class SubmissionAccordionNode extends Node { case 'team-information': return 'fas fa-users'; break; + case 'extra-links': + return 'fas fa-link'; + break; } return 'fas fa-question-circle'; diff --git a/src/Identifier.php b/src/Identifier.php deleted file mode 100644 index 1f085f4..0000000 --- a/src/Identifier.php +++ /dev/null @@ -1,46 +0,0 @@ -type == Script_Type::JS ) { + $this->deps[] = 'wp-i18n'; + wp_set_script_translations($this->handle, 'romhackplaza'); + } + + return $this; + + } + } \ No newline at end of file diff --git a/ts/globals.d.ts b/ts/globals.d.ts index a93f972..a46dfe0 100644 --- a/ts/globals.d.ts +++ b/ts/globals.d.ts @@ -1,3 +1,6 @@ /// declare const $: any; -declare const jQuery: any; \ No newline at end of file +declare const jQuery: any; + +/// +declare function romhackplaza_manage_modal( a: object, b: string|undefined, c: string|undefined, d: string|undefined, e: string|undefined ): void; \ No newline at end of file diff --git a/ts/submissions/class-drop-container.ts b/ts/submissions/class-drop-container.ts new file mode 100644 index 0000000..cf60111 --- /dev/null +++ b/ts/submissions/class-drop-container.ts @@ -0,0 +1,73 @@ +import {type Can_Upload_Detail, __} from "./globals"; + +export class DropContainer { + + element: HTMLElement; + text_element: HTMLElement; + enabled: boolean; + + constructor(element: HTMLElement, text_element: HTMLElement) { + + this.element = element; + this.text_element = text_element; + this.enabled = true; + + this.addEvents(); + + } + + switch(){ + + this.enabled = !this.enabled; + if( !this.enabled ) + this.text_element.innerHTML = __( "Please wait...", 'romhackplaza' ); + else + this.text_element.innerHTML = __( "Drop files here or click to upload the file", 'romhackplaza' ); + + } + + private addEvents(): void { + + this.element.addEventListener( 'click', this.submitFile, false ); + this.element.addEventListener( 'dragover', (e: Event) => e.preventDefault(), false ); + this.element.addEventListener( 'dragenter', (e: Event) => this.element.classList.add( 'drag-active'), false ); + this.element.addEventListener( 'dragleave', (e: Event) => this.element.classList.remove( 'drag-active'), false ); + this.element.addEventListener( 'drop', this.dropElement ); + } + + submitFile( e: Event ): void { + + if( this.enabled ) { + + let input = document.createElement("input"); + input.type = "file"; + input.onchange = ( e: Event ) => { + // @ts-ignore + let files: Array = Array.from( input.files ); + this.dispatchEvent( files[0] as File ); + } + input.click(); + } + } + + dropElement( e: Event ): void { + + e.preventDefault(); + this.element.classList.remove( 'drag-active'); + if( this.enabled ) + // @ts-ignore + this.dispatchEvent( e.dataTransfer.files[0] ); + } + + dispatchEvent( file: File ) { + + const evt = new CustomEvent( 'can_upload', { + detail: { + file + } + }); + document.dispatchEvent(evt); + + } + +} \ No newline at end of file diff --git a/ts/submissions/class-file-list.ts b/ts/submissions/class-file-list.ts new file mode 100644 index 0000000..9902677 --- /dev/null +++ b/ts/submissions/class-file-list.ts @@ -0,0 +1,8 @@ +import { Server_File } from './class-server-file'; + +export class FileList { + + list: Server_File[] = []; + + +} \ No newline at end of file diff --git a/ts/submissions/class-server-file.ts b/ts/submissions/class-server-file.ts new file mode 100644 index 0000000..2007273 --- /dev/null +++ b/ts/submissions/class-server-file.ts @@ -0,0 +1,4 @@ +export class Server_File { + + is_dirty: boolean = false; +} \ No newline at end of file diff --git a/ts/submissions/class-upload.ts b/ts/submissions/class-upload.ts new file mode 100644 index 0000000..0bdef96 --- /dev/null +++ b/ts/submissions/class-upload.ts @@ -0,0 +1,65 @@ +import {__, FORBIDDEN_CARS, romhackplaza_modal_submissions, during_upload, I } from "./globals"; +import {DropContainer} from "./class-drop-container"; + +export class Upload { + + file: File; + + constructor( file: File ){ + + this.file = file; + this.beginUpload(); + + } + + private beginUpload(){ + + if( this.checkForbiddenCars() ){ + romhackplaza_manage_modal( romhackplaza_modal_submissions, "block", __( "Submit error", 'romhackplaza' ), __( "There is a forbidden character in the file name, please change it.", 'romhackplaza' ), "" ); + return; + } + if( !this.file ) { + console.error("WTF at beginUpload method."); + return; + } + this.switchDuringUpload(); + if( typeof I.drop_container !== 'undefined' && I.drop_container instanceof DropContainer ) + I.drop_container.switch(); + + } + + checkForbiddenCars() :boolean { + + // @ts-ignore + for( let char: string of FORBIDDEN_CARS ){ + if( this.file.name.includes(char) ) + return true; + } + return false; + + } + + private switchDuringUpload(): void { + + // @ts-ignore + during_upload = !during_upload; + this.switchSubmissionButton(); + + } + + private switchSubmissionButton(): void { + + let btn: HTMLElement|null = document.getElementById( 'submitTranslationButton' ); + if( btn !== null ){ + let conv_btn = btn as HTMLButtonElement; + if( conv_btn.disabled ){ + conv_btn.disabled = false; + conv_btn.innerText = __( 'Submit Entry', 'romhackplaza' ); + } else { + conv_btn.disabled = true; + conv_btn.innerText = __( 'Wait for upload...', 'romhackplaza' ); + } + } + + } +} \ No newline at end of file diff --git a/ts/submissions/globals.ts b/ts/submissions/globals.ts new file mode 100644 index 0000000..e348c80 --- /dev/null +++ b/ts/submissions/globals.ts @@ -0,0 +1,19 @@ +export declare const _romhackplaza_script_uploader: any; +export declare const romhackplaza_modal_submissions: any; +declare const wp: any; +export const { __, _x, _n, _nx } = wp.i18n; + + +export const FORBIDDEN_CARS: Array = [ + "&", "+" +]; + +export let during_upload = false; +export const I: any = { + upload: undefined, + drop_container: undefined, +} + +export interface Can_Upload_Detail { + file: File; +} \ No newline at end of file diff --git a/ts/submissions/index.ts b/ts/submissions/index.ts new file mode 100644 index 0000000..1b466d8 --- /dev/null +++ b/ts/submissions/index.ts @@ -0,0 +1,20 @@ +import {type Can_Upload_Detail, during_upload, I} from "./globals"; +import {Upload} from "./class-upload"; +import {DropContainer} from "./class-drop-container"; + +document.addEventListener('DOMContentLoaded', () => { + + if( !document.getElementById( 'fileInput' ) ) // Check if exists. + + // @ts-ignore + I.drop_container = new DropContainer( document.getElementById( 'file-container' ) as HTMLElement, document.getElementById( 'file-container-text' ) as HTMLElement ); + + // @ts-ignore + document.addEventListener( 'can_upload', (e: CustomEvent ) => { + + const { file } = e.detail; + if( !during_upload ) // @ts-ignore + I.upload = new Upload( file ); + + }); +}) \ No newline at end of file