PluginProbe
Survey Maker by AYS / 5.2.2.0
Survey Maker by AYS v5.2.2.0
5.2.4.1 5.2.4.0 5.2.3.9 5.2.3.8 5.2.3.7 5.2.3.6 5.2.3.5 5.2.3.4 5.2.3.3 5.2.3.2 5.2.3.1 5.2.3.0 5.2.2.9 5.2.2.8 5.2.2.7 5.2.2.6 5.2.2.5 5.2.2.4 5.2.2.3 5.2.2.2 5.2.2.1 5.2.2.0 5.2.1.9 5.2.1.8 5.2.0.8 All 154 releases
survey-maker / includes / class-survey-maker-custom-post-type.php

class-survey-maker-custom-post-type.php in Survey Maker by AYS 5.2.2.0, at includes/class-survey-maker-custom-post-type.php

85 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 }
16
17 public function survey_register_custom_post_type(){
18 $args = array(
19 'public' => true,
20 'rewrite' => true,
21 'show_in_menu' => false,
22 'exclude_from_search' => false,
23 'show_ui' => false,
24 'show_in_nav_menus' => false,
25 'show_in_rest' => false
26 );
27
28 register_post_type( $this->name_prefix . $this->plugin_name, $args );
29 $this->custom_survey_rewrite_rule();
30 $this->survey_flush_permalinks();
31 }
32
33 public static function survey_add_custom_post($args, $update = true){
34
35 $survey_id = isset($args['survey_id']) && $args['survey_id'] != '' && $args['survey_id'] != 0 ? esc_attr($args['survey_id']) : '';
36 $survey_title = isset($args['survey_title']) && $args['survey_title'] != '' ? esc_attr($args['survey_title']) : '';
37 $author_id = isset($args['author_id']) && $args['author_id'] != '' ? esc_attr($args['author_id']) : get_current_user_id();
38
39 $post_content = '[ays_survey id="'.$survey_id.'"]';
40
41 $new_post = array(
42 'post_title' => $survey_title,
43 'post_author' => $author_id,
44 'post_type' => 'ays-survey-maker', // Custom post type name is -> ays-survey-maker
45 'post_content' => $post_content,
46 'post_status' => 'draft',
47 'post_date' => current_time( 'mysql' ),
48 );
49 $post_id = wp_insert_post($new_post);
50 if($update){
51 if(isset($post_id) && $post_id > 0){
52 self::update_surveys_table_custom_post_id($post_id, $survey_id);
53 }
54 }
55 return $post_id;
56 }
57
58 public static function update_surveys_table_custom_post_id($custom_post_id, $survey_id){
59 global $wpdb;
60 $table = esc_sql( $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "surveys" );
61 $result = $wpdb->update(// phpcs:ignore WordPress.DB.DirectDatabaseQuery
62 $table,
63 array('custom_post_id' => $custom_post_id),
64 array( 'id' => $survey_id ),
65 array('%d'),
66 array('%d')
67 );
68 }
69
70 public function survey_flush_permalinks(){
71 if ( get_site_option( 'survey_flush_version' ) != $this->survey_flush_version ) {
72 flush_rewrite_rules();
73 }
74 update_option( 'survey_flush_version', $this->survey_flush_version );
75 }
76
77 public function custom_survey_rewrite_rule() {
78 add_rewrite_rule(
79 'ays-survey-maker/([^/]+)/?',
80 'index.php?post_type=ays-survey-maker&name=$matches[1]',
81 'top'
82 );
83 }
84 }
85