Skip to content

Commit

Permalink
BIP type pages will now be converted to standard pages on deactivation (
Browse files Browse the repository at this point in the history
#20), and BIP functional pages are more carefully (re-)created (#21)
  • Loading branch information
lgarczewski committed Dec 13, 2020
1 parent b86927d commit a1e86cc
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 33 deletions.
28 changes: 25 additions & 3 deletions bip-pages-activation.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,20 @@ function create_page( $title, $content = '' ) {
return $page_id;
}

function create_functional_page( $title, $content = '' ) {
if ( !is_page( $title ) ) {
$page_id = create_page( $title, $content );
} else {
$page_id = untrash_page( $title );
}

return $page_id;
}

function create_main_page() {
$title = __( 'BIP Main Page', 'bip-pages' );

$main_page_id = create_page( $title );
$main_page_id = create_functional_page( $title );

if ( !is_wp_error( $main_page_id ) ) {
set_bip_main_page( $main_page_id );
Expand All @@ -43,7 +53,7 @@ function create_instructions_page() {
// Polish only for now
$instructions = file_get_contents( __DIR__ . '/boilerplate-text/bip-usage-manual-pl.txt' );

$instruction_page_id = create_page( $title, $instructions );
$instruction_page_id = create_functional_page( $title, $instructions );

if ( !is_wp_error( $instruction_page_id ) ) {
$option = get_option( Settings\OPTION_NAME, array() );
Expand All @@ -52,6 +62,18 @@ function create_instructions_page() {
}
}

function untrash_page( $title ) {
$page = WP_Post( $title );

if ( get_post_status( $page->ID ) == 'trash' ) {
untrash_post( $page );
} else {
// error handling here
}

return $page->ID;
}

function add_logo_widget() {
// initialize widget properties if not set yet
if ( empty( get_option( 'widget_bip-logo' ) ) ) {
Expand Down Expand Up @@ -104,6 +126,6 @@ function activation_notice(){
</div>
<?php

delete_transient( 'fx-admin-notice-example' );
delete_transient( 'bip-pages-activation-msg' );
}
}
63 changes: 63 additions & 0 deletions bip-pages-deactivation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
namespace BipPages;

// turn all bip pages to regular pages
function convert_page_types() {
$converted = 0;

$bip_pages = get_pages( ['post_type' => 'bip'] );
$total = count( $bip_pages );

foreach ( $bip_pages as $page ) {
if ( post_exists( $page->post_title, '', '', 'page' ) ) {
// rename first
wp_update_post( array(
'ID' => $page->ID,
'post_status' => 'draft'
));
}

$res = set_post_type( $page->ID, 'page');
$converted += $res;
}

return $converted;
}

function remove_widgets() {
// remove widget data
delete_option( 'widget_bip-logo' );
$active_widgets = get_option( 'sidebars_widgets' );

foreach ( $active_widgets as $key => $val ) {
if ( empty( $val ) || !is_array( $val ) ) {
continue;
}

$widget_ids = array_flip( $val );

foreach ( $widget_ids as $widget => $id ) {
if ( strpos( $widget, 'bip-logo-' ) === 0 ) {
unset( $active_widgets[$key][$id] );
}
}
}

update_option( 'sidebars_widgets', $active_widgets );
}

add_action( 'admin_notices', __NAMESPACE__ . '\deactivation_notice' );

function deactivation_notice(){
if( get_transient( 'bip-pages-deactivation-msg' ) ){
?>
<div class="updated notice is-dismissible">
<p>
<?= esc_html__( 'BIP Pages plugin has been deactivated. Your BIP pages have been converted to standard pages (or drafts in case of a conflicting page title)', 'bip-pages' ) ?>
</p>
</div>
<?php

delete_transient( 'bip-pages-deactivation-msg' );
}
}
40 changes: 10 additions & 30 deletions bip-pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,50 +38,30 @@ function plugin_init() {
register_deactivation_hook( __FILE__, __NAMESPACE__ . '\deactivate' );

function include_submodules() {
include( 'bip-pages-activation.php' );
include( 'bip-pages-main-page.php' );
include( 'bip-pages-settings.php' );
include( 'bip-pages-styling.php' );
include( 'bip-logo-widget.php' );
}

function activate() {
add_option('Activated_Plugin','bip-pages'); // deleted later in post_activation_flow
add_option('Activated_Plugin','bip-pages'); // deleted later in post_activation_flow

include_submodules();
include_submodules();
include( 'bip-pages-activation.php' );

create_main_page();
create_instructions_page();
add_logo_widget();
create_main_page();
create_instructions_page();
add_logo_widget();
}

function deactivate() {
// remove widget data
delete_option( 'widget_bip-logo' );
$active_widgets = get_option( 'sidebars_widgets' );

foreach ( $active_widgets as $key => $val ) {
if ( empty( $val ) || !is_array( $val ) ) {
continue;
}
include( 'bip-pages-deactivation.php' );

$widget_ids = array_flip( $val );
set_transient( 'bip-pages-deactivation-msg', true, 5 );

foreach ( $widget_ids as $widget => $id ) {
if ( strpos( $widget, 'bip-logo-' ) === 0 ) {
unset( $active_widgets[$key][$id] );
}
}
}

update_option( 'sidebars_widgets', $active_widgets );

// turn all bip pages to regular pages
$bip_pages = get_pages( ['post_type' => 'bip'] );
foreach ( $bip_pages as $page ) {
$page->post_type = 'page';
wp_update_post( $page );
}
remove_widgets();
convert_page_types();
}

function register_css() {
Expand Down

0 comments on commit a1e86cc

Please sign in to comment.