| 1 |
<?php |
| 2 |
/** |
| 3 |
* The admin-facing custom post type functionality of the plugin. |
| 4 |
* |
| 5 |
* @link http://ays-pro.com/ |
| 6 |
* @since 6.6.9.4 |
| 7 |
* |
| 8 |
* @package Chart-Builder |
| 9 |
* @subpackage Chart-Builder/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 Chart-Builder |
| 18 |
* @subpackage Chart-Builder/includes |
| 19 |
* @author AYS Pro LLC <info@ays-pro.com> |
| 20 |
*/ |
| 21 |
class Chart_Builder_Custom_Post_Type { |
| 22 |
|
| 23 |
private $plugin_name; |
| 24 |
private $version; |
| 25 |
private $ays_chart_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_chart_flush_version = '1.0.0'; |
| 33 |
add_action( 'init', array( $this, 'ays_chart_register_custom_post_type' ) ); |
| 34 |
} |
| 35 |
|
| 36 |
public function ays_chart_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( $this->name_prefix . $this->plugin_name, $args ); |
| 48 |
$this->ays_chart_custom_rewrite_rule(); |
| 49 |
$this->ays_chart_flush_permalinks(); |
| 50 |
} |
| 51 |
|
| 52 |
public static function ays_chart_add_custom_post($args, $update = true){ |
| 53 |
|
| 54 |
$chart_id = (isset($args['chart_id']) && $args['chart_id'] != '' && $args['chart_id'] != 0) ? esc_attr($args['chart_id']) : ''; |
| 55 |
$chart_title = (isset($args['chart_title']) && $args['chart_title'] != '') ? esc_attr($args['chart_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_chart id="'.$chart_id.'"]'; |
| 59 |
|
| 60 |
$new_post = array( |
| 61 |
'post_title' => $chart_title, |
| 62 |
'post_author' => $author_id, |
| 63 |
'post_type' => 'ays-chart-builder', |
| 64 |
'post_content' => $post_content, |
| 65 |
'post_status' => 'draft', |
| 66 |
'post_date' => current_time( 'mysql' ), |
| 67 |
); |
| 68 |
|
| 69 |
|
| 70 |
$post_id = wp_insert_post($new_post); |
| 71 |
if($update){ |
| 72 |
if(isset($post_id) && $post_id > 0){ |
| 73 |
self::update_charts_table_custom_post_id($post_id, $chart_id); |
| 74 |
} |
| 75 |
} |
| 76 |
return $post_id; |
| 77 |
} |
| 78 |
|
| 79 |
public static function update_charts_table_custom_post_id($custom_post_id, $chart_id){ |
| 80 |
global $wpdb; |
| 81 |
$table = esc_sql( $wpdb->prefix . "ayschart_charts" ); |
| 82 |
$result = $wpdb->update(// phpcs:ignore WordPress.DB.DirectDatabaseQuery |
| 83 |
$table, |
| 84 |
array('custom_post_id' => $custom_post_id), |
| 85 |
array('id' => $chart_id), |
| 86 |
array('%d'), |
| 87 |
array('%d') |
| 88 |
); |
| 89 |
} |
| 90 |
|
| 91 |
public function ays_chart_flush_permalinks(){ |
| 92 |
$option_key = 'ays_chart_flush_version'; |
| 93 |
// Only flush and update when the stored version differs |
| 94 |
if ( get_option( $option_key ) !== $this->ays_chart_flush_version ) { |
| 95 |
flush_rewrite_rules(); |
| 96 |
update_option( $option_key, $this->ays_chart_flush_version ); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
public function ays_chart_custom_rewrite_rule() { |
| 101 |
add_rewrite_rule( |
| 102 |
'ays-chart-builder/([^/]+)/?', |
| 103 |
'index.php?post_type=chart-builder&name=$matches[1]', |
| 104 |
'top' |
| 105 |
); |
| 106 |
} |
| 107 |
} |
| 108 |
|