| 1 |
<?php |
| 2 |
/** |
| 3 |
* A class for handling snippets of WPCode. |
| 4 |
*/ |
| 5 |
class Meow_CDEGN_Snippets_Wpcode { |
| 6 |
|
| 7 |
/** @var string */ |
| 8 |
public static $src = 'WPCode'; |
| 9 |
|
| 10 |
/** @var string */ |
| 11 |
private $post_type = 'wpcode'; |
| 12 |
|
| 13 |
/** @var string */ |
| 14 |
private $taxonomy_code_type = 'wpcode_type'; |
| 15 |
|
| 16 |
/** @var string */ |
| 17 |
private $term_field = 'slug'; |
| 18 |
|
| 19 |
/** @var array<string> */ |
| 20 |
private $term_slugs = [ 'php' ]; |
| 21 |
|
| 22 |
/** @var array<string> */ |
| 23 |
private $post_status = [ 'publish' ]; |
| 24 |
|
| 25 |
private $order_by_pairs = [ |
| 26 |
'id' => 'ID', |
| 27 |
'name' => 'title', |
| 28 |
]; |
| 29 |
|
| 30 |
public function __construct(){ |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Get the specific snippet |
| 35 |
* |
| 36 |
* @param int $id |
| 37 |
* @return Meow_CDEGN_Models_Snippet|null |
| 38 |
*/ |
| 39 |
public function getById( int $id ) |
| 40 |
{ |
| 41 |
$post = get_post($id); |
| 42 |
|
| 43 |
return $post |
| 44 |
? new Meow_CDEGN_Models_Snippet( |
| 45 |
$post->ID, |
| 46 |
Meow_CDEGN_Snippets_Wpcode::$src, |
| 47 |
$post->post_title, |
| 48 |
$post->excerpt, |
| 49 |
$post->post_content, |
| 50 |
$this->get_edit_url( $post->ID ) |
| 51 |
) |
| 52 |
: null; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Get a snippets |
| 57 |
* |
| 58 |
* @param string $cdegn_order |
| 59 |
* @param string $order_by |
| 60 |
* @param int $limit |
| 61 |
* @param int $offset |
| 62 |
* @return array<Meow_CDEGN_Models_Snippet> |
| 63 |
*/ |
| 64 |
public function get( string $cdegn_order, string $order_by, int $limit, int $offset ) { |
| 65 |
$order_by = $this->order_by_pairs[ $order_by ] ?? 'ID'; |
| 66 |
$args = array( |
| 67 |
'post_type' => $this->post_type, |
| 68 |
'tax_query' => array( |
| 69 |
array( |
| 70 |
'taxonomy' => $this->taxonomy_code_type, |
| 71 |
'field' => $this->term_field, |
| 72 |
'terms' => $this->term_slugs, |
| 73 |
), |
| 74 |
), |
| 75 |
'post_status' => $this->post_status, |
| 76 |
'order' => strtoupper( $cdegn_order ), |
| 77 |
'orderby' => $order_by, |
| 78 |
'posts_per_page' => $limit, |
| 79 |
'offset' => $offset, |
| 80 |
); |
| 81 |
$posts = get_posts( $args ); |
| 82 |
|
| 83 |
return array_map( function( $post ) { |
| 84 |
return new Meow_CDEGN_Models_Snippet( |
| 85 |
$post->ID, |
| 86 |
Meow_CDEGN_Snippets_Wpcode::$src, |
| 87 |
$post->post_title, |
| 88 |
$post->excerpt, |
| 89 |
$post->post_content, |
| 90 |
$this->get_edit_url( $post->ID ) |
| 91 |
); |
| 92 |
}, $posts ); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Get the total number of snippets |
| 97 |
* |
| 98 |
* @return int |
| 99 |
*/ |
| 100 |
public function get_total() { |
| 101 |
$args = array( |
| 102 |
'post_type' => $this->post_type, |
| 103 |
'tax_query' => array( |
| 104 |
array( |
| 105 |
'taxonomy' => $this->taxonomy_code_type, |
| 106 |
'field' => $this->term_field, |
| 107 |
'terms' => $this->term_slugs, |
| 108 |
), |
| 109 |
), |
| 110 |
'post_status' => $this->post_status, |
| 111 |
'posts_per_page' => -1, |
| 112 |
'fields' => 'ids', |
| 113 |
); |
| 114 |
$posts = get_posts( $args ); |
| 115 |
|
| 116 |
return count( $posts ); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Get the edit URL for a snippet |
| 121 |
* |
| 122 |
* @param int $id |
| 123 |
* @return string |
| 124 |
*/ |
| 125 |
protected function get_edit_url( int $id ): string { |
| 126 |
$base_url = 'admin.php?page=wpcode-snippet-manager'; |
| 127 |
|
| 128 |
$url = self_admin_url( $base_url ); |
| 129 |
|
| 130 |
return add_query_arg( 'snippet_id', absint( $id ), $url ); |
| 131 |
} |
| 132 |
} |
| 133 |
|