forked from palasthotel/grid-avoid-doublets-wordpress
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid_avoid_doublets.php
More file actions
190 lines (166 loc) · 4.11 KB
/
Copy pathgrid_avoid_doublets.php
File metadata and controls
190 lines (166 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
/**
* Plugin Name: Grid Avoid Doublets
* Plugin URI: https://github.com/palasthotel/grid-avoid-doublets-wordpress
* Description: Avoid doublets API while rendering grids
* Version: 1.1.1
* Author: Palasthotel <rezeption@palasthotel.de> (in person: Edward Bock, Enno Welbers)
* Author URI: http://www.palasthotel.de
* License: http://www.gnu.org/licenses/gpl GPLv3
* @copyright Copyright (c) 2014, Palasthotel
* @package Palasthotel\Grid-WordPress-Box-Social
*/
namespace GridAvoidDoublets;
class Plugin{
/**
* array key for not grid specific post ids
*/
const GLOBAL_KEY = "__global__";
/**
* @var array
*/
private $areas;
/**
* GridAvoidDoublets constructor.
*/
public function __construct() {
$this->clear();
add_action("plugins_loaded",array($this, "init"));
}
/**
* init plugin after plugins are loaded
*/
public function init(){
if(defined('\Grid\Constants\Hook::WILL_RENDER_GRID')){
add_action("grid_".\Grid\Constants\Hook::WILL_RENDER_GRID, array($this, "grid_render_before"));
}
}
/**
* fired before a grid renders
* @param $args object
*/
public function grid_render_before($args){
$grid_id = null;
$editmode = true;
if(is_object($args)){
/**
* grid 1.6.12 way
*/
$grid_id = $args->grid->gridid;
$editmode = $args->editmode;
} else if(is_array($args)){
/**
* @deprecated grid 1.6.11 way
*/
$grid_id = $args["grid"];
$editmode = $args["editmode"];
}
if(!$editmode){
$this->clear($grid_id);
}
}
/**
* clears the blacklist
* @param null|string|integer $grid_id
*/
public function clear($grid_id = null){
if( $grid_id == null ){
$this->areas = array();
return;
}
$this->areas[$grid_id] = array();
}
/**
* add an placed contents for example and post id in a grid id
*
* @param integer $content_id unique for area
* @param null|integer|string $area_id id for area
*
*/
public function add_content_id( $content_id, $area_id = null){
/**
* if no grid id set to global index
*/
if( $area_id == null) $area_id = self::GLOBAL_KEY;
/**
* create new entry for grid id
*/
if(!isset($this->areas[$area_id])){
$this->areas[$area_id] = array();
}
/**
* add post id if not already set
*/
if(!in_array( $content_id, $this->areas[$area_id])){
$this->areas[$area_id][] = $content_id;
}
}
/**
* check if post is already placed
* @param integer $content_id
* @param self::GLOBAL_KEY|integer|null $grid_id
*
* @return bool
*/
public function is_placed( $content_id, $area_id = null){
if( $area_id != null){
/**
* have a look in the specific grid
*/
return ( isset($this->areas[$area_id]) && in_array( $content_id, $this->areas[$area_id]));
}
/**
* have a look on all placed posts in all grids
*/
foreach ($this->areas as $_area_id => $_content_id){
if( $_content_id == $content_id) return true;
}
/**
* if nothing found its not placed
*/
return false;
}
/**
* @param null|string|integer $area_id
*
* @return array
*/
public function get_placed_ids($area_id = null){
if($area_id != null) return (isset($this->areas[$area_id]))? $this->areas[$area_id]: array();
$all = array();
foreach ($this->areas as $area_id => $content_ids){
$all = array_merge($all, $content_ids);
}
return $all;
}
}
/**
* initialize
*/
global $grid_avoid_doublets;
$grid_avoid_doublets = new Plugin();
require_once "public-functions.php";
/**
* @param integer $content_id
* @param string | integer $area_id
*/
function grid_avoid_doublets_add($content_id, $area_id = null){
global $grid_avoid_doublets;
$grid_avoid_doublets->add_content_id($content_id, $area_id);
}
/**
* @param integer $content_id
* @param null | string | integer $area_id
*/
function grid_avoid_doublets_is_placed($content_id, $area_id = null){
global $grid_avoid_doublets;
$grid_avoid_doublets->is_placed($content_id, $area_id);
}
/**
* return array of post ids that are already placed
* @return array
*/
function grid_avoid_doublets_get_placed($area_id = null){
global $grid_avoid_doublets;
return $grid_avoid_doublets->get_placed_ids($area_id);
}