/
simple-related-posts.php
155 lines (130 loc) · 4.48 KB
/
simple-related-posts.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
<?php
/*
Plugin Name: WP Simple Related Posts
Plugin URI: https://github.com/megumiteam/wp-simple-related-posts
Description: Display Related Posts. Very Simple.
Author: digitalcube
Version: 1.6.1
Author URI: https://github.com/megumiteam/wp-simple-related-posts
Text Domain: simple-related-posts
Domain Path: /languages/
Copyright 2018 - 2021 degitalcube (email : info@digitalcube)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if ( ! defined( 'SIRP_DOMAIN' ) )
define( 'SIRP_DOMAIN', 'wp-simple-related-posts' );
if ( ! defined( 'SIRP_PLUGIN_URL' ) )
define( 'SIRP_PLUGIN_URL', plugins_url() . '/' . dirname( plugin_basename( __FILE__ ) ));
if ( ! defined( 'SIRP_PLUGIN_DIR' ) )
define( 'SIRP_PLUGIN_DIR', WP_PLUGIN_DIR . '/' . dirname( plugin_basename( __FILE__ ) ));
load_plugin_textdomain( SIRP_DOMAIN, false, dirname(plugin_basename(__FILE__)) . '/languages' );
add_action( 'admin_init', 'simple_related_posts_install' );
function simple_related_posts_install() {
$css = <<<EOM
.simple-related-posts {
list-style: none;
}
.simple-related-posts * {
margin:0;
padding:0;
}
.simple-related-posts li {
margin-bottom: 10px;
}
.simple-related-posts li a {
display: block;
}
.simple-related-posts li a p {
display: table-cell;
vertical-align: top;
}
.simple-related-posts li .thumb {
padding-right: 10px;
}
EOM;
if ( !get_option('simple_related_posts_installed') ) {
$args = array(
'target' => 'Simple_Related_Posts_Tag',
'display_num' => 5,
'post_content' => 1,
'post_thumbnail' => 1,
'title' => __('Related Posts', SIRP_DOMAIN ),
'original_css' => 1,
'original_css_content' => $css,
'rss_post_content' => ''
);
update_option('sirp_options', $args);
update_option('simple_related_posts_installed', 1);
} else {
$option = get_option('sirp_options');
if ( !array_key_exists( 'original_css_content', $option ) ) {
$option['original_css_content'] = $css;
update_option('sirp_options', $option);
}
}
}
class Simple_Related_Posts {
private $related = '';
public function __construct() {
$this->requirements();
$option = get_option('sirp_options');
if ( class_exists($option['target']) ) {
$this->related = new $option['target'];
}
add_filter( 'wp_insert_post_data', [ $this, 'save_simple_related_posts' ], 10, 2 );
}
private function requirements() {
require_once(dirname( __FILE__ ) . '/modules/base.php');
$sirp_dirs = array(
dirname( __FILE__ ) . '/admin/',
dirname( __FILE__ ) . '/modules/'
);
foreach ( $sirp_dirs as $dir ) {
opendir($dir);
while(($ent = readdir()) !== false) {
if(!is_dir($ent) && strtolower(substr($ent,-4)) == ".php") {
require_once($dir.$ent);
}
}
closedir();
}
do_action( 'sirp_addon_requirement' );
}
public function get_data_original($num = '') {
return $this->related->get_data_original($num);
}
public function get_data($num = '') {
return $this->related->get_data($num);
}
public function get_data_api( $num = '', $post_id = null ) {
return $this->related->get_data_api($num, $post_id);
}
public function save_simple_related_posts( $data, $postarr ) {
if ( 'draft' === $data['post_status'] && isset( $postarr['simple_related_posts'] ) ) {
$related_posts = $postarr['simple_related_posts'];
update_post_meta( $postarr['ID'], 'simple_related_posts', $related_posts );
}
return $data;
}
}
$simple_related_posts = new Simple_Related_Posts();
require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
if ( is_plugin_active( 'json-rest-api/plugin.php' ) && ( '3.9.2' <= get_bloginfo( 'version' ) ) ) {
require_once( dirname( __FILE__ ) . '/lib/wp-rest-api.php' );
function sirp_json_api_related_filters( $server ) {
// Related
$wp_json_related = new WP_JSON_SIRP( $server );
add_filter( 'json_endpoints', array( $wp_json_related, 'register_routes' ), 1 );
}
add_action( 'wp_json_server_before_serve', 'sirp_json_api_related_filters', 10, 1 );
}