AnsPress_Uploader()

Description #

AnsPress upload hooks.

Source #

File: includes/upload.php

class AnsPress_Uploader {

	/**
	 * Delete question or answer attachment.
	 */
	public static function delete_attachment() {
		$attachment_id = ap_sanitize_unslash( 'attachment_id', 'r' );

		if ( ! ap_verify_nonce( 'delete-attachment-' . $attachment_id ) ) {
			ap_ajax_json( 'no_permission' );
		}

		// If user cannot delete then die.
		if ( ! ap_user_can_delete_attachment( $attachment_id ) ) {
			ap_ajax_json( 'no_permission' );
		}

		$attach = get_post( $attachment_id );
		$row    = wp_delete_attachment( $attachment_id, true );

		if ( false !== $row ) {
			ap_update_post_attach_ids( $attach->post_parent );
			ap_ajax_json( [ 'success' => true ] );
		}

		ap_ajax_json(
			[
				'success'  => false,
				'snackbar' => [ 'message' => __( 'Unable to delete attachment', 'anspress-question-answer' ) ],
			]
		);
	}

	/**
	 * Update users temporary attachment count before a attachment deleted.
	 *
	 * @param integer $post_id Post ID.
	 */
	public static function deleted_attachment( $post_id ) {
		$_post = get_post( $post_id );

		if ( 'attachment' === $_post->post_type ) {
			ap_update_user_temp_media_count();
			ap_update_post_attach_ids( $_post->post_parent );
		}
	}

	/**
	 * Schedule event twice daily.
	 */
	public static function create_single_schedule() {
		// Check if event scheduled before.
		if ( ! wp_next_scheduled( 'ap_delete_temp_attachments' ) ) {
			// Shedule event to run every day.
			wp_schedule_event( time(), 'twicedaily', 'ap_delete_temp_attachments' );
		}
	}

	/**
	 * Delete temporary media which are older then today.
	 *
	 * @since 4.1.8 Delete files from temporary directory as we well.
	 */
	public static function cron_delete_temp_attachments() {
		global $wpdb;

		$posts = $wpdb->get_results( "SELECT ID, post_author FROM $wpdb->posts WHERE post_type = 'attachment' AND post_title='_ap_temp_media' AND post_date >= CURDATE()" ); // db call okay, db cache okay.

		$authors = [];

		if ( $posts ) {
			foreach ( (array) $posts as $_post ) {
				wp_delete_attachment( $_post->ID, true );
				ap_update_post_attach_ids( $_post->post_parent );
				$authors[] = $_post->post_author;
			}

			// Update temporary attachment counts of a user.
			foreach ( (array) array_unique( $authors ) as $author ) {
				ap_update_user_temp_media_count( $author );
			}
		}

		// Delete all temporary files.
		$uploads  = wp_upload_dir();
		$files    = glob( $uploads['basedir'] . "/anspress-temp/*" );
		$interval = strtotime( '-2 hours' );

		if ( $files ) {
			foreach ( $files as $file ) {
				if ( filemtime( $file ) <= $interval ) {
					unlink( $file );
				}
			}
		}
	}

	/**
	 * Ajax callback for image upload form.
	 *
	 * @return void
	 * @since 4.1.8
	 */
	public static function upload_modal() {
		// Check nonce.
		if ( ! ap_verify_nonce( 'ap_upload_image' ) ) {
			ap_send_json( 'something_wrong' );
		}

		// Check if user have permission to upload tem image.
		if ( ! ap_user_can_upload() ) {
			ap_send_json( array(
				'success'  => false,
				'snackbar' => array(
					'message' => __( 'Sorry! you do not have permission to upload image.', 'anspress-question-answer' ),
				),
			) );
		}

		$image_for = ap_sanitize_unslash( 'image_for', 'r' );

		ob_start();
		anspress()->get_form( 'image_upload' )->generate( array(
			'hidden_fields' => array(
				array(
					'name'  => 'action',
					'value' => 'ap_image_upload',
				),
				array(
					'name'  => 'image_for',
					'value' => $image_for,
				),
			),
		));
		$html = ob_get_clean();

		ap_send_json( array(
			'success' => true,
			'action'  => 'ap_upload_modal',
			'html'    => $html,
			'title'   => __( 'Select image file to upload', 'anspress-question-answer' ),
		) );
	}

	/**
	 * Ajax callback for `ap_image_upload`. Process `image_upload` form.
	 *
	 * @return void
	 * @since 4.1.8
	 * @since 4.1.13 Pass a `image_for` in JSON so that javascript callback can be triggered.
	 */
	public static function image_upload() {
		$form = anspress()->get_form( 'image_upload' );

		// Check if user have permission to upload tem image.
		if ( ! ap_user_can_upload() ) {
			ap_send_json( array(
				'success'  => false,
				'snackbar' => array(
					'message' => __( 'Sorry! you do not have permission to upload image.', 'anspress-question-answer' ),
				),
			) );
		}

		// Nonce check.
		if ( ! $form->is_submitted() ) {
			ap_send_json( 'something_wrong' );
		}

		$image_for = ap_sanitize_unslash( 'image_for', 'r' );
		$values    = $form->get_values();

		// Check for errors.
		if ( $form->have_errors() ) {
			ap_send_json( array(
				'success'       => false,
				'snackbar'      => array(
					'message'      => __( 'Unable to upload image(s). Please check errors.', 'anspress-question-answer' ),
				),
				'form_errors'   => $form->errors,
				'fields_errors' => $form->get_fields_errors(),
			) );
		}

		$field = $form->find( 'image' );

		// Call save.
		$files = $field->save_cb();

		$res = array(
			'success'   => true,
			'action'    => 'ap_image_upload',
			'image_for' => $image_for,
			'snackbar'  => [ 'message' => __( 'Successfully uploaded image', 'anspress-question-answer' ) ],
			'files'     => $files,
		);

		// Send response.
		if ( is_array( $res ) ) {
			ap_send_json( $res );
		}

		ap_send_json( 'something_wrong' );
	}

	public static function image_sizes_advanced( $sizes ) {
		global $ap_thumbnail_only;

		if ( true === $ap_thumbnail_only ) {
			return array(
				'thumbnail' => array(
					'width'  => 150,
					'height' => 150,
					'crop'   => true,
				),
			);
		}

		return $sizes;
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment