0){ $postIds = []; foreach($posts as $post){ $postIds[] = $post->post_id; } return $postIds; } return []; } /** * Get folder ID by post ID * @param int $post_id * * @return int */ public static function getFolderIdByPostId($post_id, $post_type = 'attachment'){ $uid = self::getUID($post_id, $post_type); $row = Helpers::query( "SELECT `folder_id` FROM " . self::getTable() . " WHERE `uid` = %s", $uid, 'get_row', OBJECT ); if( $row && isset($row->folder_id) ){ return $row->folder_id; } return null; } /** * Get folder details by post ID * * @param int $post_id * * @return array */ public static function getFolderByPostId($post_id, $post_type = 'attachment'){ $uid = self::getUID($post_id, $post_type); return Helpers::query( "SELECT f.* FROM " . self::getTable('az_folders') . " f INNER JOIN " . self::getTable() . " fp ON f.id = fp.folder_id WHERE fp.uid = %s", $uid, 'get_row', OBJECT ); } /** * Assign post to folder * * @param int $post_id * @param int $folder_id * * @return bool */ public static function assignPostToFolder($post_type, $post_id, $folder_id){ if( !$post_id || !$folder_id ) return false; if( !$post_type) $post_type = 'attachment'; $folderIds = []; $current_folder_id = self::getFolderIdByPostId($post_id, $post_type); if( $current_folder_id == $folder_id ) return true; if( $current_folder_id ){ $result = self::changeFolderId($post_id, $folder_id, $post_type); if( $result ){ $folderIds[] = $current_folder_id; $folderIds[] = $folder_id; self::updateCountPosts($current_folder_id); self::updateCountPosts($folder_id); } } else{ $result = self::addPostToFolder($post_id, $folder_id, $post_type); if( $result ){ $folderIds[] = $folder_id; self::updateCountPosts($folder_id); } } return $folderIds; } /** * Add post to folder * * @param int $post_id * @param int $folder_id * * @return bool */ public static function addPostToFolder($post_id, $folder_id, $post_type = 'attachment'){ if( !$post_id || !$folder_id ) return false; $uid = self::getUID($post_id, $post_type); return self::addPost(array( 'post_id' => $post_id, 'uid' => $uid, 'folder_id' => $folder_id )); } /** * Change folder ID of post */ public static function changeFolderId($post_id, $folder_id, $post_type = 'attachment'){ if( !$post_id || !$folder_id ) return false; $uid = self::getUID($post_id, $post_type); return self::updatePost( $uid, array('folder_id' => $folder_id) ); } /** * Add or update post * * @param array $post * * @return bool */ public static function addOrUpdate($post, $post_type = 'attachment'){ if( !$post || !isset($post['post_id']) || !$post['post_id'] || !isset($post['folder_id']) || !$post['folder_id'] ) return false; $post_id = intval($post['post_id']); $folder_id = intval($post['folder_id']); if( !$post_type ) $post_type = 'attachment'; $uid = self::getUID($post_id, $post_type); $current_folder_id = self::getFolderIdByPostId($post_id, $post_type); if( $current_folder_id == $folder_id ) return true; if( $current_folder_id ){ $result = self::updatePost($uid, $post); if( $result ){ self::updateCountPosts($current_folder_id); self::updateCountPosts($folder_id); } } else{ $post['uid'] = $uid; $result = self::addPost($post); if( $result ){ self::updateCountPosts($folder_id); } } } /** * Add post array to folder * @param array $post */ public static function addPost($post){ global $wpdb; if( !$post || !isset($post['post_id']) || !$post['post_id'] || !isset($post['folder_id']) || !$post['folder_id'] ) return false; $post['post_id'] = intval($post['post_id']); $post['folder_id'] = intval($post['folder_id']); if( !isset($post['uid']) || !$post['uid'] ){ $post['uid'] = 'attachment_' . $post['post_id']; } $user_id = get_current_user_id(); $time = current_time('mysql'); if( !isset($post['created_by']) || !$post['created_by'] ){ $post['created_by'] = $user_id; } if( !isset($post['created_at']) || !$post['created_at'] ){ $post['created_at'] = $time; } if( !isset($post['updated_by']) || !$post['updated_by'] ){ $post['updated_by'] = $user_id; } if( !isset($post['updated_at']) || !$post['updated_at'] ){ $post['updated_at'] = $time; } $post = apply_filters('azdrive_post_add', $post); if( !$post ) return false; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery return $wpdb->insert(self::getTable(), $post); } /** * Update post fields * @param int $post_id * @param array $fields */ public static function updatePost($uid, $fields){ global $wpdb; if( !$uid ) return false; $user_id = get_current_user_id(); $row = array( 'updated_by' => $user_id, 'updated_at' => current_time('mysql') ); if( is_array($fields) && count($fields) > 0 ){ foreach($fields as $key => $value){ $row[$key] = $value; } } $row = apply_filters('azdrive_post_update', $row, $uid); if( !$row ) return false; // phpcs:ignore WordPress.DB.DirectDatabaseQuery return $wpdb->update( self::getTable(), $row, array('uid' => $uid) ); } /** * Count files in folder * @param int $folder_id */ public static function updateCountPosts($folder_id){ if( !$folder_id ) return false; $count = Helpers::query( "SELECT COUNT(*) FROM " . self::getTable() . " WHERE `folder_id` = %d", $folder_id, 'get_var' ); if( !$count ) $count = 0; FolderModel::updateFolder($folder_id, array('total_files' => $count)); return $count; } /** * Delete all posts from folder * * @param int $folder_id * @param bool $deleteOriginal use to delete original file * * @return bool */ public static function deleteAllPostsFromFolder($folder_id, $deleteOriginal = false){ global $wpdb; if( !$folder_id ) return false; // phpcs:ignore WordPress.DB.DirectDatabaseQuery return $wpdb->delete( self::getTable(), array('folder_id' => $folder_id) ); } /** * Delete post from folder * * @param int $post_id * * @return bool */ public static function deletePostFromFolder($post_id, $post_type = 'attachment'){ global $wpdb; if( !$post_id ) return false; $folder_id = self::getFolderIdByPostId($post_id, $post_type); $uid = self::getUID($post_id, $post_type); // phpcs:ignore WordPress.DB.DirectDatabaseQuery $result = $wpdb->delete( self::getTable(), array('uid' => $uid) ); if( $result ){ self::updateCountPosts($folder_id); } return $result; } /** * Delete all posts */ public static function deleteAll(){ global $wpdb; // phpcs:ignore WordPress.DB return $wpdb->query("DELETE FROM " . self::getTable()); } public static function getUID($post_id, $post_type = 'attachment'){ if( !$post_id ) return false; return $post_type . '_' . $post_id; } /** * Get table name * @return string */ public static function getTable($table = 'az_folder_posts'){ global $wpdb; return $wpdb->prefix . $table; } } ?>