murdercode/laravel-shortcode-plus 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

murdercode/laravel-shortcode-plus

最新稳定版本:v5.3.1

Composer 安装命令:

composer require murdercode/laravel-shortcode-plus

包简介

A package that allows you to use custom shortcodes

README 文档

README

Logo Laravel Shortcode Plus

Latest Version on Packagist GitHub Tests Action Status GitHub PHPStan GitHub Code Style Action Status Maintainability Test Coverage License Mit Total Downloads

Why Shortcode+?

This package allows you to use shortcodes in your application, like a Wordpress / BBS style websites.

In our days, shortcodes are a great way to preserve the integrity of the data within the content published on our site (such as a blog or forum) without risking having to rewrite the format each time.

With Laravel Shortcode+ we have the ability to turn a standard shortcode into a dynamic asset that can update over time (new HTML standards, cookie consent, AMP versions, and more)!

Warning: this is a very opinionated package and it's not intended to be multi-purpose.

How it Works

For example, you can use the following shortcode to embed a Youtube video:

[youtube url="https://www.youtube.com/watch?v=dQw4w9WgXcQ"]

This will be rendered as:

<iframe
    src="https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ&autoplay=1"
    srcdoc="<style>*{padding:0;margin:0;overflow:hidden}html,body{height:100%}img,span{position:absolute;width:100%;top:0;bottom:0;margin:auto}span{height:1.5em;text-align:center;font:48px/1.5 sans-serif;color:white;text-shadow:0 0 0.5em black}</style><a href=https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ?autoplay=1><img style='object-fit:cover;height:100%;' loading='lazy' src=https://img.youtube.com/vi/123456789/hqdefault.jpg alt='dQw4w9WgXcQ'
        loading=lazy><span>▶</span></a>"
    frameborder="0"
    allow="accelerometer; autoplay; encrypted-media; gyroscope;
        picture-in-picture"
    allowfullscreen
    title="dQw4w9WgXcQ"
></iframe>

As you can see, we don't just generate an iframe but make it accessible, performant and in line with the best SEO practices around.

Requirements

Package Version Requirement Version
5.x.x Laravel 10.x or 11.x
5.x.x Nova 4.x
dev-beta-laravel-12 Laravel 12.x
dev-beta-laravel-12 Nova 5.x

Installation

You can install the package via composer:

composer require murdercode/laravel-shortcode-plus

You can use shortcodes CSS publishing the assets:

php artisan vendor:publish --tag="shortcode-plus-assets"

You can publish and run the migrations with:

php artisan vendor:publish --tag="shortcode-plus-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --tag="shortcode-plus-config"

