| 1 |
<?php |
| 2 |
/** |
| 3 |
* The admin-facing custom post type functionality of the plugin. |
| 4 |
* |
| 5 |
* @link http://ays-pro.com/ |
| 6 |
* @since 5.7.7 |
| 7 |
* |
| 8 |
* @package Poll_Maker_Ays |
| 9 |
* @subpackage Poll_Maker_Ays/includes |
| 10 |
*/ |
| 11 |
|
| 12 |
/** |
| 13 |
* The admin-facing custom post type functionality of the plugin. |
| 14 |
* |
| 15 |
* Defines the plugin name, version, flush version, name prefix |
| 16 |
* |
| 17 |
* @package Poll_Maker_Ays |
| 18 |
* @subpackage Poll_Maker_Ays/includes |
| 19 |
* @author Poll Maker Team <info@ays-pro.com> |
| 20 |
*/ |
| 21 |
class Poll_Maker_Custom_Post_Type { |
| 22 |
|
| 23 |
private $plugin_name; |
| 24 |
private $version; |
| 25 |
private $ays_poll_flush_version; |
| 26 |
public $name_prefix; |
| 27 |
|
| 28 |
public function __construct($plugin_name, $version){ |
| 29 |
$this->plugin_name = $plugin_name; |
| 30 |
$this->name_prefix = 'ays-'; |
| 31 |
$this->version = $version; |
| 32 |
$this->ays_poll_flush_version = '1.0.0'; |
| 33 |
add_action( 'init', array( $this, 'ays_poll_register_custom_post_type' ) ); |
| 34 |
} |
| 35 |
|
| 36 |
public function ays_poll_register_custom_post_type(){ |
| 37 |
$args = array( |
| 38 |
'public' => true, |
| 39 |
'rewrite' => true, |
| 40 |
'show_in_menu' => false, |
| 41 |
'exclude_from_search' => false, |
| 42 |
'show_ui' => false, |
| 43 |
'show_in_nav_menus' => false, |
| 44 |
'show_in_rest' => false |
| 45 |
); |
| 46 |
|
| 47 |
register_post_type( 'ays-poll-maker', $args ); |
| 48 |
$this->ays_poll_custom_rewrite_rule(); |
| 49 |
$this->ays_poll_flush_permalinks(); |
| 50 |
} |
| 51 |
|
| 52 |
public static function ays_poll_add_custom_post($args, $update = true){ |
| 53 |
|
| 54 |
$poll_id = (isset($args['poll_id']) && $args['poll_id'] != '' && $args['poll_id'] != 0) ? esc_attr($args['poll_id']) : ''; |
| 55 |
$poll_title = (isset($args['poll_title']) && $args['poll_title'] != '') ? esc_attr($args['poll_title']) : ''; |
| 56 |
$author_id = (isset($args['author_id']) && $args['author_id'] != '') ? esc_attr($args['author_id']) : get_current_user_id(); |
| 57 |
|
| 58 |
$post_content = '[ays_poll id="'.$poll_id.'"]'; |
| 59 |
|
| 60 |
$new_post = array( |
| 61 |
'post_title' => $poll_title, |
| 62 |
'post_author' => $author_id, |
| 63 |
'post_type' => 'ays-poll-maker', // Custom post type name is -> ays-poll-maker |
| 64 |
'post_content' => $post_content, |
| 65 |
'post_status' => 'draft', |
| 66 |
'post_date' => current_time( 'mysql' ), |
| 67 |
); |
| 68 |
$post_id = wp_insert_post($new_post); |
| 69 |
if($update){ |
| 70 |
if(isset($post_id) && $post_id > 0){ |
| 71 |
self::update_polls_table_custom_post_id($post_id, $poll_id); |
| 72 |
} |
| 73 |
} |
| 74 |
return $post_id; |
| 75 |
} |
| 76 |
|
| 77 |
public static function update_polls_table_custom_post_id($custom_post_id, $poll_id){ |
| 78 |
global $wpdb; |
| 79 |
$table = esc_sql( $wpdb->prefix . "ayspoll_polls" ); |
| 80 |
$result = $wpdb->update(// phpcs:ignore WordPress.DB.DirectDatabaseQuery |
| 81 |
$table, |
| 82 |
array('custom_post_id' => $custom_post_id), |
| 83 |
array('id' => $poll_id), |
| 84 |
array('%d'), |
| 85 |
array('%d') |
| 86 |
); |
| 87 |
} |
| 88 |
|
| 89 |
public function ays_poll_flush_permalinks(){ |
| 90 |
if ( get_site_option( 'ays_poll_flush_version' ) != $this->ays_poll_flush_version ) { |
| 91 |
flush_rewrite_rules(); |
| 92 |
} |
| 93 |
update_option( 'ays_poll_flush_version', $this->ays_poll_flush_version ); |
| 94 |
} |
| 95 |
|
| 96 |
public function ays_poll_custom_rewrite_rule() { |
| 97 |
add_rewrite_rule( |
| 98 |
'ays-poll-maker/([^/]+)/?', |
| 99 |
'index.php?post_type=ays-poll-maker&name=$matches[1]', |
| 100 |
'top' |
| 101 |
); |
| 102 |
} |
| 103 |
} |
| 104 |
|