-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpost-worktime-logger.php
executable file
·335 lines (286 loc) · 8.96 KB
/
post-worktime-logger.php
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<?php
/**
*
* @copyright Patrick Hausmann
* @author Patrick Hausmann <privat@patrck-designs.de>
*/
/*
Plugin Name: Post Worktime Logger
Plugin URI: https://wordpress.org/plugins/post-worktime-logger/
Description: A plugin to track the worktime for each post.
Version: 1.5.3
Author: Patrick Hausmann
Author URI: https://profiles.wordpress.org/filme-blog/
License: GPLv3
Text Domain: post-worktime-logger
*/
const PWL_VERSION = "1.5.2";
if ( !defined('PLUGINDIR') )
define( 'PLUGINDIR', 'wp-content/plugins' );
include_once (__DIR__."/widget.php");
include_once (__DIR__."/settings.php");
const PWL_NAME = "post-worktime-logger";
const PWL_TEXT_DOMAIN = "post-worktime-logger";
$pwlOptions = get_option("post-worktime-logger-options");
/**
* Handles the ping from frontend to track the worktime.
*/
function pwlHandleWorktimePing()
{
if (is_user_logged_in() && isset($_POST['currentPostId']) && isset($_POST['worktimeToAdd']))
{
$postId = $_POST['currentPostId'];
$worktimeToAdd = $_POST['worktimeToAdd'];
if (is_numeric($postId) && is_numeric($worktimeToAdd))
{
$post = get_post($postId);
if ($post)
{
$oldWorktime = get_post_meta($postId, "post-worktime", true);
if ($oldWorktime===false) $oldWorktime = 0;
$newWorktime = $oldWorktime+$worktimeToAdd;
update_post_meta($postId, "post-worktime", $newWorktime);
}
}
}
}
/**
* Converts seconds to a human readable string.
*
* @param int $seconds the seconds to convert.
*
* @return string the human readable string.
*/
function pwlSecondsToHumanReadableTime($seconds)
{
$dtF = new \DateTime('@0');
$dtT = new \DateTime("@$seconds");
return $dtF->diff($dtT)->format('%H:%I:%S');
}
/**
* Renders the post meta box.
*/
function pwlRenderMetaBoxSummary()
{
$content = "";
if (isset($_GET['post']))
{
$postId = $_GET['post'];
if (is_numeric($postId))
{
$post = get_post($postId);
if ($post)
{
$currentPostId = $_GET['post'];
$worktime = get_post_meta($currentPostId, "post-worktime", true);
if (!$worktime)
{
update_post_meta( $currentPostId, "post-worktime", 0 );
$worktime = $worktime = get_post_meta($currentPostId, "post-worktime", true);
}
$content.=pwlGetPostWorktimeLoggerControlBox($worktime, $currentPostId);
}
}
}
else $content = __("You have to save the post, before we can track the time.", PWL_TEXT_DOMAIN);
echo $content;
}
/**
* Creates the control box for post worktime logger.
*
* @param int $_totalWorktime the total worktime.
* @param int $_postId the post id.
* @return string the html code for the control box.
*/
function pwlGetPostWorktimeLoggerControlBox($_totalWorktime, $_postId)
{
$content = "";
$content.= "<span style=\"display:none;\" id=\"post-worktime-logger-current-post-id\">".$_postId."</span>";
$content .= __('Current worktime', PWL_NAME).': <span id="frontendTime">0</span><br />';
$content .= __("Total worktime", PWL_NAME).': <span id="serverWorktime">';
$content .= pwlSecondsToHumanReadableTime($_totalWorktime);
$content .= '</span><br />';
if (isControlBoxEnabled())
{
$content .= '<button class="button button-small pwl-button" id="pwl-pause-button">'.__("Pause", PWL_TEXT_DOMAIN).'</button>';
$content .= '<button class="button button-small pwl-button" style="display:none;" id="pwl-resume-button">'.__("Resume", PWL_TEXT_DOMAIN).'</button>';
$content .= '<button class="button button-small pwl-button" id="pwl-reset-button">'.__("Reset", PWL_TEXT_DOMAIN).'</button>';
}
return $content;
}
/**
* Adds the meta box to post editor page.
*/
function pwlAddMetaBoxSummary()
{
add_meta_box(
'post-worktime-logger-meta-box',
__( 'Post Worktime', PWL_TEXT_DOMAIN),
'pwlRenderMetaBoxSummary'
);
}
/**
* Creates the column header.
*
* @param $_columns
* @return array
*/
function pwPostsPageHeader($_columns)
{
return array_merge( $_columns,
array('pwlworktimecolumn' => __('Worktime', PWL_TEXT_DOMAIN)) );
}
/**
* Renders the worktime of the current post.
* @param $_column
* @param $post_id
*/
function pwlWorktimeColumnRenderer($_column, $post_id)
{
if ($_column == "pwlworktimecolumn")
{
$worktime = get_post_meta($post_id, "post-worktime", true);
if ($worktime)
{
echo pwlSecondsToHumanReadableTime($worktime);
}
else echo "00:00:00";
}
}
/**
* Makes our column sortable.
*
* @param $_sortableColumns
* @return array
*/
function pwlSortableColumn( $_sortableColumns )
{
$_sortableColumns[ 'pwlworktimecolumn' ] = 'post-worktime';
return $_sortableColumns;
}
/**
* Sorting function.
*
* @param $_vars
* @return array
*/
function pwlWorktimeOrderBy( $_vars )
{
if (isset($_vars['orderby']) && 'post-worktime' == $_vars['orderby']) {
$_vars = array_merge($_vars, array(
'meta_key' => 'post-worktime',
'orderby' => 'meta_value_num'
));
}
return $_vars;
}
/**
* Clear the work time.
*/
function pwlHandleWorktimeReset()
{
if (is_user_logged_in())
{
$postId = $_POST['currentPostId'];
if (is_numeric($postId))
{
$post = get_post($postId);
if ($post)
{
update_post_meta($postId, "post-worktime", 0);
}
}
update_option("post-worktime-logger-last-ping-timestamp", time());
}
}
/**
* Checks if the control box is enabled and returns true, otherwise false.
*
* @return bool
*/
function isControlBoxEnabled()
{
global $pwlOptions;
if (isset($pwlOptions["enableControlButtons"]) && $pwlOptions["enableControlButtons"]=="on")
{
return true;
}
else return false;
}
/**
* Checks if we should disable the auto start timer, depending on the options and the state of our post.
*/
function isAutoStartDisabled()
{
global $pwlOptions;
if (!empty( $pwlOptions['disableAutoStart']))
{
return true;
}
else if (!empty( $pwlOptions['disableAutoStartPublishedPosts']) && get_post_status() == "publish")
{
return true;
}
else return false;
}
//Register post meta box
add_action( 'add_meta_boxes', 'pwlAddMetaBoxSummary');
//Register Ajax Ping from frontend
add_action( 'wp_ajax_worktime_ping', 'pwlHandleWorktimePing');
add_action( 'wp_ajax_worktime_reset', 'pwlHandleWorktimeReset');
add_action( 'init', function () {
$domain = PWL_TEXT_DOMAIN;
$locale = apply_filters( 'plugin_locale', get_locale(), $domain );
if ( $loaded = load_textdomain( $domain, trailingslashit( WP_LANG_DIR ) . $domain . '/' . $domain . '-' . $locale . '.mo' ) )
{
return $loaded;
}
else
{
load_plugin_textdomain( $domain, FALSE, basename( dirname( __FILE__ ) ) . '/lang/' );
}
});
//Register admin javascript file
add_action("admin_enqueue_scripts", function ($hook) {
if ($hook=="toplevel_page_post-worktime-logger-statistics"){
wp_enqueue_script(PWL_NAME, plugins_url( "resources/js/Chart.bundle.min.js", __FILE__ ));
}
global $pwlOptions;
wp_enqueue_style(PWL_NAME, plugins_url( "resources/css/post-worktime-logger.css", __FILE__ ));
if ($hook=="post.php")
{
wp_enqueue_script(PWL_NAME, plugins_url( "resources/js/post-worktime-logger.js", __FILE__ ));
wp_localize_script( PWL_NAME, 'pwl', array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'inactivityTimeout' => ( ! empty( $pwlOptions['inactivityTimeout'] ) ) ? esc_html( $pwlOptions['inactivityTimeout'] ) : '5',
'disableAutoStart' => ( isAutoStartDisabled() ) ? 'on' : '0',
) );
}
});
//Register scripts for frontend
add_action("wp_enqueue_scripts", function () {
wp_enqueue_style(PWL_NAME, plugins_url( "resources/css/post-worktime-logger.css", __FILE__ ));
if (is_user_logged_in())
{//only include js if the user is logged in.
wp_enqueue_script(PWL_NAME, plugins_url( "resources/js/post-worktime-logger.js", __FILE__ ), array("jquery"));
wp_localize_script( PWL_NAME, 'pwl', array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'inactivityTimeout' => ( ! empty( $pwlOptions['inactivityTimeout'] ) ) ? esc_html( $pwlOptions['inactivityTimeout'] ) : '5',
'disableAutoStart' => ( isAutoStartDisabled() ) ? 'on' : '0',
) );
}
});
add_filter( 'request', 'pwlWorktimeOrderBy' );
add_filter( 'manage_edit-post_sortable_columns', 'pwlSortableColumn' );
// For registering the column
add_filter( 'manage_posts_columns', 'pwPostsPageHeader' );
// For rendering the column
add_action( 'manage_posts_custom_column', 'pwlWorktimeColumnRenderer', 10, 2 );
//Register frontend widget
add_action('widgets_init', function(){
register_widget('PwlFrontendWidget');
});
if(is_admin())
{
new PostWorktimeLoggerSettingsPage();
}