← All changes
|
includes/class-survey-maker-custom-post-type.php
+92
-0
5.2.1.9
→
5.2.4.1
View file →
| @@ -11,8 +11,9 @@ | ||
| 11 | 11 | $this->name_prefix = 'ays-'; |
| 12 | 12 | $this->version = $version; |
| 13 | 13 | $this->survey_flush_version = '1.0.0'; |
| 14 | 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 ); | |
| 15 | 16 | } |
| 16 | 17 | |
| 17 | 18 | public function survey_register_custom_post_type(){ |
| 18 | 19 | $args = array( |
| @@ -79,6 +80,97 @@ | ||
| 79 | 80 | 'ays-survey-maker/([^/]+)/?', |
| 80 | 81 | 'index.php?post_type=ays-survey-maker&name=$matches[1]', |
| 81 | 82 | 'top' |
| 82 | 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 ''; | |
| 83 | 175 | } |
| 84 | 176 | } |