Initial commit xmarticaims
This commit is contained in:
@@ -0,0 +1,323 @@
|
||||
<?php
|
||||
/**
|
||||
* Metabox file
|
||||
*
|
||||
* @package WpFaqSchema
|
||||
*/
|
||||
|
||||
namespace WpFaqSchema;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Metabox class
|
||||
*/
|
||||
class Metabox {
|
||||
/**
|
||||
* Constructor Method for Metabox
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct() {
|
||||
add_action( 'add_meta_boxes', [ $this, 'metabox_init' ] );
|
||||
add_action( 'save_post', [ $this, 'save' ] );
|
||||
add_action( 'admin_enqueue_scripts', [ $this, 'assets' ] );
|
||||
|
||||
add_action( 'init', [ $this, 'taxonomy_meta' ], 999 );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize metabox
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function metabox_init() {
|
||||
$post_types = get_post_types( [
|
||||
'public' => true,
|
||||
] );
|
||||
unset( $post_types['attachment'] );
|
||||
unset( $post_types['elementor_library'] );
|
||||
/* translators: icon */
|
||||
add_meta_box( 'wp-faq-schema', sprintf( __( 'FAQ Schema For Pages And Posts %s', 'wp-faq-schema' ), ' <span class="dashicons dashicons-info" title="' . __( 'To save just update your page, this text saves automatically on page update.', 'wp-faq-schema' ) . '"></span>' ), [ $this, 'view' ], array_keys( $post_types ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Metabox view
|
||||
*
|
||||
* @param WP_Post $post Post Object.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function view( $post ) {
|
||||
$faqs = get_post_meta( $post->ID, '_wp_faq_schema', true );
|
||||
|
||||
if ( empty( $faqs ) ) {
|
||||
$faqs = [
|
||||
[
|
||||
'q' => '',
|
||||
'a' => '',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
?>
|
||||
<div class="wp-faq-schema-container">
|
||||
<input type="hidden" name="wp-faq-schema-nonce" value="<?php echo esc_attr( wp_create_nonce( 'wp-faq-schema' ) ); ?>">
|
||||
<div class="wp-faq-schema-items">
|
||||
<?php foreach ( $faqs as $index => $faq ) : ?>
|
||||
<div class="wp-faq-schema-item">
|
||||
<div class="wp-faq-schema-header">
|
||||
<span>
|
||||
<?php
|
||||
/* translators: index */
|
||||
echo esc_html( ! empty( $faq['q'] ) ? $faq['q'] : sprintf( __( 'Question #%s', 'wp-faq-schema' ), $index + 1 ) );
|
||||
?>
|
||||
</span>
|
||||
<button class="wp-faq-schema-delete" type="button" data-confirm="<?php esc_html_e( 'Are you sure you want to delete this question and answer?', 'wp-faq-schema' ); ?>"><span class="dashicons dashicons-trash"></span></button>
|
||||
</div>
|
||||
<div class="wp-faq-schema">
|
||||
<p>
|
||||
<label for="wp-faq-schema-q-<?php echo esc_attr( $index ); ?>"><?php esc_html_e( 'Question', 'wp-faq-schema' ); ?></label>
|
||||
<input type="text" id="wp-faq-schema-q-<?php echo esc_attr( $index ); ?>" name="wp-faq-schema[<?php echo esc_attr( $index ); ?>][q]" class="widefat" value="<?php echo esc_attr( ! empty( $faq['q'] ) ? $faq['q'] : '' ); ?>">
|
||||
</p>
|
||||
<p>
|
||||
<label for="wp-faq-schema-a-<?php echo esc_attr( $index ); ?>"><?php esc_html_e( 'Answer', 'wp-faq-schema' ); ?></label>
|
||||
<textarea name="wp-faq-schema[<?php echo esc_attr( $index ); ?>][a]" id="wp-faq-schema-a-<?php echo esc_attr( $index ); ?>" rows="3" class="widefat"><?php echo esc_html( ! empty( $faq['a'] ) ? $faq['a'] : '' ); ?></textarea>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<?php /* translators: index */ ?>
|
||||
<button type="button" class="button button-primary wp-faq-schema-add" data-index="<?php echo count( $faqs ); ?>" data-title="<?php esc_attr_e( 'Question #%s', 'wp-faq-schema' ); ?>">
|
||||
<?php echo esc_html_e( 'Add Another Question', 'wp-faq-schema' ); ?>
|
||||
</button>
|
||||
</div>
|
||||
<script type="text/html" id="tmpl-faq-schema-form">
|
||||
<div class="wp-faq-schema-item">
|
||||
<div class="wp-faq-schema-header">
|
||||
<span>{TITLE}</span>
|
||||
<button class="wp-faq-schema-delete" type="button" data-confirm="<?php esc_attr_e( 'Are you sure? You want to delete this.', 'wp-faq-schema' ); ?>"><span class="dashicons dashicons-trash"></span></button>
|
||||
</div>
|
||||
<div class="wp-faq-schema">
|
||||
<p>
|
||||
<label for="wp-faq-schema-q-{INDEX}"><?php esc_html_e( 'Question', 'wp-faq-schema' ); ?></label>
|
||||
<input type="text" id="wp-faq-schema-q-{INDEX}" name="wp-faq-schema[{INDEX}][q]" class="widefat">
|
||||
</p>
|
||||
<p>
|
||||
<label for="wp-faq-schema-a-{INDEX}"><?php esc_html_e( 'Answer', 'wp-faq-schema' ); ?></label>
|
||||
<textarea id="wp-faq-schema-a-{INDEX}" name="wp-faq-schema[{INDEX}][a]" rows="3" class="widefat"></textarea>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</script>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Save meta values
|
||||
*
|
||||
* @param int $post_id Post ID.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function save( $post_id ) {
|
||||
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! isset( $_POST['wp-faq-schema-nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['wp-faq-schema-nonce'] ), 'wp-faq-schema' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
update_post_meta( $post_id, '_wp_faq_schema', ! isset( $_POST['wp-faq-schema'] ) ? [] : $this->sanitize( $_POST['wp-faq-schema'] ) ); // phpcs:ignore
|
||||
}
|
||||
|
||||
/**
|
||||
* Metabox assets
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function assets() {
|
||||
wp_enqueue_style( 'wp-faq-schema-style', WP_FAQ_SCHEMA_URL . '/css/wp-faq-schema.css', false, '1.0.0' );
|
||||
|
||||
wp_enqueue_script( 'sweetalert2', WP_FAQ_SCHEMA_URL . '/js/sweetalert2.all.min.js', [ 'jquery' ], '1.0.0', true );
|
||||
wp_enqueue_script( 'papaparse', WP_FAQ_SCHEMA_URL . '/js/papaparse.min.js', [ 'jquery' ], '1.0.0', true );
|
||||
wp_enqueue_script( 'wp-faq-schema-scripts', WP_FAQ_SCHEMA_URL . '/js/wp-faq-schema.js', [ 'jquery', 'papaparse', 'sweetalert2' ], '1.0.0', true );
|
||||
|
||||
$data = 'var wfs_ajax="' . admin_url( 'admin-ajax.php' ) . '";';
|
||||
|
||||
wp_add_inline_script( 'wp-faq-schema-scripts', $data, 'before' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize full array
|
||||
*
|
||||
* @param array $faqs Array.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function sanitize( $faqs ) {
|
||||
$new_faqs = [];
|
||||
|
||||
if ( ! empty( $faqs ) && is_array( $faqs ) ) {
|
||||
foreach ( $faqs as $faq ) {
|
||||
$new_faq = [];
|
||||
|
||||
if ( ! empty( $faq['q'] ) ) {
|
||||
$new_faq['q'] = sanitize_text_field( $faq['q'] );
|
||||
}
|
||||
if ( ! empty( $faq['a'] ) ) {
|
||||
$new_faq['a'] = wp_kses_post( $faq['a'] );
|
||||
}
|
||||
|
||||
if ( ! empty( $new_faq ) ) {
|
||||
$new_faqs[] = $new_faq;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $new_faqs;
|
||||
}
|
||||
|
||||
public function taxonomy_meta() {
|
||||
$taxonomies = get_taxonomies( ['publicly_queryable' => true] );
|
||||
$taxonomies = array_keys( $taxonomies );
|
||||
|
||||
foreach( $taxonomies as $taxonomy ) {
|
||||
add_filter( $taxonomy . '_add_form_fields', array( $this, 'tax_meta' ) );
|
||||
add_filter( $taxonomy . '_edit_form_fields', array( $this, 'tax_meta_edit' ) );
|
||||
|
||||
add_action( 'edit_' . $taxonomy, array( $this, 'save_tax_meta' ) );
|
||||
add_action( 'create_' . $taxonomy, array( $this, 'save_tax_meta' ) );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function tax_meta() {
|
||||
?>
|
||||
<div class="form-field is-tax">
|
||||
<?php $this->tax_meta_label(); ?>
|
||||
<?php $this->tax_meta_view( false ); ?>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
public function tax_meta_edit( $term ) {
|
||||
?>
|
||||
<tr class="form-field store-logo-wrap">
|
||||
<th scope="row">
|
||||
<?php $this->tax_meta_label(); ?>
|
||||
</th>
|
||||
<td>
|
||||
<div class="is-tax">
|
||||
<?php $this->tax_meta_view( $term->term_id ); ?>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function tax_meta_label() {
|
||||
?>
|
||||
<label class="is-tax-label"><?php printf( __( 'FAQ Schema For Pages And Posts %s', 'wp-faq-schema' ), ' <span class="dashicons dashicons-info" title="' . __( 'To save just update your page, this text saves automatically on page update.', 'wp-faq-schema' ) . '"></span>' ) ?></label>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function tax_meta_view( $term_id ) {
|
||||
$faqs = $term_id ? get_term_meta( $term_id, '_wp_faq_schema', true ) : '';
|
||||
|
||||
if ( empty( $faqs ) ) {
|
||||
$faqs = [
|
||||
[
|
||||
'q' => '',
|
||||
'a' => '',
|
||||
],
|
||||
];
|
||||
}
|
||||
?>
|
||||
<div class="wp-faq-schema-container">
|
||||
<input type="hidden" name="wp-faq-schema-nonce" value="<?php echo esc_attr( wp_create_nonce( 'wp-faq-schema' ) ); ?>">
|
||||
<div class="wp-faq-schema-items">
|
||||
<?php foreach ( $faqs as $index => $faq ) : ?>
|
||||
<div class="wp-faq-schema-item">
|
||||
<div class="wp-faq-schema-header">
|
||||
<span>
|
||||
<?php
|
||||
/* translators: index */
|
||||
echo esc_html( ! empty( $faq['q'] ) ? $faq['q'] : sprintf( __( 'Question #%s', 'wp-faq-schema' ), $index + 1 ) );
|
||||
?>
|
||||
</span>
|
||||
<button class="wp-faq-schema-delete" type="button" data-confirm="<?php esc_html_e( 'Are you sure you want to delete this question and answer?', 'wp-faq-schema' ); ?>"><span class="dashicons dashicons-trash"></span></button>
|
||||
</div>
|
||||
<div class="wp-faq-schema">
|
||||
<p>
|
||||
<label for="wp-faq-schema-q-<?php echo esc_attr( $index ); ?>"><?php esc_html_e( 'Question', 'wp-faq-schema' ); ?></label>
|
||||
<input type="text" id="wp-faq-schema-q-<?php echo esc_attr( $index ); ?>" name="wp-faq-schema[<?php echo esc_attr( $index ); ?>][q]" class="widefat" value="<?php echo esc_attr( ! empty( $faq['q'] ) ? $faq['q'] : '' ); ?>">
|
||||
</p>
|
||||
<p>
|
||||
<label for="wp-faq-schema-a-<?php echo esc_attr( $index ); ?>"><?php esc_html_e( 'Answer', 'wp-faq-schema' ); ?></label>
|
||||
<textarea name="wp-faq-schema[<?php echo esc_attr( $index ); ?>][a]" id="wp-faq-schema-a-<?php echo esc_attr( $index ); ?>" rows="3" class="widefat"><?php echo esc_html( ! empty( $faq['a'] ) ? $faq['a'] : '' ); ?></textarea>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<?php /* translators: index */ ?>
|
||||
<button type="button" class="button button-primary wp-faq-schema-add" data-index="<?php echo count( $faqs ); ?>" data-title="<?php esc_attr_e( 'Question #%s', 'wp-faq-schema' ); ?>">
|
||||
<?php echo esc_html_e( 'Add Another Question', 'wp-faq-schema' ); ?>
|
||||
</button>
|
||||
</div>
|
||||
<script type="text/html" id="tmpl-faq-schema-form">
|
||||
<div class="wp-faq-schema-item">
|
||||
<div class="wp-faq-schema-header">
|
||||
<span>{TITLE}</span>
|
||||
<button class="wp-faq-schema-delete" type="button" data-confirm="<?php esc_attr_e( 'Are you sure? You want to delete this.', 'wp-faq-schema' ); ?>"><span class="dashicons dashicons-trash"></span></button>
|
||||
</div>
|
||||
<div class="wp-faq-schema">
|
||||
<p>
|
||||
<label for="wp-faq-schema-q-{INDEX}"><?php esc_html_e( 'Question', 'wp-faq-schema' ); ?></label>
|
||||
<input type="text" id="wp-faq-schema-q-{INDEX}" name="wp-faq-schema[{INDEX}][q]" class="widefat">
|
||||
</p>
|
||||
<p>
|
||||
<label for="wp-faq-schema-a-{INDEX}"><?php esc_html_e( 'Answer', 'wp-faq-schema' ); ?></label>
|
||||
<textarea id="wp-faq-schema-a-{INDEX}" name="wp-faq-schema[{INDEX}][a]" rows="3" class="widefat"></textarea>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</script>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function save_tax_meta( $term_id ) {
|
||||
if ( ! isset( $_POST['wp-faq-schema-nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['wp-faq-schema-nonce'] ), 'wp-faq-schema' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
update_term_meta( $term_id, '_wp_faq_schema', ! isset( $_POST['wp-faq-schema'] ) ? [] : $this->sanitize( $_POST['wp-faq-schema'] ) ); // phpcs:ignore
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,226 @@
|
||||
<?php
|
||||
/**
|
||||
* Option Page file
|
||||
*
|
||||
* @package WpFaqSchema
|
||||
*/
|
||||
|
||||
namespace WpFaqSchema;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Option Page class
|
||||
*/
|
||||
class Option_Page {
|
||||
/**
|
||||
* Constructor Method for Option Page
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @access public
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct() {
|
||||
add_action( 'admin_menu', [ $this, 'register' ] );
|
||||
add_action( 'wp_ajax_wfs_export', [ $this, 'export' ] );
|
||||
add_action( 'wp_ajax_wfs_import', [ $this, 'import' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register Option Page
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @access public
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register() {
|
||||
add_options_page( __( 'FAQ Schema For Pages And Posts', 'wp-faq-schema' ), __( 'FAQ Schema For Pages And Posts', 'wp-faq-schema' ), 'manage_options', 'wp-faq-schema', [ $this, 'view' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Option Page view
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @access public
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function view() {
|
||||
?>
|
||||
<div class="wrap">
|
||||
<h1><?php esc_html_e( 'FAQ Schema For Pages And Posts Settings', 'wp-faq-schema' ); ?></h1>
|
||||
<h2><?php esc_html_e( 'How the plugin works step by step:', 'wp-faq-schema' ); ?></h2>
|
||||
<p><a href="https://robotzebra.agency" target="_blank"><?php esc_html_e( 'Find out how the plugin works', 'wp-faq-schema' ); ?></a></p>
|
||||
<iframe width="560" height="315" src="https://www.youtube.com/watch?v=pRhXbu24lsQ" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
|
||||
<ol>
|
||||
<li><?php esc_html_e( 'Just to insert JSON-LD in a given page, go to that page/post and you’ll find new fields at the bottom the editor in the WP backend.', 'wp-faq-schema' ); ?></li>
|
||||
<li><?php esc_html_e( 'If you’d like to also display your Questions and Answers via a FAQ, use the shortcode. Title defines the heading above the FAQs. If you’d like to use an accordion, add accordion=1 as in the example. Remove that part not to use an accordion and just show all FAQs open (ie. [wp-faq-schema title="Car FAQs" accordion=1] )', 'wp-faq-schema' ); ?></li>
|
||||
<li><?php esc_html_e( 'If you’d like to mass edit, use the Export section to export all of the pages and posts with their ids - and then edit the file and import it back. Note: For multiple FAQs for same post add more row at CSV file with same ID and title.', 'wp-faq-schema' ); ?></li>
|
||||
</ol>
|
||||
|
||||
<h2><?php esc_html_e( 'How to make FAQ display on the front end:', 'wp-faq-schema' ); ?></h2>
|
||||
<p><?php echo wp_kses( __( 'Add the shortcode inside of the page or post content to display the FAQ. The shortcode tag is <code>wp-faq-schema</code>. It will take <code>title</code> and <code>accordion</code> attributes.', 'wp-faq-schema' ), [ 'code' => [] ] ); ?></p>
|
||||
<p><strong><?php esc_html_e( 'Example below:', 'wp-faq-schema' ); ?></strong></p>
|
||||
<p><code>[wp-faq-schema]</code> - <?php esc_html_e( 'This code will only show FAQs of that page or post.', 'wp-faq-schema' ); ?></p>
|
||||
<p><code>[wp-faq-schema title="Car FAQs"]</code> - <?php esc_html_e( 'This code will show FAQs with "Car FAQs" title.', 'wp-faq-schema' ); ?></p>
|
||||
<p><code>[wp-faq-schema accordion=1]</code> - <?php esc_html_e( 'This code will show FAQs as accordion.', 'wp-faq-schema' ); ?></p>
|
||||
<p><code>[wp-faq-schema title="Car FAQs" accordion=1]</code> - <?php esc_html_e( 'This code will show FAQs as accordion with "Car FAQs" title.', 'wp-faq-schema' ); ?></p>
|
||||
|
||||
<h2><?php esc_html_e( 'Export for editing on multiple pages', 'wp-faq-schema' ) ?></h2>
|
||||
<p><?php esc_html_e( 'Export a CSV with your posts and pages to edit the CSV with your FAQs and re-upload it back to import all of them automatically.', 'wp-faq-schema' ); ?></p>
|
||||
<form method="post">
|
||||
<input type="hidden" id="wfs-nonce" name="wfs_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wfs_nonce' ) ); ?>">
|
||||
<table class="form-table">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th>
|
||||
<label for="wp-faq-schema-export"><?php esc_html_e( 'Export', 'wp-faq-schema' ); ?></label>
|
||||
</th>
|
||||
<td>
|
||||
<button class="button button-primary" type="button" id="wp-faq-schema-export"><?php esc_html_e( 'Export as .csv', 'wp-faq-schema' ); ?></button>
|
||||
<div class="wfs-export-message" id="export-message"></div>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>
|
||||
<label for="wp-faq-schema-import-file"><?php esc_html_e( 'Import', 'wp-faq-schema' ); ?></label>
|
||||
</th>
|
||||
<td>
|
||||
<input type="file" id="wp-faq-schema-import-file">
|
||||
<button class="button button-primary" type="button" id="wp-faq-schema-import"><?php esc_html_e( 'Import', 'wp-faq-schema' ); ?></button>
|
||||
<div class="wfs-export-message" id="import-message"></div>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</form>
|
||||
|
||||
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Export FAQs
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @access public
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function export() {
|
||||
if ( ! isset( $_POST['wfs_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['wfs_nonce'] ), 'wfs_nonce' ) ) {
|
||||
wp_send_json_error( __( 'Unauthorized access!', 'wp-faq-schema' ) );
|
||||
}
|
||||
|
||||
$posts = get_posts(
|
||||
[
|
||||
'post_type' => [ 'post', 'page', 'avada_portfolio' ],
|
||||
'posts_per_page' => -1,
|
||||
'post_status' => [ 'publish', 'pending', 'draft', 'private', 'future' ],
|
||||
]
|
||||
);
|
||||
|
||||
$faqs = [];
|
||||
|
||||
foreach ( $posts as $post ) {
|
||||
$faq_items = get_post_meta( $post->ID, '_wp_faq_schema', true );
|
||||
if ( ! empty( $faq_items ) ) {
|
||||
foreach ( $faq_items as $faq_item ) {
|
||||
$faqs[] = [
|
||||
$post->ID,
|
||||
$post->post_title,
|
||||
$faq_item['q'],
|
||||
$faq_item['a'],
|
||||
];
|
||||
}
|
||||
} else {
|
||||
$faqs[] = [
|
||||
$post->ID,
|
||||
$post->post_title,
|
||||
'',
|
||||
'',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$output = fopen( 'php://output', 'w' ) or wp_send_json_error( __( 'Unable to export as .csv file!', 'wp-faq-schema' ) );
|
||||
|
||||
fputcsv( $output, [ 'id', 'title', 'question', 'answer' ] );
|
||||
|
||||
foreach ( $faqs as $faq ) {
|
||||
fputcsv( $output, $faq );
|
||||
}
|
||||
|
||||
fclose( $output );
|
||||
die();
|
||||
}
|
||||
|
||||
/**
|
||||
* Import FAQs
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @access public
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function import() {
|
||||
if ( ! isset( $_POST['wfs_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['wfs_nonce'] ), 'wfs_nonce' ) ) {
|
||||
wp_send_json_error( __( 'Unauthorized access!', 'wp-faq-schema' ) );
|
||||
}
|
||||
|
||||
$items = ! empty( $_POST['items'] ) ? $_POST['items'] : [];
|
||||
|
||||
if ( empty( $items ) || ! is_array( $_POST['items'] ) ) {
|
||||
wp_send_json_error( __( 'Empty csv file.', 'wp-faq-schema' ) );
|
||||
}
|
||||
|
||||
$items_to_import = [];
|
||||
|
||||
foreach ( $items as $item ) {
|
||||
if ( empty( $item[0] ) ) {
|
||||
continue;
|
||||
}
|
||||
$id = (int) $item[0];
|
||||
if ( empty( $items_to_import[ $id ] ) ) {
|
||||
$items_to_import[ $id ] = [
|
||||
'title' => $item[1],
|
||||
'faqs' => [],
|
||||
];
|
||||
}
|
||||
$items_to_import[ $id ]['faqs'][] = [
|
||||
'q' => $item[2],
|
||||
'a' => $item[3],
|
||||
];
|
||||
}
|
||||
|
||||
foreach ( $items_to_import as $post_id => $item_to_import ) {
|
||||
$this->import_faq( $post_id, $item_to_import );
|
||||
}
|
||||
|
||||
wp_send_json_success( __( 'All FAQs has been imported. Enjoy!', 'wp-faq-schema' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Import Single Page/Post FAQs
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @access public
|
||||
*
|
||||
* @param int $post_id Post ID.
|
||||
* @param array $post_data Post Data to import.
|
||||
* @return bool
|
||||
*/
|
||||
public function import_faq( $post_id, $post_data ) {
|
||||
$post = get_post( $post_id );
|
||||
if ( is_null( $post ) || empty( $post_data['faqs'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return update_post_meta( $post_id, '_wp_faq_schema', $post_data['faqs'] );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,257 @@
|
||||
<?php
|
||||
/**
|
||||
* Plugin file
|
||||
*
|
||||
* @package WpFaqSchema
|
||||
*/
|
||||
|
||||
namespace WpFaqSchema;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Plugin class
|
||||
*/
|
||||
class Plugin {
|
||||
/**
|
||||
* Instance.
|
||||
*
|
||||
* Holds the plugin instance.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @static
|
||||
*
|
||||
* @var Plugin
|
||||
*/
|
||||
public static $instance = null;
|
||||
|
||||
/**
|
||||
* Clone.
|
||||
*
|
||||
* Disable class cloning and throw an error on object clone.
|
||||
*
|
||||
* The whole idea of the singleton design pattern is that there is a single
|
||||
* object. Therefore, we don't want the object to be cloned.
|
||||
*
|
||||
* @access public
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function __clone() {
|
||||
// Cloning instances of the class is forbidden.
|
||||
_doing_it_wrong( __FUNCTION__, esc_html__( 'Something went wrong.', 'wp-faq-schema' ), '1.0.0' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Wakeup.
|
||||
*
|
||||
* Disable unserializing of the class.
|
||||
*
|
||||
* @access public
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function __wakeup() {
|
||||
// Unserializing instances of the class is forbidden.
|
||||
_doing_it_wrong( __FUNCTION__, esc_html__( 'Something went wrong.', 'wp-faq-schema' ), '1.0.0' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Instance.
|
||||
*
|
||||
* Ensures only one instance of the plugin class is loaded or can be loaded.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @static
|
||||
*
|
||||
* @return Plugin An instance of the class.
|
||||
*/
|
||||
public static function instance() {
|
||||
if ( is_null( self::$instance ) ) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor Method for plugin
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->includes();
|
||||
$this->init();
|
||||
|
||||
add_action( 'wp_head', [ $this, 'print_schema' ] );
|
||||
add_filter( 'init', [ $this, 'shortcode' ] );
|
||||
add_action( 'wp_enqueue_scripts', [ $this, 'frontend_assets' ] );
|
||||
add_filter( 'plugin_row_meta', [ $this, 'plugin_links' ], 10, 2 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Include all files
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function includes() {
|
||||
require_once WP_FAQ_SCHEMA_PATH . 'includes/class-option-page.php';
|
||||
require_once WP_FAQ_SCHEMA_PATH . 'includes/class-metabox.php';
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize
|
||||
*
|
||||
* Initialize some classes which is not require hook.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function init() {
|
||||
|
||||
new Metabox();
|
||||
new Option_Page();
|
||||
}
|
||||
|
||||
/**
|
||||
* Print FAQ schema at Header
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function print_schema() {
|
||||
$faqs = get_post_meta( get_the_ID(), '_wp_faq_schema', true );
|
||||
if ( ! is_singular() && ! is_tax() && ! is_category() && ! is_tag() ) {
|
||||
return;
|
||||
}
|
||||
if ( is_tax() || is_category() || is_tag() ) {
|
||||
$term = get_queried_object();
|
||||
$faqs = get_term_meta( $term->term_id, '_wp_faq_schema', true );
|
||||
}
|
||||
if ( empty( $faqs ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$schema_array = [
|
||||
'@context' => 'https://schema.org',
|
||||
'@type' => 'FAQPage',
|
||||
'mainEntity' => [],
|
||||
];
|
||||
|
||||
if ( ! empty( $faqs ) ) {
|
||||
foreach ( $faqs as $faq ) {
|
||||
$schema_array['mainEntity'][] = [
|
||||
'@type' => 'Question',
|
||||
'name' => $faq['q'],
|
||||
'acceptedAnswer' => [
|
||||
'@type' => 'Answer',
|
||||
'text' => $faq['a'],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
?>
|
||||
<script type="application/ld+json">
|
||||
<?php echo wp_json_encode( $schema_array ); ?>
|
||||
</script>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Shortcodes
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @access public
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function shortcode() {
|
||||
add_shortcode( 'wp-faq-schema', [ $this, 'shortcode_view' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Shortcode View
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @access public
|
||||
*
|
||||
* @param array $atts Shortcode Atts.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function shortcode_view( $atts ) {
|
||||
$atts = shortcode_atts(
|
||||
array(
|
||||
'accordion' => 0,
|
||||
'title' => '',
|
||||
),
|
||||
$atts
|
||||
);
|
||||
|
||||
$faqs = get_post_meta( get_the_ID(), '_wp_faq_schema', true );
|
||||
|
||||
if ( empty( $faqs ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$is_accordion = isset( $atts['accordion'] ) ? intval( $atts['accordion'] ) : 0;
|
||||
|
||||
$wrap_class = [ 'wp-faq-schema-wrap' ];
|
||||
|
||||
if ( 1 === $is_accordion ) {
|
||||
$wrap_class[] = 'wp-faq-schema-accordion';
|
||||
}
|
||||
|
||||
ob_start();
|
||||
?>
|
||||
<div class="<?php echo esc_attr( implode( ' ', $wrap_class ) ); ?>">
|
||||
<?php if ( ! empty( $atts['title'] ) ) : ?>
|
||||
<h2><?php echo esc_html( $atts['title'] ); ?></h2>
|
||||
<?php endif; ?>
|
||||
<div class="wp-faq-schema-items">
|
||||
<?php foreach ( $faqs as $faq ) : ?>
|
||||
<h3><?php echo esc_html( $faq['q'] ); ?></h3>
|
||||
<div class="">
|
||||
<?php echo wp_kses_post( wpautop( $faq['a'] ) ); ?>
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
$html = ob_get_clean();
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Frontend assets
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @access public
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function frontend_assets() {
|
||||
wp_enqueue_style( 'wp-faq-schema-jquery-ui', WP_FAQ_SCHEMA_URL . '/css/jquery-ui.css', [], '2.0.0' );
|
||||
wp_enqueue_script( 'wp-faq-schema-frontend', WP_FAQ_SCHEMA_URL . '/js/frontend.js', [ 'jquery', 'jquery-ui-accordion' ], '2.0.0', true );
|
||||
}
|
||||
|
||||
public function plugin_links( $plugin_meta, $plugin_file ) {
|
||||
if ( 'faq-schema-for-pages-and-posts/wp-faq-schema.php' === $plugin_file ) {
|
||||
$plugin_meta[] = '<a href="https://robotzebra.agency/" target="_blank">Support</a>';
|
||||
}
|
||||
|
||||
return $plugin_meta;
|
||||
}
|
||||
}
|
||||
|
||||
Plugin::instance();
|
||||
Reference in New Issue
Block a user