| 1 |
<?php |
| 2 |
/** |
| 3 |
* Module Logs Site connector class. |
| 4 |
* |
| 5 |
* @package MainWP\Dashboard |
| 6 |
* @version 4.5.1 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace MainWP\Dashboard\Module\Log; |
| 10 |
|
| 11 |
use MainWP\Dashboard\MainWP_Utility; |
| 12 |
|
| 13 |
defined( 'ABSPATH' ) || exit; |
| 14 |
|
| 15 |
/** |
| 16 |
* Class Connector_Posts |
| 17 |
* |
| 18 |
* @package MainWP\Dashboard |
| 19 |
*/ |
| 20 |
class Connector_Posts extends Log_Connector { |
| 21 |
/** |
| 22 |
* Connector slug |
| 23 |
* |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
public $name = 'posts'; |
| 27 |
|
| 28 |
/** |
| 29 |
* Actions registered for this connector |
| 30 |
* |
| 31 |
* @var array |
| 32 |
*/ |
| 33 |
public $actions = array( |
| 34 |
'mainwp_post_created', |
| 35 |
'mainwp_post_action', |
| 36 |
); |
| 37 |
|
| 38 |
/** |
| 39 |
* Return translated connector label |
| 40 |
* |
| 41 |
* @return string Translated connector label |
| 42 |
*/ |
| 43 |
public function get_label() { |
| 44 |
return esc_html__( 'Posts', 'mainwp' ); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Return translated action labels |
| 49 |
* |
| 50 |
* @return array Action label translations |
| 51 |
*/ |
| 52 |
public function get_action_labels() { |
| 53 |
return array( |
| 54 |
'published' => esc_html__( 'Published', 'mainwp' ), |
| 55 |
'updated' => esc_html__( 'Updated', 'mainwp' ), |
| 56 |
'created' => esc_html__( 'Created', 'mainwp' ), |
| 57 |
'trashed' => esc_html__( 'Trashed', 'mainwp' ), |
| 58 |
'untrashed' => esc_html__( 'Restored', 'mainwp' ), |
| 59 |
'deleted' => esc_html__( 'Deleted', 'mainwp' ), |
| 60 |
); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Return translated context labels |
| 65 |
* |
| 66 |
* @return array Context label translations |
| 67 |
*/ |
| 68 |
public function get_context_labels() { |
| 69 |
global $wp_post_types; |
| 70 |
$post_types = wp_filter_object_list( $wp_post_types, array(), null, 'label' ); |
| 71 |
add_action( 'registered_post_type', array( $this, 'registered_post_type' ), 10, 2 ); |
| 72 |
return $post_types; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Catch registration of post_types after initial loading, to cache its labels |
| 77 |
* |
| 78 |
* @action registered_post_type |
| 79 |
* |
| 80 |
* @param string $post_type Post type slug. |
| 81 |
* @param array $args Arguments used to register the post type. |
| 82 |
*/ |
| 83 |
public function registered_post_type( $post_type, $args ) { |
| 84 |
unset( $args ); |
| 85 |
|
| 86 |
$post_type_obj = get_post_type_object( $post_type ); |
| 87 |
$label = $post_type_obj->label; |
| 88 |
|
| 89 |
Log_Manager::instance()->connectors->term_labels['logs_context'][ $post_type ] = $label; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Log create post. |
| 94 |
* |
| 95 |
* @action mainwp_post_created |
| 96 |
* |
| 97 |
* @param mixed $website website. |
| 98 |
* @param string $post_action post action. |
| 99 |
* @param array $data post data array. |
| 100 |
*/ |
| 101 |
public function callback_mainwp_post_created( $website, $post_action, $data ) { |
| 102 |
if ( empty( $website ) || ! is_array( $data ) || empty( $data['post_title'] ) ) { |
| 103 |
return; |
| 104 |
} |
| 105 |
$action = ''; |
| 106 |
if ( 'newpost' === $post_action ) { |
| 107 |
$action = 'created'; |
| 108 |
if ( ! empty( $data['is_editing'] ) ) { |
| 109 |
$action = 'updated'; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
if ( empty( $action ) ) { |
| 114 |
return; |
| 115 |
} |
| 116 |
|
| 117 |
// translators: %1$s: Post title. |
| 118 |
$message = esc_html_x( |
| 119 |
'Post: %1$s', |
| 120 |
'1: Post title', |
| 121 |
'mainwp' |
| 122 |
); |
| 123 |
|
| 124 |
$default = array( |
| 125 |
'post_title' => '', |
| 126 |
'singular_name' => '', |
| 127 |
); |
| 128 |
$args = MainWP_Utility::right_array_merge( $default, $data ); |
| 129 |
|
| 130 |
$default_other = array( |
| 131 |
'post_date' => '', |
| 132 |
'post_date_gmt' => '', |
| 133 |
'new_uptime_status' => '', |
| 134 |
'old_status' => '', |
| 135 |
'post_id' => '', |
| 136 |
); |
| 137 |
$other = MainWP_Utility::right_array_merge( $default_other, $data ); |
| 138 |
$args['extra_info'] = wp_json_encode( $other ); |
| 139 |
$state = 1; |
| 140 |
$this->log( |
| 141 |
$message, |
| 142 |
$args, |
| 143 |
$website->id, |
| 144 |
$data['post_type'], |
| 145 |
$action, |
| 146 |
$state |
| 147 |
); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Log all post status changes ( creating / updating / trashing ) |
| 152 |
* |
| 153 |
* @action mainwp_post_action |
| 154 |
* |
| 155 |
* @param object $website website. |
| 156 |
* @param string $post_action post action. |
| 157 |
* @param array $data post data array. |
| 158 |
*/ |
| 159 |
public function callback_mainwp_post_action( $website, $post_action, $data ) { |
| 160 |
|
| 161 |
if ( empty( $website ) || ! is_array( $data ) || empty( $data['post_title'] ) ) { |
| 162 |
return; |
| 163 |
} |
| 164 |
|
| 165 |
$action = ''; |
| 166 |
// translators: %1$s: Post title, %2$s: Post type singular name (e.g. "Hello World", "Post"). |
| 167 |
$message = esc_html_x( |
| 168 |
'Post: %1$s (%2$s)', |
| 169 |
'1: Post title 2: singular name', |
| 170 |
'mainwp' |
| 171 |
); |
| 172 |
|
| 173 |
if ( 'publish' === $post_action ) { |
| 174 |
$action = 'published'; |
| 175 |
} elseif ( 'update' === $post_action ) { |
| 176 |
$action = 'updated'; |
| 177 |
} elseif ( 'unpublish' === $post_action ) { |
| 178 |
$action = 'unpublished'; |
| 179 |
} elseif ( 'trash' === $post_action ) { |
| 180 |
$action = 'trashed'; |
| 181 |
} elseif ( 'delete' === $post_action ) { |
| 182 |
$action = 'deleted'; |
| 183 |
} elseif ( 'restore' === $post_action ) { |
| 184 |
$action = 'restored'; |
| 185 |
} else { |
| 186 |
return; |
| 187 |
} |
| 188 |
|
| 189 |
$default = array( |
| 190 |
'post_title' => '', |
| 191 |
'singular_name' => '', |
| 192 |
'post_date' => '', |
| 193 |
'post_date_gmt' => '', |
| 194 |
'new_uptime_status' => '', |
| 195 |
'old_status' => '', |
| 196 |
); |
| 197 |
|
| 198 |
$args = MainWP_Utility::right_array_merge( $default, $data ); |
| 199 |
|
| 200 |
$state = 1; |
| 201 |
$this->log( |
| 202 |
$message, |
| 203 |
$args, |
| 204 |
$website->id, |
| 205 |
$data['post_type'], |
| 206 |
$action, |
| 207 |
$state |
| 208 |
); |
| 209 |
} |
| 210 |
} |
| 211 |
|