Skip to content

Commit

Permalink
#2814 schedule deletes
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Pollock committed Nov 21, 2018
1 parent 0e4ea01 commit 4802c92
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 20 deletions.
23 changes: 20 additions & 3 deletions cf2/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,30 @@
/**
* @return \calderawp\calderaforms\cf2\CalderaFormsV2Contract
*/
function caldera_forms_get_v2_container(){
function caldera_forms_get_v2_container()
{

static $container;
if( ! $container ){
if ( !$container ) {
$container = new \calderawp\calderaforms\cf2\CalderaFormsV2();
do_action( 'caldera_forms_v2_init', $container );
do_action('caldera_forms_v2_init', $container);
}

return $container;
}

/**
* Schedule delete with job manager
*
* @since 1.8.0
*
* @param \calderawp\calderaforms\cf2\Jobs\Job $job Job to schedule
* @param int $delay Optional. Minimum delay before job is run. Default is 0.
*/
function caldera_forms_schedule_job(\calderawp\calderaforms\cf2\Jobs\Job $job, $delay = 0)
{

caldera_forms_get_v2_container()
->getService(\calderawp\calderaforms\cf2\Services\QueueSchedulerService::class)
->schedule($job, $delay);
}
38 changes: 23 additions & 15 deletions classes/files.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,34 @@ public static function upload( $file, array $args = array() ){
'private' => false,
'field_id' => null,
'form_id' => null,
'transient_id' => null,
));
if( true == $args[ 'private' ] && ! empty( $args[ 'field_id' ] ) && ! empty( $args[ 'form_id' ] )){
$private = true;
}else{
$private = false;
}

if( $private ){
wp_schedule_single_event( time() + HOUR_IN_SECONDS, self::CRON_ACTION, array(
$args[ 'field_id' ],
$args[ 'form_id' ]
) );
}

self::add_upload_filter( $args[ 'field_id' ], $args[ 'form_id' ], $private );
self::add_upload_filter( $args[ 'field_id' ], $args[ 'form_id' ], $private, $args[ 'transient_id' ] );

$upload = wp_handle_upload($file, array( 'test_form' => false, 'foo' => 'bnar' ) );

if( $private ){
self::remove_upload_filter();
}

return $upload;
if( $private ){
$form = Caldera_Forms_Forms::get_form($args[ 'form_id' ]);
if (is_array( $form ) ) {
$field = Caldera_Forms_Field_Util::get_field($args[ 'field_id' ], $form);
if (! self::is_persistent($field)) {
caldera_forms_schedule_job(new \calderawp\calderaforms\cf2\Jobs\DeleteFileJob($upload['file']));
}
}
}

return $upload;

}

Expand Down Expand Up @@ -113,10 +118,12 @@ public static function add_to_media_library( $upload, $field ){
*
* @param string $field_id The field ID for file field
* @param string $form_id The form ID
* @param bool $private If is private
* @param null|string $transient_id ID of transient for file. Optional.
*/
public static function add_upload_filter( $field_id , $form_id, $private = true ){
public static function add_upload_filter( $field_id , $form_id, $private = true, $transient_id = null ){
if ( $private ) {
self::$dir = self::secret_dir( $field_id, $form_id );
self::$dir = self::secret_dir( $field_id, $form_id, $transient_id);
}else{
/**
* Filter directory for uploaded files
Expand All @@ -128,7 +135,7 @@ public static function add_upload_filter( $field_id , $form_id, $private = true
* @param string|null Directory
* @param string $field_id Field ID
* @param string $form_id Form ID
*/
*/
$dir = apply_filters( 'caldera_forms_upload_directory', null, $field_id, $form_id );
if( null != $dir ){
self::$dir = $dir;
Expand Down Expand Up @@ -181,11 +188,12 @@ public static function uploads_filter( $args ){
*
* @param string $field_id The field ID for file field
* @param string $form_id The form ID
*
* @return string
* @param null|string $transient_id ID of transient for file. Optional.
*
* @return string
*/
protected static function secret_dir( $field_id, $form_id ){
return md5( $field_id . $form_id . NONCE_SALT );
protected static function secret_dir( $field_id, $form_id, $transient_id = null ){
return md5( $field_id . $form_id . NONCE_SALT . (string) $transient_id );

}

Expand Down
6 changes: 4 additions & 2 deletions tests/Util/Traits/TestsImages.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,10 @@ public function deleteTestCatFile()
if (file_exists($this->tmpPathTiny)) {
unlink($this->tmpPathTiny);
}
rmdir('/tmp/cats' );
}
if (file_exists('/tmp/cats')) {
rmdir('/tmp/cats');
}
}

/**
* Make directory for cats to be stored in
Expand Down

0 comments on commit 4802c92

Please sign in to comment.