Completed
Pull Request — develop (#1403)
by Zack
20:05
created

GravityView_Powered_By::maybe_add_link()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
1
<?php
2
3
4
/** If this file is called directly, abort. */
5
if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
6
	die();
7
}
8
9
/**
10
 * Add a Powered By link below Views.
11
 *
12
 * @since 2.5.3
13
 *
14
 */
15
class GravityView_Powered_By {
16
17
	const url = 'https://gravityview.co/powered-by/';
18
19
	/**
20
	 * GravityView_Powered_By constructor.
21
	 */
22
	public function __construct() {
23
		add_action( 'gravityview/template/after', array( $this, 'maybe_add_link' ) );
24
	}
25
26
	/**
27
	 * Prints a HTML link to GravityView's site if "Powered By" GravityView setting is enabled
28
	 *
29
	 * @return void
30
	 */
31
	public function maybe_add_link() {
32
33
		$powered_by = gravityview()->plugin->settings->get( 'powered_by', '0' );
34
35
		if( empty( $powered_by ) ) {
36
			return;
37
		}
38
39
		$url = $this->get_url();
40
41
		// Allow disabling link via URL filter
42
		if ( empty( $url ) ) {
43
			return;
44
		}
45
46
		/**
47
		 * @filter `gravityview/powered_by/text` Modify the anchor text for the Powered By link
48
		 * @param string $anchor_text Anchor text for the Powered By link. Default: "Powered by GravityView". Will be sanitized before display.
49
		 */
50
		$anchor_text = apply_filters( 'gravityview/powered_by/text', __( 'Powered by GravityView', 'gravityview' ) );
51
52
		printf( '<span class="gv-powered-by"><a href="%s">%s</a></span>', esc_url( $url ), esc_html( $anchor_text ) );
53
	}
54
55
	/**
56
	 * Returns the URL to GravityView
57
	 *
58
	 * @return string URL to GravityView (not sanitized)
59
	 */
60
	protected function get_url() {
61
62
		$url = sprintf( self::url, get_bloginfo('name' ) );
63
64
		$affiliate_id = gravityview()->plugin->settings->get( 'affiliate_id', '' );
65
66
		if( $affiliate_id && is_numeric( $affiliate_id ) ) {
67
			$url = add_query_arg( array( 'ref' => $affiliate_id ), $url );
68
		}
69
70
		$url = add_query_arg( array(
71
			'utm_source' => 'powered_by',
72
            'utm_term' => get_bloginfo('name' ),
73
		), $url );
74
75
		/**
76
		 * @filter `gravityview/powered_by/url` Modify the URL returned by the Powered By link
77
		 * @param $url string The URL passed to the Powered By link
78
		 */
79
		$url = apply_filters( 'gravityview/powered_by/url', $url );
80
81
		return $url;
82
	}
83
}
84
85
new GravityView_Powered_By();
86