凯悦/凯悦WordPress包

支持在Symfony2中对WordPress用户进行身份验证。

安装次数: 22,462

依赖项: 0

建议者: 0

安全: 0

星星: 101

关注者: 12

分支: 43

开放问题: 12

类型:symfony-bundle

v5.0.5-BETA 2023-10-03 07:46 UTC

README

这是原始WordpressBundle的改进版本。最大的不同是新的KayueWordpressBundle不会加载整个WordPress核心,因此所有WordPress模板函数在您的Symfony应用中都将不可用。这也是包的目标;按照Symfony的方式完成所有操作。

我两年前开始这个包,原始仓库增长得有些混乱,所以我决定用新的仓库重新开始。

Build Status

功能

  • WordPress身份验证(v1.0.0)
  • 自定义表前缀(v1.0.1)
  • WordPress实体(v1.0.2)
  • 多站点支持(v1.1.0)
  • Twig扩展(v1.1.0)
  • WordPress风格短代码(v1.1.0)
  • 主要代码更新。(v2.0.0)
  • 支持Symfony 4,新的缓存配置(v4.0.0)

待办事项

  • 单元测试(请帮助!)

安装

Composer

composer require kayue/kayue-wordpress-bundle

注册包

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Kayue\WordpressBundle\KayueWordpressBundle(),
    );
    // ...
}

配置

Doctrine

此包需要数据库连接。请确保您已正确配置Doctrine。

// app/config/parameters.yml

parameters:
    database_driver:   pdo_mysql
    database_host:     127.0.0.1
    database_port:     ~
    database_name:     my_wordpress_db
    database_user:     root
    database_password: pass

config.yml

以下配置是可选的。

kayue_wordpress:
    # Custom table prefix. Default is "wp_".
    table_prefix:   'wp_'

    # Doctrine connection to use. Default is 'default'.
    connection: 'default'

    # Specify Symfony cache pool
    orm:
        metadata_cache_pool: cache.system
        query_cache_pool: cache.app
        result_cache_pool: cache.app
    
    # The following configuration only needed only when you use WordPress authentication. 
    
    # Site URL must match *EXACTLY* with WordPress's setting. Can be found
    # on the Settings > General screen, there are field named "WordPress Address"
    site_url:       'https:///wordpress'

    # Logged in key and salt. Can be found in the wp-config.php file.
    logged_in_key:  ':j$_=(:l@8Fku^U;MQ~#VOJXOZcVB_@u+t-NNYqmTH4na|)5Bhs1|tF1IA|>tz*E'
    logged_in_salt: ')A^CQ<R:1|^dK/Q;.QfP;U!=J=(_i6^s0f#2EIbGIgFN{,3U9H$q|o/sJfWF`NRM'

    # WordPress cookie path / domain settings.
    cookie_path:    '/'
    cookie_domain:  null

使用方法

获取文章内容、作者、评论和分类的示例

<?php
// path/to/controller.php

public function postAction($slug)
{
    $repo = $this->get('kayue_wordpress')->getManager()->getRepository('KayueWordpressBundle:Post');
    $post = $repo->findOneBy(array(
        'slug'   => 'hello-world',
        'type'   => 'post',
        'status' => 'publish',
    ));

    echo $post->getTitle() , "\n";
    echo $post->getUser()->getDisplayName() , "\n";
    echo $post->getContent() , "\n";

    foreach($post->getComments() as $comment) {
        echo $comment->getContent() . "\n";
    }

    foreach($post->getTaxonomies()->filter(function(Taxonomy $tax) {
        // Only return categories, not tags or anything else.
        return 'category' === $tax->getName();
    }) as $tax) {
        echo $tax->getTerm()->getName() . "\n";
    }

    // ...
}

Twig扩展

此包包含以下Twig扩展。

