Completed
Push — development ( a370cd...036ab5 )
by Atanas
05:00
created

Network_Container::render()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 4
nop 0
dl 9
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Carbon_Fields\Container;
4
5
use Carbon_Fields\Datastore\Datastore;
6
use Carbon_Fields\Exception\Incorrect_Syntax_Exception;
7
8
/**
9
 * Theme options container class.
10
 */
11
class Network_Container extends Theme_Options_Container {
12
	/**
13
	 * ID of the site the container is operating with
14
	 *
15
	 * @see init()
16
	 * @var int
17
	 */
18
	protected $site_id;
19
20
	/**
21
	 * {@inheritDoc}
22
	 */
23
	public function __construct( $id, $title, $type, $condition_collection, $condition_translator ) {
24
		parent::__construct( $id, $title, $type, $condition_collection, $condition_translator );
25
26
		if ( ! is_multisite() ) {
27
			Incorrect_Syntax_Exception::raise( 'The "' . $title . '" container will not be available because your site is not a multisite.' );
28
			return;
29
		}
30
31
		$this->set_datastore( Datastore::make( 'network' ), $this->has_default_datastore() );
32
		$this->set_site_id( SITE_ID_CURRENT_SITE );
33
	}
34
35
	/**
36
	 * {@inheritDoc}
37
	 */
38
	public function init() {
39
		$registered = $this->register_page();
40
		if ( $registered ) {
41
			add_action( 'network_admin_menu', array( $this, '_attach' ) );
42
		}
43
	}
44
45
	/**
46
	 * Check if a site exists by id
47
	 *
48
	 * @param  integer $id
49
	 * @return boolean
50
	 */
51
	protected function site_exists( $id ) {
52
		if ( ! function_exists( 'get_blog_status' ) ) {
53
			return false;
54
		}
55
56
		$blog_domain = get_blog_status( $id, 'domain' );
57
		return ! empty( $blog_domain );
58
	}
59
60
	/**
61
	 * Get the site ID the container is operating with.
62
	 *
63
	 * @return integer
64
	 */
65
	public function get_site_id() {
66
		return $this->site_id;
67
	}
68
69
	/**
70
	 * Set the site ID the container will operate with.
71
	 *
72
	 * @param  int  $id
73
	 * @return self $this
74
	 */
75
	public function set_site_id( $id ) {
76
		$id = intval( $id );
77
78
		if ( ! $this->site_exists( $id ) ) {
79
			Incorrect_Syntax_Exception::raise( 'The specified site id #' . $id . ' does not exist' );
80
			return $this;
81
		}
82
83
		$this->site_id = $id;
84
		$this->datastore->set_object_id( $this->get_site_id() );
85
86
		return $this;
87
	}
88
}
89