If you want to upgrade every time your assets, add in your composer.json:

    "scripts": {
"post-update-cmd": [
"@php artisan vendor:publish --tag=shortcode-plus-assets --ansi --force",

Optionally, you can publish the views using

php artisan vendor:publish --tag="shortcode-plus-views"

Usage

Laravel Shortcode Plus is shipped with a default CSS and JS for a better user experience. You can add on resources/css/app.css the CSS files:

@import url("/public/vendor/shortcode-plus/app.css");

and in resources/js/app.js the JS files:

import '/public/vendor/shortcode-plus/app2.js';

Now you can parse your source as follows:

use Murdercode\LaravelShortcodePlus\Facades\LaravelShortcodePlus;

$html = "I want to parse this twitter tag: [twitter url=\"https://twitter.com/elonmusk/status/1585841080431321088\"]";
return LaravelShortcodePlus::source($html)->parseAll();

Use Iubenda Cookie

Add in your iubenda cookie script the following code: (/organisms/cookie-solution.blade.php)

if (purposeId === "3") {
    var elements = document.getElementsByClassName('shortcode_nocookie');
    for (var i = 0; i < elements.length; i++) {
        elements[i].style.display = 'none';
    }
}

Use Paywall with Iubenda Cookie

In config, set cookiePaywall to true

In your iubenda cookie script, add the following code: (/organisms/cookie-solution.blade.php)

<script>
    function manageShortcodePaywall() {
        const shortcodesWithPaywall = document.querySelectorAll('.shortcode_with_paywall');
        const paywalls = document.querySelectorAll('.shortcode_paywall');

        shortcodesWithPaywall.forEach(shortcode => {
            if (_iub.cs.api.isConsentGiven()) {
                shortcode.style.display = 'block';
                paywalls.forEach(paywall => paywall.style.display = 'none');
            } else {
                shortcode.style.display = 'none';
                paywalls.forEach(paywall => paywall.style.display = 'block');
            }
        });
    }
    
    var _iub = _iub || [];
_iub.csConfiguration.callback.onPreferenceExpressed = manageShortcodePaywall;
_iub.csConfiguration.callback.onReady = manageShortcodePaywall;
<script>
    var paywallPrefBtn = document.querySelector('.shortcode_paywall button');
    paywallPrefBtn.addEventListener('click', function(ev) {
        ev.preventDefault();
        _iub.cs.api.acceptAll();
    });
</script>

Indexing feature

If you want to use the [index] shortcode, you can add the withAutoHeadingIds() method to your source before parsing it. It will add an automatic ID to every headline (h2, h3, h4 etc...) in your source:

return LaravelShortcodePlus::source($html)->withAutoHeadingIds()->parseAll();

This will add an ID to every heading (h2, h3, h4 etc...) in your source.

Parsers

Here is the list of the available parsers:

Shortcode Description Parameters Example
[twitter] Get a Twitter card url [twitter url="https://twitter.com/elonmusk/status/1585841080431321088"]
[bluesky] Get a BlueSky card url [bluesky url="https://bsky.app/profile/adamparkhomenko.bsky.social/post/3liz5p73u6k2f"]
[youtube] Get a Youtube (light) player url [youtube url="https://www.youtube.com/watch?v=9bZkp7q19f0"]
[spotify] Get a Spotify player url or uri [spotify url="https://open.spotify.com/track/2TpxZ7JUBn3uw46aR7qd6V"]
[faq] Create a <details> tag with embedded content title [faq title="What is the answer to the ultimate question?"]42[/faq]
[spoiler] Create a <details> tag with embedded content title [spoiler title="Spoiler"]This is hidden content[/spoiler]
[facebook] Get a Facebook card url [facebook url="https://www.facebook.com/elonmusk/posts/10157744420210129"]
[instagram] Get a Instagram card url [instagram url="https://www.instagram.com/p/CApQfIjBGxC/"]
[image] Create an image with Image::class model id, caption (optional) [image id="123"]
[gallery] Create a gallery image with Image::class model title, images Single or multiple images: [gallery title="Gallery title here" images="1"] or [gallery title="Gallery title here" images="1,2,3"]
[photo] Create a gallery image with [Nova Media Hub](https://github.com/outl1ne/nova-media-hub) model didascalia effect(optional) link(optional) shape(optional) Single or multiple images: `[photo didascalia="Gallery title here" id="1,2,3"] Effect [photo id="1,2,3" effect="carousel - juxtapose - gallery-flex" link="https://..." shape="default
[leggianche] Create a Read more div, based on Article or Post model id [leggianche id="1"]
[distico] Create a side text block, based on Article or Post model id [distico id="1"]
[button] Create a button that links to an URL link, label, level (optional) [button link="https://www.google.com" label="Google" level="primary/secondary"]
[tmdb] Create a TMDB card type, id [tmdb type="movie/tv" id="123"]
[widgetbay] Create a Widgetbay iframe id (optional), link (optional), forceLink (optional), title (optional) [widgetbay id="1"] [widgetbay title="Product Title" link="https://www.amazon.it/product?tag="41515&subtag="5151"..."]
[index] Create an automatic index based on Heading (h2, h3, h4 etc...) none [index]
[trivia] Create a trivia id [trivia id="1"]

Note for Facebook

Please remember to call the SDK before </body>:

<div id="fb-root"></div>
<script
    async
    defer
    crossorigin="anonymous"
    src="https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v15.0"
    nonce="UcAjseAO"
></script>

Note for Twitter

Please remember to call the SDK before </body>:

<script type="text/javascript">
    window.twttr = (function (d, s, id) {
        var js, fjs = d.getElementsByTagName(s)[0],
            t = window.twttr || {};
        if (d.getElementById(id)) return t;
        js = d.createElement(s);
        js.id = id;
        js.src = "https://platform.twitter.com/widgets.js";
        fjs.parentNode.insertBefore(js, fjs);

        t._e = [];
        t.ready = function (f) {
            t._e.push(f);
        };

        return t;
    }(document, "script", "twitter-wjs"));
</script>

Note for Reddit

Please remember to call the SDK before </body>:

<script async src="https://embed.reddit.com/widgets.js" charset="UTF-8"></script>

Note for Justwatch

Please remember to call the SDK before </body>:

<script async src="https://widget.justwatch.com/justwatch_widget.js" type="text/javascript"></script>

Note for Parse links

Please remember to add in config file the links to parse:

    'linksToParse' => [
        'sponsored' => [
            '#https://www\\.amazon\\.[A-Za-z]+#i',
            '#https://www\\.ebay\\.[A-Za-z]+#i',
            'https://www.instant-gaming.com',
        ],
        'dofollow' => [
            'https://forum.tomshw.it/',
        ],
        'nofollow' => [
            'https://www.youtube.com',
            'https://multiplayer.it',
            'https://www.everyeye.it',
        ],
    ],

You can use a regex or a string to parse the links.

And, when parse your content, you can use forceRel():

    $content = LaravelShortcodePlus::source($content)
        ->forceRel()
        ->parseAll();

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

murdercode/laravel-shortcode-plus 适用场景与选型建议

murdercode/laravel-shortcode-plus 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 548 次下载、GitHub Stars 达 5, 最近一次更新时间为 2022 年 10 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「embed」 「shortcodes」 「shortcode」 「murdercode」 「laravel-shortcode-plus」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 murdercode/laravel-shortcode-plus 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 murdercode/laravel-shortcode-plus 我们能提供哪些服务?
定制开发 / 二次开发

基于 murdercode/laravel-shortcode-plus 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 548
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 12
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 0
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-10-30