函数

  • wp_switch_blog - 等同于WordPress的switch_to_blog()方法。
  • wp_find_option_by - 等同于WordPress的get_option()方法。
  • wp_find_post_by - 通过ID或别名获取文章。
  • wp_find_post_metas_by($post, $key) - 等同于WordPress的get_post_meta()方法。
  • wp_find_post_metas_by({'post': $post, 'key': $key}) - 与上述相同,接受数组作为参数。
  • wp_find_comments_by_post($post) - 返回文章中的所有已批准评论。
  • wp_find_attachments_by_post($post)
  • wp_find_attachment_by_id($id)
  • wp_find_thumbnail($post) - wp_find_featured_image_by_post的别名
  • wp_find_featured_image - 等同于WordPress的get_the_post_thumbnail方法。
  • wp_get_attachment_url($post)
  • wp_get_post_format
  • wp_find_terms_by_post
  • wp_find_categories_by_post - 等同于WordPress的get_categories()方法。
  • wp_find_tags_by_post - 等同于WordPress的get_tags()方法。

过滤器

  • wp_autop - 使用<p>标签包裹段落。用于文章格式化。
  • wp_texturize - Texturize。用于文章格式化
  • wp_shortcode - 等同于WordPress的do_shortcode()方法。

要转换额外内容,如视频链接或社交网络链接,您可以使用Essence Bundle

多站点

多站点是WordPress的一项功能,允许多个虚拟站点共享单个WordPress安装。在此包中,每个博客(站点)都有自己的实体管理器。您需要使用博客管理器检索博客,然后使用实体管理器。

以下示例展示了如何显示博客2中的最新10篇文章。

<?php

public function firstPostAction()
{
    // Method 1: Switch current blog's id. Similar to WordPress's `switch_to_blog()` method.
    // Changing the current blog ID will affect Twig extensions too.
    $blogManager = $this->get('kayue_wordpress')->getManager();
    $blogManager->setCurrentBlogId(2);
    $this->getRepository('KayueWordpressBundle:Post')->findOnePostById(1);

    // Method 2: Use entity manager if you don't want to switch the entire blog.
    // This won't change the current blog ID.
    $blogId = 3;
    $anotherBlog = $this->get('kayue_wordpress')->getManager($blogId);
    $posts = $anotherBlog->getRepository('KayueWordpressBundle:Post')->findOneById(1);
}

WordPress身份验证

此包允许您在Symfony中创建WordPress登录表单。您只需在您的security.yml中配置WordPress防火墙即可。

以下示例演示了如何将AcmeDemoBundle的登录表单转换为WordPress登录表单。

security:
    encoders:
        # Add the WordPress password encoder
        Kayue\WordpressBundle\Entity\User:
            id: kayue_wordpress.security.encoder.phpass

    providers:
        # Add the WordPress user provider
        wordpress:
            entity: { class: Kayue\WordpressBundle\Entity\User, property: username }

    firewalls:
        login:
            pattern:  ^/demo/secured/login$
            security: false
        secured_area:
            pattern:    ^/demo/secured/
            # Add the WordPress firewall. Allow you to read WordPress's login state in Symfony app.
            kayue_wordpress: ~
            # Optional. Symfony's default form login works for WordPress user too.
            form_login:
                 check_path: /demo/secured/login_check
                 login_path: /demo/secured/login
                 default_target_path: /demo/secured/hello/world
            # Optional. Use this to logout.
            logout:
                path:   /demo/secured/logout
                target: /demo/secured/login
            # ...

短代码

WordpressBundle支持WordPress风格的短代码。目前,该捆绑包只包含[caption][gallery]短代码。欢迎提交拉取请求。

要创建新的短代码,您需要

  1. 扩展ShortcodeInterface
  2. 使用kayue_wordpress.shortcode标记
<?php

use Kayue\WordpressBundle\Wordpress\Shortcode\ShortcodeInterface;

class GalleryShortcode implements ShortcodeInterface
{
    public function getName()
    {
        return 'gallery';
    }

    public function($attr, $content = null)
    {
        // do your things...

        return "<p>Return HTML</p>";
    }
}
}
services:
    acme_demo_bundle.wordpress.shortcode.gallery:
        class: Acme\DemoBundle\Wordpress\Shortcode\GalleryShortcode
        tags:
            - { name: kayue_wordpress.shortcode }