Completed
Push — BaseComponent-PostHero ( 17c243...37e6c7 )
by
unknown
02:01
created

functions.php ➔ getShareProviders()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 22
nc 1
nop 3
dl 0
loc 34
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Flynt\Components\HeroPostHeader;
4
5
use Timber\Timber;
6
use Flynt\Features\Components\Component;
7
use Flynt\Utils\Asset;
8
9
add_action('wp_enqueue_scripts', function () {
10
    Component::enqueueAssets('HeroPostHeader', []);
11
});
12
13
add_filter('Flynt/addComponentData?name=HeroPostHeader', function ($data) {
14
    $data['post'] = Timber::get_post();
15
    $data['providers'] = getShareProviders(
16
        $data['post']->post_title,
17
        $data['post']->link,
18
        $data['post']->post_excerpt
19
    );
20
    return $data;
21
});
22
23
function getShareProviders($postTitle, $postUrl, $postExcerpt)
24
{
25
    $providers = [
26
        [
27
            'title' => 'Facebook',
28
            'class' => 'share-facebook',
29
            'url' => 'https://www.facebook.com/sharer/sharer.php?u=$url'
30
        ],
31
        [
32
            'title' => 'Twitter',
33
            'class' => 'share-twitter',
34
            'url' => 'https://twitter.com/home?status=$url'
35
        ],
36
        [
37
            'title' => 'Google+',
38
            'class' => 'share-google',
39
            'url' => 'https://plus.google.com/share?url=$url'
40
        ],
41
        [
42
            'title' => 'Linkedin',
43
            'class' => 'share-linkedin',
44
            'url' => 'https://www.linkedin.com/shareArticle?mini=true&url=$url&title=$title&summary=$summary'
45
        ]
46
    ];
47
48
    $providers = array_map(function($provider) use ($postTitle, $postUrl, $postExcerpt) {
49
        $shareUrl = str_replace('$url', $postUrl, $provider['url']);
50
        $shareUrl = str_replace('$title', $postTitle, $shareUrl);
51
        $shareUrl = str_replace('$summary', $postExcerpt, $shareUrl);
52
        $provider['shareUrl'] = $shareUrl;
53
        return $provider;
54
    }, $providers);
55
    return $providers;
56
}
57