Completed
Push — develop ( e22e79...744247 )
by David
03:50
created

Wordlift_ShareThis_Service::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 5
rs 9.4286
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * A service to maintain a compatibilty layer with the ShareThis plugin (which only displays itself on pages and posts).
5
 *
6
 * @since 3.2.0
7
 */
8
class Wordlift_ShareThis_Service {
9
10
	/**
11
	 * The ShareThis function which prints the buttons.
12
	 *
13
	 * @since 3.2.0
14
	 */
15
	const ADD_WIDGET_FUNCTION_NAME = 'st_add_widget';
16
17
	/**
18
	 * The Log service.
19
	 *
20
	 * @since 3.2.0
21
	 * @access private
22
	 * @var \Wordlift_Log_Service $log_service The Log service.
23
	 */
24
	private $log_service;
25
26
	/**
27
	 * Create an instance of the ShareThis service.
28
	 *
29
	 * @since 3.2.0
30
	 */
31
	public function __construct() {
32
33
		$this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_ShareThis_Service' );
34
35
	}
36
37
	/**
38
	 * Receive <em>the_content</em> filter calls from WordPress.
39
	 *
40
	 * @since 3.2.0
41
	 *
42
	 * @param string $content The post content.
43
	 *
44
	 * @return string The updated post content.
45
	 */
46
	public function the_content( $content ) {
47
48
		return $this->call_sharethis( 'the_content', $content );
49
	}
50
51
	/**
52
	 * Receive <em>the_excerpt</em> filter calls from WordPress.
53
	 *
54
	 * @since 3.2.0
55
	 *
56
	 * @param string $content The post excerpt.
57
	 *
58
	 * @return string The updated post excerpt.
59
	 */
60
	public function the_excerpt( $content ) {
61
62
		return $this->call_sharethis( 'the_excerpt', $content );
63
	}
64
65
	/**
66
	 * Call the ShareThis function.
67
	 *
68
	 * @since 3.2.0
69
	 *
70
	 * @param string $tag The filter tag.
71
	 * @param string $content The post content.
72
	 *
73
	 * @return string The updated post content.
74
	 */
75
	private function call_sharethis( $tag, $content ) {
76
77
		// Get the current post.
78
		global $post;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
79
80
		// If it's not the entity type, return.
81
		if ( Wordlift_Entity_Service::TYPE_NAME !== $post->post_type ) {
82
			return $content;
83
		}
84
85
		// If the ShareThis function doesn't exist, return.
86
		if ( ! function_exists( self::ADD_WIDGET_FUNCTION_NAME ) ) {
87
			return $content;
88
		}
89
90
		// If ShareThis hasn't been added as a filter, return.
91
		if ( ! has_filter( $tag, self::ADD_WIDGET_FUNCTION_NAME ) ) {
92
			return $content;
93
		}
94
95
		// Temporary pop the post type and replace it with post.
96
		$post_type       = $post->post_type;
97
		$post->post_type = 'post';
98
99
		// Call ShareThis (disguised as a post).
100
		$content = call_user_func_array( self::ADD_WIDGET_FUNCTION_NAME, array( $content ) );
101
102
		// Restore our post type.
103
		$post->post_type = $post_type;
104
105
		// Finally return the content.
106
		return $content;
107
	}
108
109
}
110