diff --git a/composer.json b/composer.json index 6e3d6de..af61bc3 100755 --- a/composer.json +++ b/composer.json @@ -1,4 +1,12 @@ { + "name": "romhackplaza/wp-plugin", + "type": "project", + "authors": [ + { + "name": "Benjamin", + "email": "benjamin@cartememoire.org" + } + ], "autoload": { "psr-4": { "RomhackPlaza\\": "src/" diff --git a/src/Extenders/Submissions.php b/src/Extenders/Submissions.php index 8b27eae..fb45544 100644 --- a/src/Extenders/Submissions.php +++ b/src/Extenders/Submissions.php @@ -111,11 +111,15 @@ class Submissions extends Abstract_Extender { 'submissions-uploader', ROMHACKPLAZA_PLUGIN_URI . '/assets/js/submissions/uploader.js', [], - '20251106-68', + '20251106-77', args: [ 'defer' => true, 'in_footer' => true ] )->enqueue()->add_localize( '_romhackplaza_script_uploader', - [ 'submit_url' => admin_url( 'admin-ajax.php' ) ], + [ + 'submit_url' => admin_url( 'admin-ajax.php' ), + 'reserve_nonce' => wp_create_nonce( Format::format_nonce_name( 'reserve_post_id' ) ), + 'set_favorite_nonce' => wp_create_nonce( Format::format_nonce_name( 'set_favorite_server' ) ), + ], )->enable_i18n(); } diff --git a/ts/submissions/class-reserve-post-id.ts b/ts/submissions/class-reserve-post-id.ts index bb2e4ed..4c5fd2d 100644 --- a/ts/submissions/class-reserve-post-id.ts +++ b/ts/submissions/class-reserve-post-id.ts @@ -14,5 +14,41 @@ export class Reserve_Post_ID { callback(); return; } + + if( I.custom_post_type === undefined || I.custom_post_type === "unknown" ){ + console.error( "The custom post type is missing" ); + return; + } + + this.do_request( callback ); + + } + + private do_request( callback: () => any ){ + + jQuery.ajax({ + type: "POST", + url: _romhackplaza_script_uploader.submit_url, + data: { + action: "reserve_post_id", + custom_post_type: I.custom_post_type, + _wpnonce: _romhackplaza_script_uploader.reserve_nonce + }, + success: function( response: any ) { + if( response.success ) { + + I.reserved_post_id = response.data.post_id; + jQuery( "#reservedPostID" ).val( I.reserved_post_id ); + callback(); + + } else { + romhackplaza_manage_modal( romhackplaza_modal_submissions, "block", __( "Submit error", 'romhackplaza' ), __( "Could not reserve a post ID. Please refresh the page.", "romhackplaza" ), "" ); + } + }, + error: function( response: any ) { + romhackplaza_manage_modal( romhackplaza_modal_submissions, "block", __( "Submit error", 'romhackplaza' ), __( "Error occurred while reserving a post ID. Please refresh the page.", "romhackplaza" ), "" ); + } + }); + } } \ No newline at end of file diff --git a/ts/submissions/class-server-api.ts b/ts/submissions/class-server-api.ts new file mode 100644 index 0000000..77d6484 --- /dev/null +++ b/ts/submissions/class-server-api.ts @@ -0,0 +1,50 @@ +import { __, I } from "./globals"; + +export class Server_Api { + + url: string|undefined; + id: number|undefined; + + init( server_url: string, server_id: number ){ + this.url = server_url; + this.id = server_id; + } + + is_init(): boolean { + return this.url !== undefined && this.id !== undefined; + } + + private build_url( call: string ): string { + + let new_url: string = this.url + "upload.php"; + if( call === "check_existence" ) + new_url += "?checkExistence=true"; + + return new_url; + } + + async check_file_existence( filename: string ): Promise { + + return new Promise( ( resolve, reject ) => { + + const XHR: XMLHttpRequest = new XMLHttpRequest(); + XHR.open( 'POST', this.build_url( 'check_existence' ), true ); + + let form_data: FormData = new FormData(); + form_data.append( "filename", filename ); + form_data.append( 'reservedPostID', I.reserved_post_id ); + form_data.append( 'custom_post_type', I.custom_post_type ); + + XHR.onload = () => { + resolve( XHR.responseText === 'exists' ); + } + XHR.onerror = () => { + reject( false ); + } + + XHR.send( form_data ); + + }); + + } +} \ No newline at end of file diff --git a/ts/submissions/class-set-favorite-server.ts b/ts/submissions/class-set-favorite-server.ts new file mode 100644 index 0000000..ec33606 --- /dev/null +++ b/ts/submissions/class-set-favorite-server.ts @@ -0,0 +1,34 @@ +import {__, I } from "./globals"; +declare const _romhackplaza_script_uploader: any; +declare const romhackplaza_modal_submissions: any; + +export class Set_Favorite_Server { + + /** + * + * @param callback - MUST BE FROM A Upload child. + */ + constructor( server_id: number ){ + + this.do_request( server_id ); + + } + + private do_request( server_id : number ){ + + jQuery.ajax({ + type: "POST", + url: _romhackplaza_script_uploader.submit_url, + data: { + action: 'set_favorite_server', + post_id: I.reserved_post_id, + favorite_server: server_id, + _wpnonce: _romhackplaza_script_uploader.set_favorite_nonce + }, + success: function(){ + console.log( "Favorite server set" ); + } + }); + + } +} \ No newline at end of file diff --git a/ts/submissions/class-upload.ts b/ts/submissions/class-upload.ts index dffbd78..c7c9312 100644 --- a/ts/submissions/class-upload.ts +++ b/ts/submissions/class-upload.ts @@ -1,5 +1,7 @@ -import {__, FORBIDDEN_CARS, I } from "./globals"; +import {__, FORBIDDEN_CARS, I, API } from "./globals"; import {DropContainer} from "./class-drop-container"; +import {Reserve_Post_ID} from "./class-reserve-post-id"; +import {Set_Favorite_Server} from "./class-set-favorite-server"; declare const _romhackplaza_script_uploader: any; declare const romhackplaza_modal_submissions: any; @@ -27,6 +29,7 @@ export class Upload { } this.switchDuringUpload(); + // @ts-ignore if( typeof I.drop_container !== 'undefined' && I.drop_container instanceof DropContainer ) I.drop_container.switch(); @@ -36,8 +39,56 @@ export class Upload { this.progress_bar = progress.querySelector('.bar') as HTMLElement; } - if( I.reserved_post_id === undefined || I.reserved_post_id === null ) - console.log( "ok" ); + if( I.reserved_post_id === undefined || I.reserved_post_id === null ){ + // Reserve a post ID. + new Reserve_Post_ID( this.step1_checkFileExists ); + } else + // Already reserved. + this.step1_checkFileExists(); + + } + + step1_checkFileExists = () => { + + if( I.favorite_server_set === false ){ + // @ts-ignore + new Set_Favorite_Server( chosenDownloadServerId || 0 ); + I.favorite_server_set = true; + } + + API.check_file_existence( this.file.name ).then( exists => { + + if( !exists ){ + this.step3_uploadFile(); + // @ts-ignore + document.getElementById( 'cancelButton' )?.style.display = 'block'; + } else { + + let want_to_delete = window.confirm( __( "File already exists! Proceed with upload and overwrite it ?" ) ); + if( want_to_delete ) + this.step2_deleteFileBeforeUpload(); + + else{ + + // @ts-ignore + if( typeof I.drop_container !== 'undefined' && I.drop_container instanceof DropContainer ) + I.drop_container.switch(); + this.switchDuringUpload(); + + } + + } + + } ); + + } + + step2_deleteFileBeforeUpload = () => { + + + } + + step3_uploadFile = () => { } diff --git a/ts/submissions/class-uploader-data.ts b/ts/submissions/class-uploader-data.ts index bff8f57..de6023c 100644 --- a/ts/submissions/class-uploader-data.ts +++ b/ts/submissions/class-uploader-data.ts @@ -1,21 +1,31 @@ export class Uploader_Data { + [x: string]: any // @ts-ignore + ; + // @ts-ignore private data: object; constructor() { this.data = {}; return new Proxy( this, { - get: ( target, p ) => { + get: ( target, p, receiver ) => { + if (p in target) { + return Reflect.get(target, p, receiver); + } return target.get( p as string ); }, - set: ( target, p, v ) => { + set: ( target, p, v , receiver) => { + if (p in target) { + return Reflect.set(target, p, v, receiver); + } target.set( p as string, v ); return true } }); } + // @ts-ignore add_field( field_name: string, field_value: any, readonly: boolean ){ // @ts-ignore @@ -25,6 +35,7 @@ export class Uploader_Data { } + // @ts-ignore get( field_name : string ): any { // @ts-ignore @@ -36,6 +47,7 @@ export class Uploader_Data { } + // @ts-ignore set( field_name: string, field_value: any ){ // @ts-ignore diff --git a/ts/submissions/globals.ts b/ts/submissions/globals.ts index 0bba185..05e9471 100644 --- a/ts/submissions/globals.ts +++ b/ts/submissions/globals.ts @@ -1,4 +1,5 @@ import {Uploader_Data} from "./class-uploader-data"; +import {Server_Api} from "./class-server-api"; export const { __, _x, _n, _nx } = wp.i18n; @@ -8,10 +9,14 @@ export const FORBIDDEN_CARS: Array = [ export const I : Uploader_Data = new Uploader_Data(); I.add_field( 'is_edit', false, true ); +I.add_field( 'custom_post_type', undefined, true ); I.add_field( 'reserved_post_id', undefined, false ); I.add_field( 'during_upload', false, false ); I.add_field( 'upload', undefined, false ); I.add_field( 'drop_container', undefined, true ); +I.add_field( 'favorite_server_set', false, false ); + +export const API : Server_Api = new Server_Api(); export interface Can_Upload_Detail { file: File; diff --git a/ts/submissions/index.ts b/ts/submissions/index.ts index 8fa9d43..4a76624 100644 --- a/ts/submissions/index.ts +++ b/ts/submissions/index.ts @@ -9,7 +9,10 @@ document.addEventListener('DOMContentLoaded', () => { // Add PostID if edit. let url_params = new URLSearchParams(window.location.search); + // @ts-ignore I.is_edit = url_params.has( 'edit_entry' ); + + I.custom_post_type = jQuery( 'input[name="custom_post_type"]').val() || 'unknown'; I.reserved_post_id = url_params.get('edit_entry' ) || null; // @ts-ignore