Laravel使用laravel-permission进行权限管理

安装

composer require spatie/laravel-permission

发布迁移文件:

php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" --tag="migrations"

在config/app.php 中,Providers 数组里添加:

'providers' => [
    // ...
    Spatie\Permission\PermissionServiceProvider::class,
];

发布配置文件:

php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" --tag="config"

迁移数据库

php artisan migrate

在User Model 引用 HasRoles trait:

use Spatie\Permission\Models\Role;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable, HasRoles;
}

用法:

创建角色:$role=Role::create(['name'=>'超级管理员']);

创建权限:$permission=Permission::create(['name'=>'*']);

给角色赋予权限:$role->givePermissionTo($permission);

给用户一个角色:$user->assignRole('超级管理员');

在blade页面获取用户的第一个角色:{{ Auth()->user()->getRoleNames()->first() }}

WordPress 修改附件元数据

今天修改了一张图片的名字,以为在数据库里同步修改就行了,结果不是那么回事,在wp_postmeta表中有个meta_key字段保存了一个附件的元数据,meta_key的值是_wp_attachment_metadata,对应的meta_value是附件的元数据,如下图所示:

这个元数据是不能直接编辑的,要用代码生成,代码如下:

// 获取附件ID
$attachment_id = 3261; // 请根据实际的附件ID修改

// 获取附件元数据
$metadata = wp_get_attachment_metadata( $attachment_id );

// 修改文件路径和文件名
$new_file = '2023/08/zaochencongzhongwukaishi.jpeg'; // 新的文件路径
$metadata['file'] = $new_file;

// 修改各个尺寸的路径(如果有需要)
foreach ( $metadata['sizes'] as $size => $size_data ) {
    $metadata['sizes'][$size]['file'] = str_replace('zaochenchongzhongwukaishi', 'zaochencongzhongwukaishi', $size_data['file']);
}

// 更新附件元数据
wp_update_attachment_metadata( $attachment_id, $metadata );

注意在执行这段代码前,不要动原来的元数据。

可以把这面代码放在functions.php里执行,或者放到某个插件代码文件里执行都行。执行完了记得删除。

补充:附件ID需要到wp_posts表找。

关于FreshRSS的一些记录

Freshrss推荐的API是 Google Reader API

移动客户端可以用 Fluent Reader Lite

FreshRSS自动更新订阅,需要用到系统定时任务。

使用以下命令编辑 www-data 用户的 crontab:

sudo crontab -u www-data -e

在打开的 crontab 编辑器中,添加以下行:

10 * * * * php -f /var/www/freshrss.wujie.me/app/actualize_script.php > /tmp/FreshRSS.log 2>&1

保存并退出编辑器,这样,定时任务就会每小时的第 10 分钟运行,并将输出记录到 /tmp/FreshRSS.log 文件中。

给wordpress添加归档页面

今天给另一个网站增加一个文章归档功能,步骤如下:

1、复制page.php,改名为archive.php,并在最顶部添加以下代码,告诉wordpress这是一个模板

/*
Template Name: Archives
*/

2、把page.php中间输出内容的部分换成我们的归档代码。

完整代码如下:

<?php
/*
Template Name: Archives
*/
get_header(); ?>

<style>
	.monthly-posts {
		list-style-type: none;
		/* 去掉默认的列表样式 */
		padding-left: 0;
		/* 去掉外边距 */
	}

	.monthly-posts li {
		margin-left: 20px;
		/* 为每个文章项添加左侧缩进 */
		line-height: 1.8;
		/* 增加行高让显示更清晰 */

		margin-left: 20px;
		line-height: 1.6;
		margin-bottom: 5px;
		/* 增加每个列表项之间的间距 */
	}

	.monthly-posts li a {
		font-weight: bold;
		/* 强调文章标题 */
	}


	.monthly-posts li span.post-date {
		font-size: 0.9em;
		color: #666;
		/* 设置日期的颜色 */
		margin-left: 10px;
	}

	/* 针对 <p> 标签样式进行调整 */
	.monthly-posts p.post-excerpt {
		font-size: 1em;
		color: #444;
		/* 设置摘要文本颜色 */
		margin-top: 5px;
		line-height: 1.6;
		margin-left: 40px;
		/* 添加左侧缩进 */
	}

	h3 {
		font-size: 1.2em;
		color: #333;
		margin-top: 20px;
		margin-bottom: 10px;
	}

	.post-count {
		margin-top: 30px;
		font-size: 1.2em;
		font-weight: bold;
		color: #333;
		text-align: center;
	}
</style>


<div id="primary" class="site-content">
	<div id="content" role="main">

		<?php
		// 获取按月归档的日期
		$archives = $wpdb->get_results("
    SELECT DISTINCT YEAR(post_date) AS year, MONTH(post_date) AS month
    FROM $wpdb->posts
    WHERE post_status = 'publish'
    ORDER BY post_date DESC
");

		// 遍历每个月的归档
		if ($archives) :
			foreach ($archives as $archive) :
				// 获取当前月份的文章
				$year_month = $archive->year . '-' . str_pad($archive->month, 2, '0', STR_PAD_LEFT);

				// 设置自定义查询来列出该月的文章
				$query = new WP_Query(array(
					'year'  => $archive->year,
					'monthnum' => $archive->month,
					'posts_per_page' => -1 // -1 表示列出该月所有文章
				));

				// 如果该月有文章,显示月份和文章列表
				if ($query->have_posts()) :
					echo '<h3>' . $archive->year . '年' . $archive->month . '月</h3>';
					echo '<ul class="monthly-posts">';
					while ($query->have_posts()) : $query->the_post();
						// 获取文章的发布日期
						$post_date = get_the_date('Y年m月d日'); // 修改为你想要的日期格式
						echo '<li><a href="' . get_permalink() . '" title="' . get_the_title() . '">' . get_the_title() . '</a>  <span class="post-date">- ' . $post_date . '</span></li>';
						// 输出文章摘要
						echo '<p class="post-excerpt">' . get_the_excerpt() . '</p>';
					endwhile;
					echo '</ul>';
				endif;

				// 重置查询
				wp_reset_postdata();
			endforeach;
		else :
			echo '<p>没有按月归档的文章。</p>';
		endif;
		?>

		<?php
		// 获取已发布文章的数量
		$post_count = wp_count_posts()->publish;
		?>

		<div class="post-count">
			<p>总共写了 <?php echo $post_count; ?> 篇文章。</p>
		</div>


	</div><!-- #content -->
</div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

3、把archive.php传到服务器主题目录下,并新建一个页面,在该新建页面里面选择模板,选择Archives,保存即可。

PHP解决http_build_query中&not被转义的问题

在开发一个支付项目时,遇到PHP把&not给转义了,可以通过htmlspecialchars再转一次就可以了:

<?php
 $vars = array(
             'book' => 'humpty',
             'newspaper' => 'onion',
             'note' => 'not working');
$query = http_build_query($vars);
$fixed_query = htmlspecialchars($query, ENT_QUOTES, 'UTF-8');

该段代码的功能是http_build_query根据数组产生一个urlencode之后的请求字符串。