| 1 |
<?php |
| 2 |
/** |
| 3 |
* Posts handling |
| 4 |
* |
| 5 |
* Handles all post operations and detection. |
| 6 |
* |
| 7 |
* @package System |
| 8 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Decalog\System; |
| 13 |
|
| 14 |
/** |
| 15 |
* Define the post functionality. |
| 16 |
* |
| 17 |
* Handles all post operations and detection. |
| 18 |
* |
| 19 |
* @package System |
| 20 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 21 |
* @since 1.0.0 |
| 22 |
*/ |
| 23 |
class Post { |
| 24 |
|
| 25 |
/** |
| 26 |
* Initializes the class and set its properties. |
| 27 |
* |
| 28 |
* @since 1.0.0 |
| 29 |
*/ |
| 30 |
public function __construct() { |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Get a user nice name. |
| 35 |
* |
| 36 |
* @param integer $id Optional. The post id. |
| 37 |
* @param string $default Optional. Default value to return if post has no title. |
| 38 |
* @return string The user nice name if detected, $default otherwise. |
| 39 |
* @since 1.0.0 |
| 40 |
*/ |
| 41 |
public static function get_post_title( $id = 0, $default = 'untitled' ) { |
| 42 |
$title = get_the_title( $id ); |
| 43 |
if ( '' !== $id ) { |
| 44 |
return $title; |
| 45 |
|
| 46 |
} else { |
| 47 |
return $default; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Get a user string representation. |
| 53 |
* |
| 54 |
* @param integer $id Optional. The user id. |
| 55 |
* @return string The post string representation, ready to be inserted in a log. |
| 56 |
* @since 1.0.0 |
| 57 |
*/ |
| 58 |
public static function get_post_string( $id = 0 ) { |
| 59 |
$post = get_post( $id ); |
| 60 |
$id = isset( $post->ID ) ? $post->ID : 0; |
| 61 |
$title = self::get_post_title( $id ); |
| 62 |
return sprintf( '"%s" (post ID %s)', $title, $id ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Get post types. |
| 67 |
* |
| 68 |
* @return array An array of post type names. |
| 69 |
* @since 3.0.0 |
| 70 |
*/ |
| 71 |
public static function get_types() { |
| 72 |
$cache_id = 'data/post_types'; |
| 73 |
$types = Cache::get( $cache_id, true ); |
| 74 |
if ( ! $types ) { |
| 75 |
$types = get_post_types(); |
| 76 |
global $wpdb; |
| 77 |
$sql = 'SELECT DISTINCT `post_type` FROM `' . $wpdb->posts . '`;'; |
| 78 |
// phpcs:ignore |
| 79 |
$comt = $wpdb->get_results( $sql, ARRAY_A ); |
| 80 |
foreach ( $comt as $t ) { |
| 81 |
if ( ! array_key_exists( $t['post_type'], $types ) ) { |
| 82 |
$types[ strtolower( $t['post_type'] ) ] = strtolower( str_replace( '_', ' ', $t['post_type'] ) ); |
| 83 |
} |
| 84 |
} |
| 85 |
Cache::set( $cache_id, $types, 'longquery', true ); |
| 86 |
} |
| 87 |
return $types; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Get post status. |
| 92 |
* |
| 93 |
* @return array An array of post status names. |
| 94 |
* @since 3.0.0 |
| 95 |
*/ |
| 96 |
public static function get_status() { |
| 97 |
$cache_id = 'data/post_status'; |
| 98 |
$status = Cache::get( $cache_id, true ); |
| 99 |
if ( ! $status ) { |
| 100 |
$status = [ |
| 101 |
'draft' => 'Draft', |
| 102 |
'pending' => 'Pending Review', |
| 103 |
'private' => 'Private', |
| 104 |
'publish' => 'Published', |
| 105 |
'inherit' => 'Inherited', |
| 106 |
]; |
| 107 |
global $wpdb; |
| 108 |
$sql = 'SELECT DISTINCT `post_status` FROM `' . $wpdb->posts . '`;'; |
| 109 |
// phpcs:ignore |
| 110 |
$comt = $wpdb->get_results( $sql, ARRAY_A ); |
| 111 |
foreach ( $comt as $t ) { |
| 112 |
if ( ! array_key_exists( $t['post_status'], $status ) ) { |
| 113 |
$status[ strtolower( $t['post_status'] ) ] = strtolower( str_replace( '_', ' ', $t['post_status'] ) ); |
| 114 |
} |
| 115 |
} |
| 116 |
Cache::set( $cache_id, $status, 'longquery', true ); |
| 117 |
} |
| 118 |
return $status; |
| 119 |
} |
| 120 |
|
| 121 |
} |
| 122 |
|