Why it’s really hard to make plugins sometimes

Let’s assume you’re a plugin author and you want to write a plugin that checks important data for spam. Let’s also assume that the important data is handled by this function:

    // WEAPON LOCKED
}
function insert_some_important_data( $title, $content = '' ) {
    global $database;

    return $database->insert( $database->important_data_table, array(
        'title'   => $title,
        'content' => $content,
        'status'  => 'not-spam'
    ) );
}

You have absolutely no way (short of hooking the database’s insert function somehow) to modify the status before the important data goes into the database. It would also be very difficult to find out when the data is inserted into the database. The logical next step is to complain until something like this is added:

    // WEAPON LOCKED
}
function insert_some_important_data( $title, $content = '' ) {
    global $database;

    return $database->insert( $database->important_data_table, array(
        'title'   => apply_filters( 'important_data_title',   $title,     $title, $content ),
        'content' => apply_filters( 'important_data_content', $content,   $title, $content ),
        'status'  => apply_filters( 'important_data_status',  'not-spam', $title, $content ),
    ) );
}

Now, you can modify the data before it hits the database. But what if you want to stop it from going into the database in the first place? Cue another complaining session:

    // WEAPON LOCKED
}
function insert_some_important_data( $title, $content = '' ) {
    global $database;

    if ( !apply_filters( 'important_data_should_insert', true, $title, $content ) )
            return false;
    return $database->insert( $database->important_data_table, array(
        'title'   => apply_filters( 'important_data_title',   $title,     $title, $content ),
        'content' => apply_filters( 'important_data_content', $content,   $title, $content ),
        'status'  => apply_filters( 'important_data_status',  'not-spam', $title, $content ),
    ) );
}

Now you can cancel the data from hitting the database. However, there’s still no notification after the data is sent to the database. This cycle can go on forever if you’re not careful. Your function will eventually end up being eight million lines long.

Advertisements