| 1 |
<?php |
| 2 |
namespace MBSocial; |
| 3 |
defined('ABSPATH') or die('No direct access permitted'); |
| 4 |
|
| 5 |
// Helper for admin pages / fields / saving etc. |
| 6 |
|
| 7 |
class admin |
| 8 |
{ |
| 9 |
protected $fields = array(); |
| 10 |
protected $defined_fields = array(); |
| 11 |
protected $defined_updatable = array(); |
| 12 |
protected static $namespace; |
| 13 |
|
| 14 |
public function __construct() |
| 15 |
{ |
| 16 |
self::$namespace = __NAMESPACE__ . '\\'; // PHP 5.3 |
| 17 |
|
| 18 |
} |
| 19 |
|
| 20 |
// add a maxfield to be displayed on the admin. |
| 21 |
public function addfield( $field, $start = '', $end = '') |
| 22 |
{ |
| 23 |
$field_id = isset($field->id) ? $field->id : $field->template . \rand(0,1000); |
| 24 |
|
| 25 |
$field->publish = false; // Output fields via class - never output. |
| 26 |
|
| 27 |
$this->fields[$field_id] = array('field' => $field, |
| 28 |
'start' => $start, |
| 29 |
'end' => $end); |
| 30 |
$this->fields = apply_filters('mbsocial/editor/addfield', $this->fields, $field); |
| 31 |
|
| 32 |
$this->defined_fields[] = $field_id; |
| 33 |
|
| 34 |
} |
| 35 |
|
| 36 |
public function addUpdate($field) |
| 37 |
{ |
| 38 |
$field_id = $field->id; |
| 39 |
$this->defined_updatable[] = $field_id; |
| 40 |
} |
| 41 |
|
| 42 |
public function namespaceit($var) |
| 43 |
{ |
| 44 |
return self::$namespace . $var; |
| 45 |
} |
| 46 |
|
| 47 |
public function display_fields($clean = true, $return = false) |
| 48 |
{ |
| 49 |
$fields = apply_filters('mbsocial/display_fields', $this->fields); |
| 50 |
$output = ''; |
| 51 |
|
| 52 |
|
| 53 |
foreach($fields as $id => $item) |
| 54 |
{ |
| 55 |
$field = $item['field']; |
| 56 |
$output .= $field->output($item['start'], $item['end']); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
// auto-update system |
| 62 |
$updatable = $this->defined_updatable; |
| 63 |
foreach($updatable as $index => $field) |
| 64 |
{ |
| 65 |
$updatable[$index] = '#' . $field; |
| 66 |
} |
| 67 |
|
| 68 |
$output .= ' <span class="updatables hidden">' . implode(',', $updatable) . '</span>'; |
| 69 |
|
| 70 |
if ($clean) |
| 71 |
{ |
| 72 |
$this->fields = array(); |
| 73 |
$this->defined_updatable = array(); |
| 74 |
} |
| 75 |
|
| 76 |
if (! $return) |
| 77 |
echo $output; |
| 78 |
else |
| 79 |
return $output; |
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
public function output_defined_fields() |
| 84 |
{ |
| 85 |
$fields = $this->defined_fields; |
| 86 |
$fields = implode(',', $fields); |
| 87 |
|
| 88 |
echo '<input type="hidden" name="defined_fields" value="' . $fields . '">'; |
| 89 |
} |
| 90 |
|
| 91 |
/** @param $active_networks - Networks that were saved by the users. This should be loaded instead of the default ones **/ |
| 92 |
public function get_default_networks($active_networks = array() ) |
| 93 |
{ |
| 94 |
$networks = mbSocial()->networks()->get(); |
| 95 |
|
| 96 |
$selected = array(); |
| 97 |
$unselected = array(); |
| 98 |
$readmore = array(); |
| 99 |
|
| 100 |
foreach($networks as $index => $network) |
| 101 |
{ |
| 102 |
$priority = $network->get('priority'); |
| 103 |
$is_active = array_search($network->get('network'), $active_networks); // retrieve the array index |
| 104 |
|
| 105 |
if ($is_active !== false) |
| 106 |
$priority = 'selected'; |
| 107 |
|
| 108 |
if ($priority == 'selected') |
| 109 |
$selected[$is_active] = $network; |
| 110 |
if ($priority == 'unselected') |
| 111 |
$unselected[] = $network; |
| 112 |
if ($priority == 'readmore') |
| 113 |
$readmore[] = $network; |
| 114 |
|
| 115 |
|
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
// Sort networks by order saved. |
| 120 |
ksort($selected); |
| 121 |
|
| 122 |
return array('selected' => $selected, 'unselected' => $unselected, 'readmore' => $readmore); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
/* Load saved networks, check which ones are selected. Load others as default settings if no save */ |
| 128 |
public function get_networks($active_networks = array() ) |
| 129 |
{ |
| 130 |
$networks_array = $this->get_default_networks($active_networks); |
| 131 |
return $networks_array; |
| 132 |
} |
| 133 |
|
| 134 |
public function header( $args = array() ) |
| 135 |
{ |
| 136 |
$defaults = array( |
| 137 |
'title' => '', |
| 138 |
); |
| 139 |
|
| 140 |
$args = array_merge($defaults, $args); |
| 141 |
extract($args); |
| 142 |
|
| 143 |
include_once( mbSocial()->get_plugin_path() . '/admin/header.php'); |
| 144 |
} |
| 145 |
|
| 146 |
public function footer() |
| 147 |
{ |
| 148 |
include_once( mbSocial()->get_plugin_path() . '/admin/footer.php'); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
} |
| 154 |
|