[ad_1]
Hi, I developed a small plugin for documents management. The plugin is not available to the public, it is strictly related to a particular website. The purpose of the plugin is manage documents like they were pages, create a documents archive, make documents searchable in the default search. It worked fine on some WP versions, then, I couldn’t find WHEN, it broke for some reason.
The issues:
1. when I click the name of the user who posted a post, I usually get a list of the posts written by that user: instead, if the plugin is active, I get 404 error;
2. when I click the name of a month in my posts Archive, I generally get a list of any post posted in that particular month: instead, if the plugin is active, I get a 404 error (no error at all IF for some reason there’s my CPTDocument between the matches);
I tried flushing permalinks, with no luck.
htaccess is fine.
I tried using a default theme on another domain, with a completely new WP, with no luck.
So, there’s something broken in my plugin, but I don’t understand what.
Can you help me?
This is ALL the code:
<?php
/*
Plugin Name: MyDocuments
Plugin URI: https://
Description: Manages documents, creates documents archive, make them searchable in the default search
Version: 0.1
Author: PLSI
Author URI: https://
Tags: files, management
License: for what, it doensn't work either
Text Domain: plsi-documents
*/
if (!defined('ABSPATH')) exit;
define( 'DIR_PATH', plugin_dir_path( __FILE__ ) );
define('FORMATS', array(
'pdf' => 290975,
'doc' => 290977,
'docx' => 290977,
'ppt' => 290976,
'pptx' => 290976,
'xls' => 290978,
'xlsx' => 290978,
'multiple_files' => 290974,
'generic_type_A' => 290972,
'generic_type_B' => 290973
));
if ( ! class_exists( 'MyDocuments' ) ) {
class MyDocuments {
public function __construct() {
$this->setup_actions();
}
public function setup_actions() {
register_activation_hook( DIR_PATH, array( 'MyDocuments', 'activate' ) );
register_deactivation_hook( DIR_PATH, array( 'MyDocuments', 'deactivate' ) );
add_action('save_post_docs', [$this, 'save_MyDocument'], 10, 2);
add_action('init', [$this, 'add_MyDocuments_custom_post_type']);
add_filter( 'post_updated_messages', function($messages) {
global $post, $post_ID;
$link = esc_url( get_permalink($post_ID) );
$messages['docs'] = array(
0 => '',
1 => sprintf( __('Document updated. <a href="%s">View document</a>'), $link ),
2 => __('Custom field updated.'),
3 => __('Custom field deleted.'),
4 => __('Document updated.'),
5 => isset($_GET['revision']) ? sprintf( __('Document restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
6 => sprintf( __('Document published. <a href="%s">View document</a>'), $link ),
7 => __('Document saved.'),
8 => sprintf( __('Document submitted. <a target="_blank" href="%s" rel="noopener noreferrer">Preview document</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
9 => sprintf( __('Document scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s" rel="noopener noreferrer">Preview document</a>'), date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), $link ),
10 => sprintf( __('Document draft updated. <a target="_blank" href="%s" rel="noopener noreferrer">Preview document</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
);
return $messages;
});
add_filter( 'bulk_post_updated_messages', function( $bulk_messages, $bulk_counts ) {
$bulk_messages['docs'] = array(
'updated' => _n( "%s document updated.", "%s documents updated.", $bulk_counts["updated"] ),
'locked' => _n( "%s document not updated, somebody is editing it.", "%s documents not updated, somebody is editing them.", $bulk_counts["locked"] ),
'deleted' => _n( "%s document permanently deleted.", "%s documents permanently deleted.", $bulk_counts["deleted"] ),
'trashed' => _n( "%s document moved to the Trash.", "%s documents moved to the Trash.", $bulk_counts["trashed"] ),
'untrashed' => _n( "%s document restored from the Trash.", "%s documents restored from the Trash.", $bulk_counts["untrashed"] ),
);
return $bulk_messages;
}, 10, 2 );
// help
add_action('admin_head', function() {
$screen = get_current_screen();
if(($screen->action == 'add' || $screen->action == '') && $screen->id == 'docs')
{
$args = [
'id' => 'docs_help',
'title' => 'Document help',
'content' => '<h3>Help</h3>help text
',
];
$screen->add_help_tab($args);
}
});
add_action('pre_get_posts', function ($query) {
if (!is_admin() && $query->is_main_query() && empty($query->query_vars['suppress_filters'])) {
if (is_archive()) {
if (is_category()) {
$query->set('post_type', ['post', 'docs']);
} else {
if (!is_tag()) {
$query->set('post_type', 'docs');
} else {
$query->set('post_type', 'post');
}
}
}
}
});
}
public static function activate() {
//flush_rewrite_rules( false );
}
public static function deactivate() {
//flush_rewrite_rules( false );
}
function set_ui_labels($singular = 'Post', $plural = 'Posts') {
$p_lower = strtolower($plural);
$s_lower = strtolower($singular);
return [
'name' => $plural,
'singular_name' => $singular,
'menu_name' => $plural,
'add_new_item' => "New $singular",
'edit_item' => "Edit $singular",
'view_item' => "View $singular",
'view_items' => "View $plural",
'search_items' => "Search $plural",
'not_found' => "No $p_lower found",
'not_found_in_trash' => "No $p_lower found in trash",
'parent_item_colon' => "Parent $singular",
'all_items' => "All $plural",
'archives' => "$singular Archives",
'attributes' => "$singular Attributes",
'insert_into_item' => "Insert into $s_lower",
'uploaded_to_this_item' => "Uploaded to this $s_lower",
];
}
function add_MyDocuments_custom_post_type()
{
unregister_post_type('docs');
$labels = $this->set_ui_labels('Document', 'Documents');
register_post_type('docs', array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'has_archive' => true,
'menu_icon' => 'dashicons-text-page',
'exclude_from_search' => false,
'taxonomies' => ['category'],
'rewrite' => array('slug' => 'docs'),
'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'custom-fields', 'revisions'),
)
);
}
function save_MyDocument( $post_id, $post ) {
if (isset($post->post_status) && $post->post_status === 'auto-draft') {
return;
}
$u = $post->post_content;
if($u === ''){return;}
$matches = [];
$DOM = new DOMDocument();
$DOM->loadHTML($u);
$a = $DOM->getElementsByTagName('a');
foreach($a as $link){
$matches[]=$link->getAttribute('href');
}
$matches = array_unique($matches);
$matches_count = count($matches);
if($matches_count > 0) {
if($matches_count == 1){
$file_extension = wp_check_filetype($matches[0])['ext'];
if($file_extension){
if(array_key_exists($file_extension, FORMATS)){
set_post_thumbnail($post, FORMATS[$file_extension]);
}else{
set_post_thumbnail($post, FORMATS['generic_type_A']);
}
}else{
set_post_thumbnail($post, FORMATS['generic_type_B']);
}
}else{
set_post_thumbnail($post, FORMATS['multiple_files']);
}
}else{
delete_post_thumbnail($post);
}
}
}
$wp_plugin_template = new MyDocuments();
}