Making WordPress.org

Changeset 1655


Ignore:
Timestamp:
06/10/2015 09:58:13 PM (10 years ago)
Author:
coffee2code
Message:

jobs.wordpress.net: Implement mechanism for job posters to remove their own jobs.

  • Generate unique token for each job, given to job poster after submission.
  • Add template for remove-a-job page for submitting job removal requests (including form hook, e.g. for adding captcha)
  • Handle job removal requests
  • Email job poster after submission, to include the job token
File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/jobs.wordpress.net/public_html/wp-content/plugins/jobswp/jobswp.php

    r1654 r1655  
    119119            add_filter( $filter,                      array( $this, 'WordPress_dangit' ) );
    120120
     121        add_action( 'save_post_job',                  array( $this, 'email_job_poster' ), 10, 3 );
     122        add_action( 'wp',                             array( $this, 'maybe_remove_job' ) );
    121123        $this->save_job();
    122124        $this->schedule_job_pruning();
     
    519521
    520522    /**
     523     * Generates a random token.
     524     *
     525     * Incorporates the job id to further ensure uniqueness and to facilitate
     526     * later use.
     527     *
     528     * @param int     $job_id The job post ID.
     529     * @return string The token.
     530     */
     531    protected function generate_job_token( $job_id ) {
     532        return $job_id . '|' . bin2hex( openssl_random_pseudo_bytes( 20 ) );
     533    }
     534
     535    /**
     536     * Gets the published job with the given token.
     537     *
     538     * @param string         $token  The token.
     539     * @return WP_Post|false The job, or false if no matching job found.
     540     */
     541    public function get_job_by_token( $token ) {
     542        $job = false;
     543
     544        $parts = explode( '|', trim( $token ), 2 );
     545
     546        if ( count( $parts ) > 1 ) {
     547            list( $job_id, $job_token ) = $parts;
     548
     549            $stored_job_token = get_post_meta( (int) $job_id, 'job_token', true );
     550
     551            if ( $token === $stored_job_token ) {
     552                $job = get_post( (int) $job_id );
     553            }
     554        }
     555
     556        return $job;
     557    }
     558
     559    /**
     560     * Handles front-end submission of a job removal request.
     561     */
     562    public function maybe_remove_job() {
     563        if ( isset( $_POST['removejob'] ) && 1 == $_POST['removejob'] ) {
     564            check_admin_referer( 'jobswpremovejob' );
     565            $has_errors    = false;
     566            $this->success = false;
     567
     568            // Verify job token is provided.
     569            if ( ! isset( $_POST['job_token'] ) || empty( $_POST['job_token'] ) ) {
     570                $has_errors = true;
     571            }
     572
     573            $has_errors = apply_filters( 'jobswp_remove_job_errors', $has_errors );
     574
     575            // Only query for job if no errors thus far.
     576            if ( ! $has_errors ) {
     577                $job = $this->get_job_by_token( $_POST['job_token'] );
     578                if ( ! $job ) {
     579                    $has_errors = __( 'The provided job token does not match an open or pending job posting.', 'jobswp' );
     580                }
     581            }
     582
     583            if ( $has_errors ) {
     584                $_POST['errors'] = $has_errors;
     585            } else {
     586                $this->success = true;
     587            }
     588
     589            // If everything checks out, try to remove the job.
     590            if ( $this->success ) {
     591                $updated = $this->close_job( $job );
     592
     593                if ( is_wp_error( $updated ) || ! $updated ) {
     594                    $this->success = false;
     595                    $_POST['errors'] = __( 'Unable to remove job. Please try again or contact us for assistance.', 'jobswp' );
     596                } else {
     597                    wp_safe_redirect( '/remove-a-job/?removedjob=1' );
     598                }
     599            }
     600
     601        }
     602    }
     603
     604    /**
     605     * Emails the job poster after submission of their job posting.
     606     *
     607     * @param int     $post_ID Post ID.
     608     * @param WP_Post $post    Post object.
     609     * @param bool    $update  Whether this is an existing post being updated or not.
     610    */
     611    public function email_job_poster( $post_id, $post, $update ) {
     612        if ( ! $update ) {
     613            $to      = get_post_meta( $post_id, 'email', true );
     614            $title   = get_the_title( $post );
     615            $subject = sprintf( 'Job submitted: %s', $title );
     616            $token   = get_post_meta( $post_id, 'job_token', true );
     617            $body    = <<<EMAIL
     618Hi,
     619
     620Your job "%1\$s" has been successfully submitted to %2\$s. Please be patient as it may take our team of volunteer moderators 24-48 hours to review and publish it to the site.
     621
     622Take note of this special job token: %3\$s
     623
     624Your job will automatically be removed from the site after 21 days. If you wish to remove the job sooner than that, you can do so by using the job removal form at %4\$s and providing the job token provided above.
     625
     626Cheers.
     627
     628- The jobs.wordpress.net team.
     629
     630EMAIL;
     631
     632            $headers = '';
     633            $headers['From'] = 'jobs.wordpress.net <jobs@wordpress.net>';
     634
     635            $body = sprintf( $body, $title, 'http://jobs.wordpress.net/', $token, 'http://jobs.wordpress.net/remove-a-job/' );
     636
     637            if ( $to ) {
     638                wp_mail( $to, $subject, $body, $headers );
     639            }
     640        }
     641
     642        return $post_id;
     643    }
     644
     645    /**
    521646     * Saves a job posting submission, which is coming from the front-end by an
    522647     * unverified visitor.
     
    557682            if ( $this->success ) {
    558683                $job_id = $this->create_job();
     684
     685                // Generate and store a unique token for the job, primarily to be used by
     686                // job posters to close their jobs themselves despite the site's lack of
     687                // users.
     688                $_POST['job_token'] = $this->generate_job_token( $job_id );
     689                add_post_meta( $job_id, 'job_token', $_POST['job_token'], true );
     690
    559691                if ( is_wp_error( $job_id ) ) {
    560692                    $_POST['errors'] = $job_id->get_error_message();
Note: See TracChangeset for help on using the changeset viewer.