| Server IP : 43.141.49.107 / Your IP : 113.219.202.44 Web Server : Apache System : Linux VM-8-5-opencloudos 6.6.34-9.oc9.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Jun 19 19:35:45 CST 2024 x86_64 User : www ( 1000) PHP Version : 8.1.27 Disable Function : passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /www/wwwroot/wordpress/wordpress/wp-content/plugins/wpjam-basic/extends/ |
Upload File : |
<?php
/*
Name: 常用简码
URI: https://blog.wpjam.com/m/wpjam-basic-shortcode/
Description: 添加 email, list, table, bilibili, youku, qqv 等常用简码,并在后台罗列系统的所有可用的简码。
Version: 1.0
*/
class WPJAM_Shortcode{
public static function callback($attr, $content, $tag){
$attr = array_map('esc_attr', (array)$attr);
$content = wp_kses($content, 'post');
if($tag == 'email'){
$attr = shortcode_atts(['mailto'=>false], $attr);
return antispambot($content, $attr['mailto']);
}elseif(in_array($tag, ['bilibili', 'youku', 'tudou', 'qqv', 'sohutv'])){
return WPJAM_Video::render($content, $attr) ?: wp_video_shortcode(array_merge($attr, ['src'=>$content]));
}elseif($tag == 'code'){
$attr = shortcode_atts(['type'=>'php'], $attr);
$type = $attr['type'] == 'html' ? 'markup' : $attr['type'];
$content = str_replace("<br />\n", "\n", $content);
$content = str_replace("</p>\n", "\n\n", $content);
$content = str_replace("\n<p>", "\n", $content);
$content = str_replace('&', '&', esc_textarea($content)); // wptexturize 会再次转化 & => &
$content = trim($content);
return $type ? '<pre><code class="language-'.$type.'">'.$content.'</code></pre>' : '<pre>'.$content.'</pre>';
}elseif($tag == 'list'){
$attr = shortcode_atts(['type'=>'', 'class'=>''], $attr);
$content = str_replace(["\r\n", "<br />\n", "</p>\n", "\n<p>"], "\n", $content);
$output = '';
foreach(explode("\n", $content) as $li){
if($li = trim($li)){
$output .= "<li>".do_shortcode($li)."</li>\n";
}
}
$class = $attr['class'] ? ' class="'.$attr['class'].'"' : '';
$tag = in_array($attr['type'], ['order', 'ol']) ? 'ol' : 'ul';
return '<'.$tag.$class.">\n".$output."</".$tag.">\n";
}elseif($tag == 'table'){
$attr = shortcode_atts([
'border' => '0',
'cellpading' => '0',
'cellspacing' => '0',
'width' => '',
'class' => '',
'caption' => '',
'th' => '0', // 0-无,1-横向,2-纵向,4-横向并且有 footer
], $attr);
$output = $thead = $tbody = '';
$content = str_replace(["\r\n", "<br />\n", "\n<p>", "</p>\n"], ["\n", "\n", "\n", "\n\n"], $content);
if($attr['caption']){
$output .= '<caption>'.$attr['caption'].'</caption>';
}
$th = $attr['th'];
$tr_i = 0;
foreach(explode("\n\n", $content) as $tr){
if($tr = trim($tr)){
$tds = explode("\n", $tr);
if(($th == 1 || $th == 4) && $tr_i == 0){
foreach($tds as $td){
if($td = trim($td)){
$thead .= "\t\t\t".'<th>'.$td.'</th>'."\n";
}
}
$thead = "\t\t".'<tr>'."\n".$thead."\t\t".'</tr>'."\n";
}else{
$tbody .= "\t\t".'<tr>'."\n";
$td_i = 0;
foreach($tds as $td){
if($td = trim($td)){
if($th == 2 && $td_i ==0){
$tbody .= "\t\t\t".'<th>'.$td.'</th>'."\n";
}else{
$tbody .= "\t\t\t".'<td>'.$td.'</td>'."\n";
}
$td_i++;
}
}
$tbody .= "\t\t".'</tr>'."\n";
}
$tr_i++;
}
}
if($th == 1 || $th == 4){ $output .= "\t".'<thead>'."\n".$thead."\t".'</thead>'."\n"; }
if($th == 4){ $output .= "\t".'<tfoot>'."\n".$thead."\t".'</tfoot>'."\n"; }
$output .= "\t".'<tbody>'."\n".$tbody."\t".'</tbody>'."\n";
$attr = wp_array_slice_assoc($attr, ['border', 'cellpading', 'cellspacing', 'width', 'class']);
return wpjam_tag('table', $attr, $output);
}
}
public static function query_items($args){
foreach($GLOBALS['shortcode_tags'] as $tag => $callback){
if(is_array($callback)){
if(is_object($callback[0])){
$callback = '<p>'.get_class($callback[0]).'->'.(string)$callback[1].'</p>';
}else{
$callback = '<p>'.$callback[0].'->'.(string)$callback[1].'</p>';
}
}elseif(is_object($callback)){
$callback = '<pre>'.print_r($callback, true).'</pre>';
}else{
$callback = wpautop($callback);
}
$items[] = ['tag'=>wpautop($tag), 'callback'=>$callback];
}
return $items ?? [];
}
public static function get_actions(){
return [];
}
public static function get_fields($action_key='', $id=0){
return [
'tag' => ['title'=>'简码', 'type'=>'view', 'show_admin_column'=>true],
'callback' => ['title'=>'回调函数', 'type'=>'view', 'show_admin_column'=>true]
];
}
public static function get_list_table(){
return [
'model' => self::class,
'primary_key' => 'tag',
];
}
}
add_shortcode('hide', '__return_empty_string');
add_shortcode('email', ['WPJAM_Shortcode', 'callback']);
add_shortcode('list', ['WPJAM_Shortcode', 'callback']);
add_shortcode('table', ['WPJAM_Shortcode', 'callback']);
add_shortcode('code', ['WPJAM_Shortcode', 'callback']);
add_shortcode('youku', ['WPJAM_Shortcode', 'callback']);
add_shortcode('qqv', ['WPJAM_Shortcode', 'callback']);
add_shortcode('bilibili', ['WPJAM_Shortcode', 'callback']);
add_shortcode('tudou', ['WPJAM_Shortcode', 'callback']);
add_shortcode('sohutv', ['WPJAM_Shortcode', 'callback']);
wpjam_add_menu_page('wpjam-shortcodes', [
'parent' => 'wpjam-basic',
'menu_title' => '常用简码',
'network' => false,
'function' => 'list',
'list_table' => 'WPJAM_Shortcode',
'summary' => __FILE__,
]);