Commit inicial powerservice

This commit is contained in:
Gabriel
2026-10-19 12:45:45 -06:00
commit ad85486fe2
10158 changed files with 2736804 additions and 0 deletions
@@ -0,0 +1,33 @@
<?php
/**
* Importer for Code Snippets Pro.
*
* @package WPCode.
*/
/**
* Class WPCode_Importer_Code_Snippets_Pro.
*/
class WPCode_Importer_Code_Snippets_Pro extends WPCode_Importer_Code_Snippets {
/**
* The plugin name.
*
* @var string
*/
public $name = 'Code Snippets Pro';
/**
* Importer slug.
*
* @var string
*/
public $slug = 'code-snippets-pro';
/**
* Plugin path.
*
* @var string
*/
public $path = 'code-snippets-pro/code-snippets.php';
}
@@ -0,0 +1,194 @@
<?php
/**
* Importer for Code Snippets.
*
* @package WPCode.
*/
/**
* Class WPCode_Importer_Code_Snippets.
*/
class WPCode_Importer_Code_Snippets extends WPCode_Importer_Type {
/**
* The plugin name.
*
* @var string
*/
public $name = 'Code Snippets';
/**
* Importer slug.
*
* @var string
*/
public $slug = 'code-snippets';
/**
* Plugin path.
*
* @var string
*/
public $path = 'code-snippets/code-snippets.php';
/**
* Get an array of snippets for this plugin.
*
* @return array
*/
public function get_snippets() {
$snippets = array();
if ( ! $this->is_active() ) {
return $snippets;
}
if ( ! function_exists( '\Code_Snippets\get_snippets' ) ) {
return $snippets;
}
$code_snippets = \Code_Snippets\get_snippets();
foreach ( $code_snippets as $code_snippet ) {
/**
* The Code Snippets Snippet object.
*
* @var \Code_Snippets\Snippet $code_snippet
*/
if ( '' === $code_snippet->name ) {
$code_snippet->name = '(no title)';
}
$snippets[ $code_snippet->id ] = $code_snippet->name;
}
return $snippets;
}
/**
* Import the snippet data.
*
* @return void
*/
public function import_snippet() {
// Run a security check.
check_ajax_referer( 'wpcode_admin' );
if ( ! current_user_can( 'wpcode_edit_snippets' ) ) {
wp_send_json_error();
}
if ( ! function_exists( '\Code_Snippets\get_snippets' ) ) {
wp_send_json_error();
}
$id = isset( $_POST['snippet_id'] ) ? absint( $_POST['snippet_id'] ) : 0;
// Grab a snippet from Code Snippets.
$snippets = \Code_Snippets\get_snippets( array( $id ) );
if ( empty( $snippets ) || empty( $snippets[0] ) ) {
wp_send_json_error(
array(
'error' => true,
'name' => esc_html__( 'Unknown Snippet', 'insert-headers-and-footers' ),
'msg' => esc_html__( 'The snippet you are trying to import does not exist.', 'insert-headers-and-footers' ),
)
);
}
// If we got so far we have a snippet to process.
$snippet = $snippets[0];
// Create a new snippet from the snippet data array.
$new_snippet = new WPCode_Snippet( $this->get_snippet_data( $snippet ) );
$new_snippet->save();
if ( ! empty( $new_snippet->get_id() ) ) {
$title = $new_snippet->get_title();
wp_send_json_success(
array(
'name' => '' !== $title ? $title : '(no title)',
'edit' => esc_url_raw(
add_query_arg(
array(
'page' => 'wpcode-snippet-manager',
'snippet_id' => $new_snippet->get_id(),
),
admin_url( 'admin.php' )
)
),
)
);
}
}
/**
* Convert a "Code Snippets" snippet to the format for a WPCode snippet.
*
* @param \Code_Snippets\Snippet $snippet The snippet object.
*
* @return array
*/
public function get_snippet_data( $snippet ) {
$code_type = $this->get_snippet_type( $snippet );
$auto_insert = 1;
switch ( $snippet->scope ) {
case 'admin':
$location = 'admin_only';
break;
case 'front-end':
$location = 'frontend_only';
break;
default:
$location = 'everywhere';
}
if ( 'php' !== $code_type ) {
$location = '';
$auto_insert = 0;
}
$tags = is_array( $snippet->tags ) ? $snippet->tags : explode( ',', $snippet->tags );
$tags[] = $this->add_imported_tag();
return array(
'code' => wp_slash( $snippet->code ),
'note' => $snippet->desc,
'title' => $snippet->name,
'tags' => implode( ',', $tags ), // Combine all tags into a single string.
'code_type' => $code_type,
'priority' => $snippet->priority,
'location' => $location,
'auto_insert' => $auto_insert,
);
}
/**
* Get the snippet type from the scope as the method in \Code_Snippets\Snippet is private.
*
* @param \Code_Snippets\Snippet $snippet The snippet to check the scope for.
*
* @return string
*/
public function get_snippet_type( $snippet ) {
if ( ! isset( $snippet->scope ) ) {
return 'php';
}
if ( '-css' === substr( $snippet->scope, - 4 ) ) {
return 'css';
}
if ( '-js' === substr( $snippet->scope, - 3 ) ) {
return 'js';
}
if ( 'content' === substr( $snippet->scope, - 7 ) ) {
return 'universal';
}
return 'php';
}
}
@@ -0,0 +1,391 @@
<?php
/**
* Importer for Header Footer Code Manager.
*
* @package WPCode
*/
/**
* Class WPCode_Importer_Header_Footer_Code_Manager
*/
class WPCode_Importer_Header_Footer_Code_Manager extends WPCode_Importer_Type {
/**
* The plugin name.
*
* @var string
*/
public $name = 'Header Footer Code Manager';
/**
* Importer slug.
*
* @var string
*/
public $slug = 'header-footer-code-manager';
/**
* Plugin path.
*
* @var string
*/
public $path = 'header-footer-code-manager/99robots-header-footer-code-manager.php';
/**
* Get an array of snippets from the Header Footer Code Manager table.
*
* Returns an associative array where keys are snippet IDs and values are snippet labels.
*
* @return array
*/
public function get_snippets() {
$snippets = array();
if ( ! class_exists( 'Hfcm_Snippets_List' ) ) {
return $snippets;
}
$results = Hfcm_Snippets_List::get_snippets();
if ( $results ) {
foreach ( $results as $snippet ) {
$label = ! empty( $snippet['name'] ) ? $snippet['name'] : __( '(no title)', 'insert-headers-and-footers' );
$snippets[ $snippet['script_id'] ] = $label;
}
}
return $snippets;
}
/**
* Import the selected snippet.
*
* Expects a POST parameter 'snippet_id' that contains the HFCM snippet ID.
*
* @return void
*/
public function import_snippet() {
// Verify AJAX nonce.
check_ajax_referer( 'wpcode_admin' );
if ( ! current_user_can( 'wpcode_edit_snippets' ) ) {
wp_send_json_error();
}
$id = isset( $_POST['snippet_id'] ) ? absint( $_POST['snippet_id'] ) : 0;
if ( ! $id ) {
wp_send_json_error(
array(
'error' => true,
'msg' => __( 'Invalid snippet ID.', 'insert-headers-and-footers' ),
)
);
}
global $wpdb;
$table_name = $wpdb->prefix . 'hfcm_scripts';
$snippet = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table_name} WHERE script_id = %d", $id ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery
if ( ! $snippet ) {
wp_send_json_error(
array(
'error' => true,
'msg' => __( 'Snippet not found.', 'insert-headers-and-footers' ),
)
);
}
// Convert the HFCM snippet data into the WPCode snippet format.
$snippet_data = $this->get_snippet_data( $snippet );
// Create a new snippet (assumes WPCode_Snippet is defined in your codebase).
$new_snippet = new WPCode_Snippet( $snippet_data );
$new_snippet->save();
if ( $new_snippet->get_id() ) {
$title = $new_snippet->get_title();
wp_send_json_success(
array(
'name' => '' !== $title ? $title : '(no title)',
'edit' => esc_url_raw(
add_query_arg(
array(
'page' => 'wpcode-snippet-manager',
'snippet_id' => $new_snippet->get_id(),
),
admin_url( 'admin.php' )
)
),
)
);
} else {
wp_send_json_error(
array(
'error' => true,
'msg' => __( 'Failed to import snippet.', 'insert-headers-and-footers' ),
)
);
}
}
/**
* Convert a Header Footer Code Manager snippet into a WPCode snippet data array.
*
* @param object $snippet The snippet row from the HFCM table.
* @return array The snippet data array.
*/
public function get_snippet_data( $snippet ) {
// Decode snippet content that is stored with HTML entities.
$code = html_entity_decode( $snippet->snippet );
$title = $snippet->name;
$device_type = $snippet->device_type;
if ( 'both' === $device_type ) {
$device_type = 'any';
}
// Determine code type (default is 'html').
$code_type = 'html';
if ( 'js' === $snippet->snippet_type ) {
$code_type = 'javascript';
} elseif ( 'css' === $snippet->snippet_type ) {
$code_type = 'css';
}
$location = '';
if ( isset( $snippet->location ) ) {
switch ( $snippet->location ) {
case 'header':
$location = 'site_wide_header';
break;
case 'footer':
$location = 'site_wide_footer';
break;
case 'before_content':
case 'after_content':
$location = $snippet->location;
break;
default:
$location = 'site_wide_header';
}
}
// Prepare the snippet data array.
$snippet_data = array(
'code' => wp_slash( $code ),
'title' => $title,
'code_type' => $code_type,
'location' => $location,
'device_type' => $device_type,
'tags' => $this->add_imported_tag(),
'auto_insert' => 1,
'priority' => 10,
);
$display_on = isset( $snippet->display_on ) ? $snippet->display_on : 'All';
switch ( $display_on ) {
case 's_pages':
$pages = json_decode( $snippet->s_pages, true );
if ( is_array( $pages ) && ! empty( $pages ) ) {
$conditional_rules = array(
'show' => 'show',
'groups' => array(
array(
array(
'type' => 'page',
'option' => 'post_id',
'relation' => '=',
'value' => $pages,
),
),
),
);
}
break;
case 's_posts':
$posts = json_decode( $snippet->s_posts, true );
if ( is_array( $posts ) && ! empty( $posts ) ) {
$conditional_rules = array(
'show' => 'show',
'groups' => array(
array(
array(
'type' => 'page',
'option' => 'post_id',
'relation' => '=',
'value' => $posts,
),
),
),
);
}
break;
case 's_categories':
$cats = json_decode( $snippet->s_categories, true );
if ( is_array( $cats ) && ! empty( $cats ) ) {
$conditional_rules = array(
'show' => 'show',
'groups' => array(
array(
array(
'type' => 'page',
'option' => 'taxonomy_term',
'relation' => 'in',
'value' => $cats,
),
),
),
);
}
break;
case 's_custom_posts':
$custom = json_decode( $snippet->s_custom_posts, true );
if ( is_array( $custom ) && ! empty( $custom ) ) {
$groups = array();
foreach ( $custom as $custom_post_type_value ) {
$groups[] = array(
'type' => 'page',
'option' => 'post_type',
'relation' => '=',
'value' => $custom_post_type_value,
);
}
$conditional_rules = array(
'show' => 'show',
'groups' => array( $groups ),
);
}
break;
case 's_tags':
$tags = json_decode( $snippet->s_tags, true );
if ( is_array( $tags ) && ! empty( $tags ) ) {
$conditional_rules = array(
'show' => 'show',
'groups' => array(
array(
array(
'type' => 'page',
'option' => 'taxonomy_term',
'relation' => 'in',
'value' => $tags,
),
),
),
);
}
break;
case 's_is_home':
$conditional_rules = array(
'show' => 'show',
'groups' => array(
array(
array(
'type' => 'page',
'option' => 'type_of_page',
'relation' => '=',
'value' => 'is_front_page',
),
),
),
);
break;
case 's_is_search':
$conditional_rules = array(
'show' => 'show',
'groups' => array(
array(
array(
'type' => 'page',
'option' => 'type_of_page',
'relation' => '=',
'value' => 'is_search',
),
),
),
);
break;
case 's_is_archive':
$conditional_rules = array(
'show' => 'show',
'groups' => array(
array(
array(
'type' => 'page',
'option' => 'type_of_page',
'relation' => '=',
'value' => 'is_archive',
),
),
),
);
break;
case 'latest_posts':
$conditional_rules = array(
'show' => 'show',
'groups' => array(
array(
array(
'type' => 'page',
'option' => 'type_of_page',
'relation' => '=',
'value' => 'is_single',
),
),
),
);
break;
case 'manual':
$conditional_rules = null;
$snippet_data['auto_insert'] = 0;
break;
case 'All':
default:
$ex_pages = json_decode( $snippet->s_pages, true );
$ex_posts = json_decode( $snippet->s_posts, true );
$exclude = array();
if ( is_array( $ex_pages ) && ! empty( $ex_pages ) ) {
$exclude = array_merge( $exclude, $ex_pages );
}
if ( is_array( $ex_posts ) && ! empty( $ex_posts ) ) {
$exclude = array_merge( $exclude, $ex_posts );
}
if ( ! empty( $exclude ) ) {
$conditional_rules = array(
'show' => 'hide',
'groups' => array(
array(
array(
'type' => 'page',
'option' => 'post_id',
'relation' => '=',
'value' => $exclude,
),
),
),
);
} else {
$conditional_rules = null;
}
break;
}
if ( ! empty( $conditional_rules ) ) {
$snippet_data['use_rules'] = true;
$snippet_data['rules'] = $conditional_rules;
}
return $snippet_data;
}
}
@@ -0,0 +1,259 @@
<?php
/**
* Importer for Post Snippets.
*
* @package WPCode
*/
/**
* Class WPCode_Importer_Post_Snippets
*/
class WPCode_Importer_Post_Snippets extends WPCode_Importer_Type {
/**
* The plugin name.
*
* @var string
*/
public $name = 'Post Snippets';
/**
* Importer slug.
*
* @var string
*/
public $slug = 'post-snippets';
/**
* Plugin path.
*
* @var string
*/
public $path = 'post-snippets/post-snippets.php';
/**
* Get an array of snippets from the Post Snippets plugin.
*
* @return array
*/
public function get_snippets() {
$snippets = array();
if ( ! $this->is_active() ) {
return $snippets;
}
global $wpdb;
$table_name = $wpdb->prefix . 'pspro_snippets';
// Check if the table exists.
if ( $wpdb->get_var( "SHOW TABLES LIKE '{$table_name}'" ) !== $table_name ) { // phpcs:ignore
return $snippets;
}
// Get all snippets.
$results = $wpdb->get_results( "SELECT * FROM {$table_name}", ARRAY_A ); // phpcs:ignore
if ( ! empty( $results ) ) {
foreach ( $results as $snippet ) {
$title = ! empty( $snippet['snippet_title'] ) ? $snippet['snippet_title'] : __( '(no title)', 'insert-headers-and-footers' );
$snippets[ $snippet['ID'] ] = $title;
}
}
return $snippets;
}
/**
* Import a single snippet.
*
* @return void
*/
public function import_snippet() {
// Run a security check.
check_ajax_referer( 'wpcode_admin' );
if ( ! current_user_can( 'wpcode_edit_snippets' ) ) { // phpcs:ignore
wp_send_json_error();
}
$id = isset( $_POST['snippet_id'] ) ? absint( $_POST['snippet_id'] ) : 0;
if ( ! $id ) {
wp_send_json_error(
array(
'error' => true,
'msg' => __( 'Invalid snippet ID.', 'insert-headers-and-footers' ),
)
);
}
global $wpdb;
$table_name = $wpdb->prefix . 'pspro_snippets';
$snippet = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table_name} WHERE ID = %d", $id ), ARRAY_A ); // phpcs:ignore
if ( empty( $snippet ) ) {
wp_send_json_error(
array(
'error' => true,
'msg' => __( 'Snippet not found.', 'insert-headers-and-footers' ),
)
);
}
// Convert the Post Snippets data into the WPCode snippet format.
$snippet_data = $this->get_snippet_data( $snippet );
// Create a new snippet.
$new_snippet = new WPCode_Snippet( $snippet_data );
$new_snippet->save();
if ( ! empty( $new_snippet->get_id() ) ) {
$title = $new_snippet->get_title();
wp_send_json_success(
array(
'name' => '' !== $title ? $title : '(no title)',
'edit' => esc_url_raw(
add_query_arg(
array(
'page' => 'wpcode-snippet-manager',
'snippet_id' => $new_snippet->get_id(),
),
admin_url( 'admin.php' )
)
),
)
);
} else {
wp_send_json_error(
array(
'error' => true,
'msg' => __( 'Failed to import snippet.', 'insert-headers-and-footers' ),
)
);
}
}
/**
* Convert a Post Snippets snippet to the format for a WPCode snippet.
*
* @param array $snippet The snippet data from Post Snippets.
*
* @return array
*/
public function get_snippet_data( $snippet ) {
// Determine code type based on snippet_php value.
$code_type = 'html';
if ( isset( $snippet['snippet_php'] ) ) {
if ( '1' === $snippet['snippet_php'] ) {
$code_type = 'php';
} elseif ( '2' === $snippet['snippet_php'] ) {
$code_type = 'js';
} elseif ( '3' === $snippet['snippet_php'] ) {
$code_type = 'css';
}
}
// Get group names for tags.
$tags = array( $this->add_imported_tag() );
if ( ! empty( $snippet['snippet_group'] ) ) {
$group_ids = maybe_unserialize( $snippet['snippet_group'] );
if ( is_array( $group_ids ) ) {
global $wpdb;
$group_table = $wpdb->prefix . 'pspro_groups';
foreach ( $group_ids as $group_id ) {
$group_name = $wpdb->get_var( $wpdb->prepare( "SELECT group_name FROM {$group_table} WHERE ID = %d", $group_id ) ); // phpcs:ignore
if ( $group_name && __( 'ungrouped', 'insert-headers-and-footers' ) !== $group_name ) {
$tags[] = sanitize_title( $group_name );
}
}
}
}
// Determine if this is a shortcode.
$is_shortcode = ! empty( $snippet['snippet_shortcode'] ) && '1' === $snippet['snippet_shortcode'];
$auto_insert = $is_shortcode ? 0 : 1;
// Get custom shortcode name.
$custom_shortcode = '';
if ( $is_shortcode ) {
$custom_shortcode = sanitize_title( $snippet['snippet_title'] );
}
// Determine location for PHP, CSS and JS snippets.
$location = '';
if ( 'php' === $code_type && $auto_insert ) {
// For PHP snippets, set location to "run everywhere".
$location = 'everywhere';
} elseif ( ( 'css' === $code_type || 'js' === $code_type ) && $auto_insert ) {
// Default locations.
$location = 'site_wide_header';
// Check snippet_vars for location information.
if ( ! empty( $snippet['snippet_vars'] ) ) {
$vars = explode( ',', $snippet['snippet_vars'] );
// For JS snippets, check header/footer location (first variable).
if ( 'js' === $code_type && isset( $vars[1] ) ) {
$js_location = trim( $vars[1] );
// The site location is the third variable in the snippet_vars field.
$site_location = isset( $vars[2] ) ? trim( $vars[2] ) : 'frontend';
// Map to appropriate WPCode location.
if ( 'footer' === $js_location && 'admin' === $site_location ) {
$location = 'admin_footer';
} elseif ( 'header' === $js_location && 'admin' === $site_location ) {
$location = 'admin_head';
} elseif ( 'footer' === $js_location ) {
$location = 'site_wide_footer';
}
}
// For CSS snippets, check admin/frontend location.
elseif ( 'css' === $code_type && isset( $vars[2] ) && 'admin' === trim( $vars[2] ) ) {
$location = 'admin_head';
}
}
}
// Prepare the snippet data.
$snippet_data = array(
'title' => $snippet['snippet_title'],
'code' => $snippet['snippet_content'],
'code_type' => $code_type,
'tags' => implode( ',', $tags ),
'note' => $snippet['snippet_desc'],
'auto_insert' => $auto_insert,
'active' => false,
'custom_shortcode' => $custom_shortcode,
);
// Add location if set.
if ( ! empty( $location ) ) {
$snippet_data['location'] = $location;
}
// If there are variables in the snippet, add them as shortcode attributes.
if ( ! empty( $snippet['snippet_vars'] ) && $is_shortcode ) {
$variables = explode( ',', $snippet['snippet_vars'] );
$shortcode_attributes = array();
foreach ( $variables as $variable ) {
$variable = trim( $variable );
// Check if variable has a default value (format: var=default).
$parts = explode( '=', $variable, 2 );
$variable_name = $parts[0];
if ( ! empty( $variable_name ) ) {
$shortcode_attributes[] = $variable_name;
}
}
if ( ! empty( $shortcode_attributes ) ) {
$snippet_data['shortcode_attributes'] = $shortcode_attributes;
}
}
return $snippet_data;
}
}
@@ -0,0 +1,223 @@
<?php
/**
* Importer for Simple Custom CSS and JS.
*
* @package WPCode.
*/
/**
* Class WPCode_Importer_Simple_Custom_CSS_and_JS.
*/
class WPCode_Importer_Simple_Custom_CSS_and_JS extends WPCode_Importer_Type {
/**
* The plugin name.
*
* @var string
*/
public $name = 'Simple Custom CSS and JS';
/**
* Importer slug.
*
* @var string
*/
public $slug = 'simple-custom-css-js';
/**
* Plugin path.
*
* @var string
*/
public $path = 'custom-css-js/custom-css-js.php';
/**
* Get an array of snippets for this plugin.
*
* @return array
*/
public function get_snippets() {
$snippets = array();
if ( ! $this->is_active() ) {
return $snippets;
}
// Retrieve all 'custom-css-js' posts.
$args = array(
'post_type' => 'custom-css-js',
'posts_per_page' => -1,
'post_status' => array( 'publish', 'draft' ),
);
$posts = get_posts( $args );
foreach ( $posts as $post ) {
$status = $post->post_status;
$label = $post->post_title ? $post->post_title : '(no title)';
if ( 'publish' !== $status ) {
$label .= ' (' . $status . ')';
}
$snippets[ $post->ID ] = $label;
}
return $snippets;
}
/**
* Import the snippet data.
*
* @return void
*/
public function import_snippet() {
// Run a security check.
check_ajax_referer( 'wpcode_admin' );
if ( ! current_user_can( 'wpcode_edit_snippets' ) ) {
wp_send_json_error();
}
$id = isset( $_POST['snippet_id'] ) ? absint( $_POST['snippet_id'] ) : 0;
if ( ! $id ) {
wp_send_json_error(
array(
'error' => true,
'name' => esc_html__( 'Unknown Snippet', 'insert-headers-and-footers' ),
'msg' => esc_html__( 'The snippet you are trying to import does not exist.', 'insert-headers-and-footers' ),
)
);
}
$post = get_post( $id );
if ( ! $post || 'custom-css-js' !== $post->post_type ) {
wp_send_json_error(
array(
'error' => true,
'name' => esc_html__( 'Unknown Snippet', 'insert-headers-and-footers' ),
'msg' => esc_html__( 'The snippet you are trying to import does not exist.', 'insert-headers-and-footers' ),
)
);
}
// Now get the snippet data.
$snippet_data = $this->get_snippet_data( $post );
// Create new WPCode snippet.
$new_snippet = new WPCode_Snippet( $snippet_data );
// Save the snippet.
$new_snippet->save();
if ( ! empty( $new_snippet->get_id() ) ) {
$title = $new_snippet->get_title();
wp_send_json_success(
array(
'name' => '' !== $title ? $title : '(no title)',
'edit' => esc_url_raw(
add_query_arg(
array(
'page' => 'wpcode-snippet-manager',
'snippet_id' => $new_snippet->get_id(),
),
admin_url( 'admin.php' )
)
),
)
);
} else {
wp_send_json_error(
array(
'error' => true,
'name' => $post->post_title,
'msg' => esc_html__( 'Failed to import the snippet.', 'insert-headers-and-footers' ),
)
);
}
}
/**
* Convert a "Simple Custom CSS and JS" snippet to the format for a WPCode snippet.
*
* @param WP_Post $post The snippet post object.
*
* @return array
*/
public function get_snippet_data( $post ) {
// Prepare the data.
$code = $post->post_content;
$title = $post->post_title;
// Get the options array.
$options = get_post_meta( $post->ID, 'options', true );
$code_type = 'css';
if ( is_array( $options ) && isset( $options['language'] ) ) {
$language = $options['language'];
// Map language to code_type.
switch ( $language ) {
case 'js':
$code_type = 'javascript';
break;
case 'html':
$code_type = 'html';
break;
}
}
$type_location = isset( $options['type'] ) ? $options['type'] : '';
if ( 'header' === $type_location ) {
$location = 'site_wide_header';
} elseif ( 'footer' === $type_location ) {
$location = 'site_wide_footer';
} else {
$location = 'site_wide_body';
}
if ( ! empty( $options['side'] ) ) {
$site_locations = explode( ',', $options['side'] );
$site_locations = array_map( 'trim', $site_locations );
if ( in_array( 'admin', $site_locations, true ) && in_array( 'frontend', $site_locations, true ) ) {
$location = 'site_wide_header';
} elseif ( 'header' === $type_location && in_array( 'admin', $site_locations, true ) ) {
$location = 'admin_head';
} elseif ( 'footer' === $type_location && in_array( 'admin', $site_locations, true ) ) {
$location = 'admin_footer';
}
}
$priority = 10;
if ( is_array( $options ) && isset( $options['priority'] ) ) {
$priority = intval( $options['priority'] );
}
$load_as_file = false;
if ( is_array( $options ) && isset( $options['linking'] ) ) {
$linking = $options['linking'];
if ( 'external' === $linking ) {
$load_as_file = true;
}
}
// Prepare the data array.
$snippet_data = array(
'code' => wp_slash( $code ),
'title' => $title,
'code_type' => $code_type,
'location' => $location,
'auto_insert' => 1, // Set to 1 for auto-insert.
'priority' => $priority,
'tags' => $this->add_imported_tag(),
);
// Set 'load_as_file' if applicable.
if ( $load_as_file ) {
$snippet_data['load_as_file'] = true;
}
return $snippet_data;
}
}
@@ -0,0 +1,107 @@
<?php
/**
* Abstract class for importing data from other plugins.
*
* @package WPCode
*/
/**
* Abstract class WPCode_Importer_Type.
*/
abstract class WPCode_Importer_Type {
/**
* The plugin name.
*
* @var string
*/
public $name = '';
/**
* The plugin slug.
*
* @var string
*/
public $slug = '';
/**
* The plugin path.
*
* @var string
*/
public $path = '';
/**
* Constructor.
*/
public function __construct() {
add_action( "wp_ajax_wpcode_import_snippet_{$this->slug}", array( $this, 'import_snippet' ) );
}
/**
* Add to list of registered importers.
*
* @param array $importers List of supported importers.
*
* @return array
*/
public function register( $importers = array() ) {
$importers[ $this->slug ] = array(
'name' => $this->name,
'slug' => $this->slug,
'path' => $this->path,
'installed' => file_exists( trailingslashit( WP_PLUGIN_DIR ) . $this->path ),
'active' => $this->is_active(),
);
return $importers;
}
/**
* Get all the snippets for this plugin.
*
* @return array
*/
abstract public function get_snippets();
/**
* Check if the plugin is active.
*
* @return bool
*/
public function is_active() {
return is_plugin_active( $this->path );
}
/**
* After a snippet has been successfully imported we track it, so that in the
* future we can alert users if they try to import a snippet that has already
* been imported.
*
* @param int $source_id Imported plugin snippet ID.
* @param int $wpcode_id WPCode snippet ID.
*/
public function track_import( $source_id, $wpcode_id ) {
$imported = get_option( 'wpcode_imported', array() );
$imported[ $this->slug ][ $wpcode_id ] = $source_id;
update_option( 'wpcode_imported', $imported, false );
}
/**
* Get the imported tag for this plugin.
*
* @return string
*/
public function add_imported_tag(){
return 'imported-' . $this->slug;
}
/**
* Import a single snippet using AJAX.
*
* @return void
*/
abstract public function import_snippet();
}
@@ -0,0 +1,171 @@
<?php
/**
* Importer for Woody.
*
* @package WPCode.
*/
/**
* Class WPCode_Importer_Woody.
*/
class WPCode_Importer_Woody extends WPCode_Importer_Type {
/**
* The plugin name.
*
* @var string
*/
public $name = 'Woody Code Snippets';
/**
* Importer slug.
*
* @var string
*/
public $slug = 'woody';
/**
* Plugin path.
*
* @var string
*/
public $path = 'insert-php/insert_php.php';
/**
* Get an array of snippets for this plugin.
*
* @return array
*/
public function get_snippets() {
$snippets = array();
if ( ! $this->is_active() ) {
return $snippets;
}
$snippets_posts = get_posts(
array(
'post_type' => 'wbcr-snippets',
'posts_per_page' => - 1,
'post_status' => 'any',
)
);
foreach ( $snippets_posts as $post ) {
if ( '' === $post->post_title ) {
$post->post_title = '(no title)';
}
$snippets[ $post->ID ] = $post->post_title;
}
return $snippets;
}
/**
* Import the snippet data.
*
* @return void
*/
public function import_snippet() {
// Run a security check.
check_ajax_referer( 'wpcode_admin' );
if ( ! current_user_can( 'wpcode_edit_snippets' ) ) {
wp_send_json_error();
}
if ( ! class_exists( 'WINP_Helper' ) ) {
wp_send_json_error();
}
$id = isset( $_POST['snippet_id'] ) ? absint( $_POST['snippet_id'] ) : 0;
// Grab a snippet from Code Snippets.
$snippet = get_post( $id );
if ( null === $snippet ) {
wp_send_json_error(
array(
'error' => true,
'name' => esc_html__( 'Unknown Snippet', 'insert-headers-and-footers' ),
'msg' => esc_html__( 'The snippet you are trying to import does not exist.', 'insert-headers-and-footers' ),
)
);
}
// If we got so far we have a snippet to process.
// Create a new snippet from the snippet data array.
$new_snippet = new WPCode_Snippet( $this->get_snippet_data( $snippet ) );
$new_snippet->save();
if ( ! empty( $new_snippet->get_id() ) ) {
$title = $new_snippet->get_title();
wp_send_json_success(
array(
'name' => '' !== $title ? $title : '(no title)',
'edit' => esc_url_raw(
add_query_arg(
array(
'page' => 'wpcode-snippet-manager',
'snippet_id' => $new_snippet->get_id(),
),
admin_url( 'admin.php' )
)
),
)
);
}
}
/**
* Convert a "Woody" snippet to the format for a WPCode snippet.
*
* @param WP_Post $snippet The snippet post.
*
* @return array
*/
public function get_snippet_data( $snippet ) {
$snippet_location = WINP_Helper::getMetaOption( $snippet->ID, 'snippet_location', '' );
$scope = WINP_Helper::getMetaOption( $snippet->ID, 'snippet_scope', '' );
$auto_insert = in_array(
$scope,
array(
'auto',
'evrywhere',
),
true
) ? 1 : 0;
switch ( $snippet_location ) {
case 'header':
$location = 'site_wide_header';
break;
case 'footer':
$location = 'site_wide_footer';
break;
default:
$location = $snippet_location;
}
$existing_tags = wp_get_post_terms( $snippet->ID, WINP_SNIPPETS_TAXONOMY, array( 'fields' => 'slugs' ) );
$existing_tags[] = $this->add_imported_tag(); // Add the import tag.
$tags = ( is_array( $existing_tags ) ) ? implode( ',', $existing_tags ) : $existing_tags;
return array(
'code' => wp_slash( WINP_Helper::get_snippet_code( $snippet ) ),
'note' => WINP_Helper::getMetaOption( $snippet->ID, 'snippet_description', '' ),
'title' => $snippet->post_title,
'tags' => $tags,
'code_type' => WINP_Helper::get_snippet_type( $snippet->ID ),
'priority' => intval( WINP_Helper::getMetaOption( $snippet->ID, 'snippet_priority', '' ) ),
'location' => $location,
'auto_insert' => $auto_insert,
);
}
}