Last active
April 13, 2023 15:25
-
-
Save brasofilo/141df41cb1aff0a0dd9bf4036ff6df01 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Move Snippet | |
* Plugin URI: http://wordpress.stackexchange.com/questions/57211 | |
* Description: Uses a custom template in the plugin directory accordding to Conditional Tags (http://codex.wordpress.org/Conditional_Tags), maybe even other conditions | |
* Version: 2023-03-15 | |
* Author: brasofilo | |
* Author URI: http://wordpress.stackexchange.com/users/12615 | |
* Gist Plugin URI: brasofilo/141df41cb1aff0a0dd9bf4036ff6df01 | |
* Gist Plugin URI: https://gist.github.com/brasofilo/141df41cb1aff0a0dd9bf4036ff6df01 | |
*/ | |
B5F_MoveSnippets::init(); | |
/** | |
* https://wordpress.stackexchange.com/questions/55724/plugin-menu-addition-in-multisite | |
*/ | |
class B5F_MoveSnippets { | |
private static $ins = null; | |
public static function instance() { | |
is_null( self::$ins ) && self::$ins = new self; | |
return self::$ins; | |
} | |
public static function init() { | |
add_action( 'plugins_loaded', [self::instance(), 'setup'] ); | |
} | |
public function setup() { | |
if (is_network_admin()) return; | |
add_action('admin_init', [$this, 'adminit']); | |
add_action('admin_menu', [$this, 'menu']); | |
# Adiciona botões no topo | |
add_action('code_snippets/admin/manage/before_list_table', [$this, 'doSnipStuff']); | |
add_action('code_snippets/admin/before_title_input', [$this, 'doSnipStuff']); | |
add_action('admin_footer-snippets_page_snippets-settings', [$this, 'doSnipStuff']); | |
add_action('admin_head-snippets_page_edit-snippet', [$this, 'cleanUp']); | |
add_action('admin_head-trechos-de-codigo_page_edit-snippet', [$this, 'cleanUp']); | |
add_action('admin_head-toplevel_page_snippets', [$this, 'cleanUp']); | |
} | |
public function adminit() { | |
global $menu; | |
$snip = $this->_recursiveSearch('Snippets', $menu); | |
$trechos = $this->_recursiveSearch('Trechos de código', $menu); | |
if ($snip) unset($menu[$snip]); | |
if ($trechos) unset($menu[$trechos]); | |
} | |
public function menu() { | |
add_submenu_page( | |
'tools.php', | |
'Snippets', | |
'<span class="dashicons dashicons-admin-generic" style="opacity:.35"></span> Snippets', | |
'manage_options', | |
'admin.php?page=snippets', | |
'', | |
999 | |
); | |
} | |
public function doSnipStuff (){ | |
$make_menu = [ | |
'sp-todos' => ['Todos os Snippets', admin_url('admin.php?page=snippets')], | |
'sp-config' => ['Configurações', admin_url('admin.php?page=snippets-settings')] | |
]; | |
echo '<div id="the-menu-b5f" style="display:none">'; | |
foreach( $make_menu as $k => $v ){ | |
printf( '<a href="%s" class="page-title-action add-new-h2">%s</a>', $v[1], $v[0] ); | |
} | |
echo '</div>'; | |
echo <<<HTML | |
<script type="text/javascript"> | |
jQuery(document).ready(function($) { | |
$('#the-menu-b5f a').appendTo('.wrap h1'); | |
}); | |
</script> | |
HTML; | |
} | |
public function cleanUp(){ | |
if ( 'edit-snippet' === $_GET['page'] || 'snippets' === $_GET['page'] ) { | |
?> | |
<style> | |
#snippet-type-tabs a[href='https://codesnippets.pro/pricing/'] { | |
display: none !important; | |
} | |
</style> | |
<script type="text/javascript"> | |
jQuery(document).ready( function($) { | |
$('#menu-tools > ul a[href^="admin.php?page=snippets"]').addClass('current').parent().addClass('current'); | |
$('#menu-tools').removeClass('wp-not-current-submenu').addClass('wp-has-current-submenu wp-menu-open'); | |
}); | |
</script> | |
<?php | |
} | |
} | |
private function _recursiveSearch ($needle, $haystack) { | |
foreach ($haystack as $key => $value) { | |
$current_key = $key; | |
if ($needle === $value OR ( | |
is_array($value) | |
&& $this->_recursiveSearch($needle, $value) !== false | |
) | |
) { | |
return $current_key; | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment