-
Notifications
You must be signed in to change notification settings - Fork 3
/
plugin.php
104 lines (85 loc) · 3.1 KB
/
plugin.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
<?php
/**
* Plugin Name: DustPress Comments
* Plugin URI: https://github.com/devgeniem/dustpress-comments
* Description: Comments Helper for DustPress - A WordPress plugin that adds a DustPress helper enabling ajaxified commenting.
* Version: 1.2.0
* Author: Geniem Oy / Ville Siltala
* Author URI: http://www.geniem.com
* Text Domain: dustpress-comments
*/
namespace DustPress;
use get_file_data;
add_action( 'after_setup_theme', __NAMESPACE__ . '\init_comments_helper' );
/**
* Init the helper.
*/
function init_comments_helper() {
// Stop immediately if Dustpress is not enabled
if ( ! class_exists( '\DustPress\Helper' ) ) { return; }
// Require the class file
require_once( dirname( __FILE__ ) . '/classes/comments.php' );
// Get the current plugin version
$plugin_version = get_file_data( __FILE__, [ 'Version' ], 'plugin' );
// Instantiate the class
$comments = new Comments( $plugin_version );
// Add into the helpers array
dustpress()->add_helper( 'comments', $comments );
// Add templates into DustPress
add_filter( 'dustpress/partials', __NAMESPACE__ . '\add_comments_templates' );
/**
* Hooks for comment posting and paginating
*/
$ajaxing = filter_input( INPUT_POST, 'dustpress_comments_ajax', FILTER_SANITIZE_NUMBER_INT );
if ( $ajaxing ) {
/**
* Notify others
*/
if ( ! defined( 'DUSTPRESS_AJAX' ) ) {
define( 'DUSTPRESS_AJAX', true );
}
/**
* Add a hook for handling comment posting
*
* The priority ensures this is run last because it ends the script execution.
*/
add_action( 'comment_post', [ $comments, 'comment_posted' ], PHP_INT_MAX );
/**
* Handle WP comment errors
*/
add_filter( 'wp_die_handler', [ $comments, 'get_error_handler' ] );
add_filter( 'wp_die_ajax_handler', [ $comments, 'get_error_handler' ] );
/**
* Run pagination
*/
$paginate = filter_input( INPUT_POST, 'dustpress_comments_paginate', FILTER_SANITIZE_NUMBER_INT );
if ( $paginate ) {
add_action( 'wp_ajax_dustpress_comments_paginate', [ $comments, 'paginate' ] );
add_action( 'wp_ajax_nopriv_dustpress_comments_paginate', [ $comments, 'paginate' ] );
}
}
/**
* Handle pagination
*/
$paginating = filter_input( INPUT_POST, 'dustpress_comments_paginate', FILTER_SANITIZE_NUMBER_INT );
if ( $paginating ) {
$page = filter_input( INPUT_POST, 'page', FILTER_SANITIZE_NUMBER_INT );
$offset = filter_input( INPUT_POST, 'offset', FILTER_SANITIZE_NUMBER_INT );
$comments->paginate();
}
}
/**
* Add plugin template directory
*/
function add_comments_templates( $templatepaths ) {
array_push( $templatepaths, dirname( __FILE__ ) . '/partials' );
return $templatepaths;
}
/**
* Plugin text-domain
* @return no return
*/
add_action( 'init', __NAMESPACE__ . '\comments_textdomain' );
function comments_textdomain() {
load_plugin_textdomain( 'dustpress-comments', false, 'dustpress-comments/languages' );
}