| 1 |
<?php |
| 2 |
class Survey_Maker_Custom_Post_Type { |
| 3 |
|
| 4 |
private $plugin_name; |
| 5 |
private $version; |
| 6 |
private $survey_flush_version; |
| 7 |
public $name_prefix; |
| 8 |
|
| 9 |
public function __construct($plugin_name, $version){ |
| 10 |
$this->plugin_name = $plugin_name; |
| 11 |
$this->name_prefix = 'ays-'; |
| 12 |
$this->version = $version; |
| 13 |
$this->survey_flush_version = '1.0.0'; |
| 14 |
add_action( 'init', array( $this, 'survey_register_custom_post_type' ) ); |
| 15 |
add_filter( 'the_content', array( $this, 'ays_survey_add_preview_notice_to_content' ), 9 ); |
| 16 |
} |
| 17 |
|
| 18 |
public function survey_register_custom_post_type(){ |
| 19 |
$args = array( |
| 20 |
'public' => true, |
| 21 |
'rewrite' => true, |
| 22 |
'show_in_menu' => false, |
| 23 |
'exclude_from_search' => false, |
| 24 |
'show_ui' => false, |
| 25 |
'show_in_nav_menus' => false, |
| 26 |
'show_in_rest' => false |
| 27 |
); |
| 28 |
|
| 29 |
register_post_type( $this->name_prefix . $this->plugin_name, $args ); |
| 30 |
$this->custom_survey_rewrite_rule(); |
| 31 |
$this->survey_flush_permalinks(); |
| 32 |
} |
| 33 |
|
| 34 |
public static function survey_add_custom_post($args, $update = true){ |
| 35 |
|
| 36 |
$survey_id = isset($args['survey_id']) && $args['survey_id'] != '' && $args['survey_id'] != 0 ? esc_attr($args['survey_id']) : ''; |
| 37 |
$survey_title = isset($args['survey_title']) && $args['survey_title'] != '' ? esc_attr($args['survey_title']) : ''; |
| 38 |
$author_id = isset($args['author_id']) && $args['author_id'] != '' ? esc_attr($args['author_id']) : get_current_user_id(); |
| 39 |
|
| 40 |
$post_content = '[ays_survey id="'.$survey_id.'"]'; |
| 41 |
|
| 42 |
$new_post = array( |
| 43 |
'post_title' => $survey_title, |
| 44 |
'post_author' => $author_id, |
| 45 |
'post_type' => 'ays-survey-maker', // Custom post type name is -> ays-survey-maker |
| 46 |
'post_content' => $post_content, |
| 47 |
'post_status' => 'draft', |
| 48 |
'post_date' => current_time( 'mysql' ), |
| 49 |
); |
| 50 |
$post_id = wp_insert_post($new_post); |
| 51 |
if($update){ |
| 52 |
if(isset($post_id) && $post_id > 0){ |
| 53 |
self::update_surveys_table_custom_post_id($post_id, $survey_id); |
| 54 |
} |
| 55 |
} |
| 56 |
return $post_id; |
| 57 |
} |
| 58 |
|
| 59 |
public static function update_surveys_table_custom_post_id($custom_post_id, $survey_id){ |
| 60 |
global $wpdb; |
| 61 |
$table = esc_sql( $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "surveys" ); |
| 62 |
$result = $wpdb->update(// phpcs:ignore WordPress.DB.DirectDatabaseQuery |
| 63 |
$table, |
| 64 |
array('custom_post_id' => $custom_post_id), |
| 65 |
array( 'id' => $survey_id ), |
| 66 |
array('%d'), |
| 67 |
array('%d') |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
public function survey_flush_permalinks(){ |
| 72 |
if ( get_site_option( 'survey_flush_version' ) != $this->survey_flush_version ) { |
| 73 |
flush_rewrite_rules(); |
| 74 |
} |
| 75 |
update_option( 'survey_flush_version', $this->survey_flush_version ); |
| 76 |
} |
| 77 |
|
| 78 |
public function custom_survey_rewrite_rule() { |
| 79 |
add_rewrite_rule( |
| 80 |
'ays-survey-maker/([^/]+)/?', |
| 81 |
'index.php?post_type=ays-survey-maker&name=$matches[1]', |
| 82 |
'top' |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
public function ays_survey_add_preview_notice_to_content( $content ) { |
| 87 |
global $post; |
| 88 |
|
| 89 |
if ( ! is_singular( $this->name_prefix . $this->plugin_name ) || ! is_main_query() || ! in_the_loop() ) { |
| 90 |
return $content; |
| 91 |
} |
| 92 |
|
| 93 |
$is_preview = get_query_var( 'preview' ); |
| 94 |
|
| 95 |
if ( $is_preview !== 'true' && $is_preview !== true ) { |
| 96 |
return $content; |
| 97 |
} |
| 98 |
|
| 99 |
$post_type = isset( $post->post_type ) && $post->post_type != "" ? sanitize_text_field($post->post_type) : ''; |
| 100 |
|
| 101 |
if( $post_type !== 'ays-survey-maker' ){ |
| 102 |
return $content; |
| 103 |
} |
| 104 |
|
| 105 |
return $this->ays_survey_get_preview_notice_html() . $content; |
| 106 |
} |
| 107 |
|
| 108 |
private function ays_survey_get_preview_notice_html() { |
| 109 |
global $post; |
| 110 |
|
| 111 |
$post_id = isset( $post->ID ) ? absint( $post->ID ) : 0; |
| 112 |
$shortcode = isset( $post->post_content ) ? $this->ays_survey_get_shortcode_from_content( $post->post_content ) : ''; |
| 113 |
|
| 114 |
if ( $shortcode === '' && $post_id > 0 ) { |
| 115 |
$shortcode = $this->ays_survey_get_shortcode_by_custom_post_id( $post_id ); |
| 116 |
} |
| 117 |
|
| 118 |
$content = array(); |
| 119 |
|
| 120 |
$content[] = '<div id="ays-survey-preview-notice-main-container" class="ays-survey-preview-notice-wrap">'; |
| 121 |
$content[] = '<div role="status" aria-live="polite" class="ays-survey-preview-notice">'; |
| 122 |
$content[] = '<span class="ays-survey-preview-notice-icon" aria-hidden="true">'; |
| 123 |
$content[] = '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">'; |
| 124 |
$content[] = '<path d="M2.062 12.348a1 1 0 0 1 0-.696 10.75 10.75 0 0 1 19.876 0 1 1 0 0 1 0 .696 10.75 10.75 0 0 1-19.876 0"></path>'; |
| 125 |
$content[] = '<circle cx="12" cy="12" r="3"></circle>'; |
| 126 |
$content[] = '</svg>'; |
| 127 |
$content[] = '</span>'; |
| 128 |
$content[] = '<div class="ays-survey-preview-notice-text">'; |
| 129 |
$content[] = '<strong>' . esc_html__( 'This is a preview page.', 'survey-maker' ) . '</strong> '; |
| 130 |
$content[] = esc_html__( 'Other users cannot access this link. To publish this survey, copy its shortcode and add it to a post or page.', 'survey-maker' ); |
| 131 |
$content[] = '</div>'; |
| 132 |
|
| 133 |
if ( $shortcode !== '' ) { |
| 134 |
$content[] = '<div class="ays-survey-preview-shortcode-actions">'; |
| 135 |
$content[] = '<button type="button" class="ays-survey-preview-copy-shortcode" data-shortcode="' . esc_attr( $shortcode ) . '" data-label="' . esc_attr__( 'Copy Shortcode', 'survey-maker' ) . '" data-copied-label="' . esc_attr__( 'Copied', 'survey-maker' ) . '">'; |
| 136 |
$content[] = '<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">'; |
| 137 |
$content[] = '<rect width="14" height="14" x="8" y="8" rx="2" ry="2"></rect>'; |
| 138 |
$content[] = '<path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"></path>'; |
| 139 |
$content[] = '</svg>'; |
| 140 |
$content[] = '<span class="ays-survey-preview-copy-shortcode-label">' . esc_html__( 'Copy Shortcode', 'survey-maker' ) . '</span>'; |
| 141 |
$content[] = '</button>'; |
| 142 |
$content[] = '<code class="ays-survey-preview-shortcode-text">[' . esc_html( $shortcode ) . ']</code>'; |
| 143 |
$content[] = '</div>'; |
| 144 |
} |
| 145 |
$content[] = '</div>'; |
| 146 |
$content[] = '</div>'; |
| 147 |
|
| 148 |
return implode( '', $content ); |
| 149 |
} |
| 150 |
|
| 151 |
private function ays_survey_get_shortcode_from_content( $content ) { |
| 152 |
if ( preg_match( '/\[ays_survey[^\]]*id\s*=\s*([\'"]?)(\d+)\1[^\]]*\]/', $content, $matches ) ) { |
| 153 |
return "ays_survey id='" . absint( $matches[2] ) . "'"; |
| 154 |
} |
| 155 |
|
| 156 |
return ''; |
| 157 |
} |
| 158 |
|
| 159 |
private function ays_survey_get_shortcode_by_custom_post_id( $custom_post_id ) { |
| 160 |
global $wpdb; |
| 161 |
|
| 162 |
$table = esc_sql( $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . 'surveys' ); |
| 163 |
$survey_id = $wpdb->get_var(// phpcs:ignore WordPress.DB.DirectDatabaseQuery |
| 164 |
$wpdb->prepare( |
| 165 |
"SELECT id FROM {$table} WHERE custom_post_id = %d", |
| 166 |
$custom_post_id |
| 167 |
) |
| 168 |
); |
| 169 |
|
| 170 |
if ( $survey_id > 0 ) { |
| 171 |
return "ays_survey id='" . absint( $survey_id ) . "'"; |
| 172 |
} |
| 173 |
|
| 174 |
return ''; |
| 175 |
} |
| 176 |
} |
| 177 |
|