public function form_publish_success( FormUI $form )
{
$post_id = 0;
if ( isset( $this->handler_vars['id'] ) ) {
$post_id = intval( $this->handler_vars['id'] );
}
// If an id has been passed in, we're updating an existing post, otherwise we're creating one
if ( 0 !== $post_id ) {
$post = Post::get( array( 'id' => $post_id, 'status' => Post::status( 'any' ) ) );
$this->theme->admin_page = sprintf(_t('Publish %s'), Plugins::filter('post_type_display', Post::type_name($post->content_type), 'singular'));
// Verify that the post hasn't already been updated since the form was loaded
if ( $post->modified != $form->modified->value ) {
Session::notice( _t( 'The post %1$s was updated since you made changes. Please review those changes before overwriting them.', array( sprintf('\'%2$s\'', $post->permalink, htmlspecialchars( $post->title ) ) ) ) );
Utils::redirect( URL::get( 'admin', 'page=publish&id=' . $post->id ) );
exit;
}
// Don't try to update form values that have been removed by plugins
$expected = array('title', 'tags', 'content');
foreach ( $expected as $field ) {
if ( isset($form->$field) ) {
$post->$field = $form->$field->value;
}
}
if ( $form->newslug->value == '' ) {
Session::notice( _t( 'A post slug cannot be empty. Keeping old slug.' ) );
}
elseif ( $form->newslug->value != $form->slug->value ) {
$post->slug = $form->newslug->value;
}
// sorry, we just don't allow changing posts you don't have rights to
if ( ! ACL::access_check( $post->get_access(), 'edit' ) ) {
Session::error( _t( 'You don\'t have permission to edit that post' ) );
$this->get_blank();
}
// sorry, we just don't allow changing content types to types you don't have rights to
$user = User::identify();
$type = 'post_' . Post::type_name( $form->content_type->value );
if ( $form->content_type->value != $post->content_type && ( $user->cannot( $type ) || ! $user->can_any( array( 'own_posts' => 'edit', 'post_any' => 'edit', $type => 'edit' ) ) ) ) {
Session::error(_t('Changing content types is not allowed'));
$this->get_blank();
}
$post->content_type = $form->content_type->value;
// if not previously published and the user wants to publish now, change the pubdate to the current date/time
// if the post pubdate is <= the current date/time.
if ( ( $post->status != Post::status( 'published' ) )
&& ( $form->status->value == Post::status( 'published' ) )
&& ( HabariDateTime::date_create( $form->pubdate->value )->int <= HabariDateTime::date_create()->int )
) {
$post->pubdate = HabariDateTime::date_create();
}
// else let the user change the publication date.
// If previously published and the new date is in the future, the post will be unpublished and scheduled. Any other status, and the post will just get the new pubdate.
// This will result in the post being scheduled for future publication if the date/time is in the future and the new status is published.
else {
$post->pubdate = HabariDateTime::date_create( $form->pubdate->value );
}
$minor = $form->minor_edit->value && ($post->status != Post::status('draft'));
$post->status = $form->status->value;
}
else {
$post = new Post();
// check the user can create new posts of the set type.
$user = User::identify();
$type = 'post_' . Post::type_name($form->content_type->value);
if ( ACL::user_cannot( $user, $type) || ( ! ACL::user_can( $user, 'post_any', 'create' ) && ! ACL::user_can( $user, $type, 'create') ) ) {
Session::error(_t('Creating that post type is denied'));
$this->get_blank();
}
$form->on_success( array( $this, 'form_publish_success' ) );
if ( HabariDateTime::date_create( $form->pubdate->value )->int > $post->pubdate->int ) {
$post->pubdate = HabariDateTime::date_create( $form->pubdate->value );
}
$postdata = array(
'slug' => $form->newslug->value,
'user_id' => User::identify()->id,
'pubdate' => $post->pubdate,
'status' => $form->status->value,
'content_type' => $form->content_type->value,
);
// Don't try to add form values that have been removed by plugins
$expected = array('title', 'tags', 'content');
foreach ( $expected as $field ) {
if ( isset($form->$field) ) {
$postdata[$field] = $form->$field->value;
}
}
$minor = false;
$post = Post::create( $postdata );
}
if ( $post->pubdate->int > HabariDateTime::date_create()->int && $post->status == Post::status( 'published' ) ) {
$post->status = Post::status( 'scheduled' );
}
$post->info->comments_disabled = !$form->comments_enabled->value;
Plugins::act('publish_post', $post, $form);
$post->update( $minor );
$permalink = ( $post->status != Post::status( 'published' ) ) ? $post->permalink . '?preview=1' : $post->permalink;
Session::notice( sprintf( _t( 'The post %1$s has been saved as %2$s.' ), sprintf('\'%2$s\'', $permalink, htmlspecialchars( $post->title ) ), Post::status_name( $post->status ) ) );
if ( $post->slug != Utils::slugify( $post->title ) ) {
Session::notice( sprintf( _t( 'The content address is \'%1$s\'.'), $post->slug ));
}
Utils::redirect( URL::get( 'admin', 'page=publish&id=' . $post->id ) );
}