Skip to content

Commit

Permalink
Add survey page framework.
Browse files Browse the repository at this point in the history
  • Loading branch information
renintw committed Sep 6, 2023
1 parent 3bbf0e3 commit dbe81e5
Showing 1 changed file with 133 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php
/**
* Adds a page which contains the survey.
*/

namespace WordCamp\OrganizerSurvey\Page;

defined( 'WPINC' ) || die();

use function WordCamp\OrganizerSurvey\{get_survey_page_id};

add_filter( 'pre_trash_post', __NAMESPACE__ . '\prevent_deletion', 10, 2 );
add_filter( 'pre_delete_post', __NAMESPACE__ . '\prevent_deletion', 10, 3 );

/**
* Constants.
*/
const SURVEY_PAGE_ID = 'organizer_survey_page';


/**
* Return the page ID.
*
* @return mixed
*/
function get_page_id() {
return get_option( SURVEY_PAGE_ID );
}


/**
* Prevent deletion of the Survey page, unless force_delete is true.
*
* @param bool|null $check Whether to go forward with trashing/deletion.
* @param WP_Post $post Post object.
* @param bool $force_delete Whether to bypass the trash, set when deactivating the plugin to clean up.
*/
function prevent_deletion( $check, $post, $force_delete = false ) {
$survey_page = (int) get_option( SURVEY_PAGE_ID );

if ( $survey_page === $post->ID ) {
// Allow it, and delete the option if the page is force-deleted.
if ( $force_delete ) {
delete_option( SURVEY_PAGE_ID );
return $check;
}

return false;
}

return $check;
}

/**
* Get the content of the survey page.
*
* @return string
*/
function get_page_content() {
return <<<EOT
// TODO
EOT;
}

/**
* Turns on the page by changing its status to 'publish'.
*
* @return int|WP_Error
*/
function publish_survey_page() {
return wp_update_post( array(
'ID' => get_option( SURVEY_PAGE_ID ),
'post_status' => 'publish',
) );
}

/**
* Generate a link to the front end feedback UI for a particular session.
*
* @return string|bool
*/
function get_survey_page_url() {
return get_permalink( get_option( SURVEY_PAGE_ID ) );
}


/**
* Create the Survey page, save ID into an option.
*/
function add_page() {
$page_id = get_option( SURVEY_PAGE_ID );
if ( $page_id ) {
return;
}

$page_id = wp_insert_post( array(
'post_title' => __( 'Organizer Survey', 'wordcamporg' ),
/* translators: Page slug for the organizer survey. */
'post_name' => __( 'organizer survey', 'wordcamporg' ),
'post_content' => get_page_content(),
'post_status' => 'draft',
'post_type' => 'page',
) );

if ( $page_id > 0 ) {
update_option( SURVEY_PAGE_ID, $page_id );
}
}

/**
* Turns off the page by changing its status to 'draft'.
*/
function disable_page() {
$page_id = get_option( SURVEY_PAGE_ID );

if ( ! $page_id ) {
return;
}

return wp_update_post( array(
'ID' => $page_id,
'post_status' => 'draft',
) );
}

/**
* Delete the Survey page and associated meta data.
*/
function delete_page() {
$page_id = get_option( SURVEY_PAGE_ID );
wp_delete_post( $page_id, true );
delete_option( SURVEY_PAGE_ID );
}

0 comments on commit dbe81e5

Please sign in to comment.